Вопрос

My Access 2013 database has two main tables; Customers and Sites. The Sites table has a foreign key pointing to a Customer record. I am building a search box into a Sites Form with an unbound text box and a command button. Once the button is pressed, it should filter the records based on the search criteria and VBA code. I need to be able to search by the Customer name, located in a separate table linked through a foreign key. So far, this is the code I have for the button:

Private Sub cmdSearch_Click()

Dim strSQL As String

strSQL = "SELECT * FROM tblMainSites " & _
            "INNER JOIN tblMainCustomers " & _
            "ON tblMainSites.Customer=tblMainCustomers.CustomerID " & _
            "WHERE tblMainCustomers.Customer LIKE '*" & Me.txtSearch & "*'"

If IsNull(Me.txtSearch) Then
    Me.Filter = ""
    Me.FilterOn = False
Else
    Debug.Print strSQL
    Me.Filter = strSQL
    Me.FilterOn = True
End If

End Sub

The problem is that every time I try to filter the records, I get the following:

"Run Time Error 3075, Syntax Error in Query Expression"

Using the Debug Print, it looks like the query expression I wrote is fine, and looks just like a working query I built within Access:

SELECT * FROM tblMainSites INNER JOIN tblMainCustomers ON tblMainSites.Customer=tblMainCustomers.CustomerID WHERE tblMainCustomers.Customer LIKE '*Affinity*'

My question is this: Am I using the Me.Filter method correctly? I cannot find any examples using a full SQL string. I've also tried using the DoCmd.OpenForm method with OpenArgs as the string, also to no avail.

Asking another way, is there a way to filter records in an Access form using a JOIN query?

My goal is to be able to search for a record in the Sites table while referencing the Customer name in the Customers table, since searching for the customer name in the Sites table fails due to it being the foreign key pointing to the Customers table.

Thanks in advance for your assistance!

Это было полезно?

Решение

Ok, after a lot more Googling, I found a site that told me I cannot use more than one table in a form without basing the form on a query.

Here's what I did:

I made a query called qrySites and it does this:

SELECT * FROM tblMainSites INNER JOIN tblMainCustomers ON tblMainSites.Customer = tblMainCustomers.CustomerID;

This gives me a recordset where the CustomerID is associated with the Customer name. I then changed the Record Source of the form frmSites to this new query, and updated the appropriate Control sources.

Then, the Search function filters records like this:

Private Sub cmdSearch_Click()

Dim strFilter As String

strFilter = "tblMainCustomers.Customer LIKE ""*" & Me.txtSearch & "*"""

If IsNull(Me.txtSearch) Then
    Me.Filter = "" 'Clears filter if text box is blank when searching
    Me.FilterOn = False
Else
    Me.Filter = strFilter
    Me.FilterOn = True
End If

End Sub

This gives the Me.Filter method the proper input, which is just the Where clause of the SQL query string.

I tested it, and the search works. The form also passes any changes through the query to the appropriate table, though I did have to check my control sources and some of the fields shared names in the tables.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top