VBA Query gets Syntax Error (missing operator)
Hi there,
I'm getting an error with the following message which I wonder if anyone could help me with? :
I enter the text in, select the area of the db to search, and it runs the relevant query with the typed in text ("A bit of Text" in this case) within the where clause. Then it opens the form which uses the query as its source.
here's my code:
I'm getting quite desperate now! I shall keep trying though.
Hi there,
I'm getting an error with the following message which I wonder if anyone could help me with? :
Basically, I have a form:Run-time Error '3075'
Syntax Error (Missing Operator) in query expression
'tblContact.chrContactCompany LIKE A bit of Text'
I enter the text in, select the area of the db to search, and it runs the relevant query with the typed in text ("A bit of Text" in this case) within the where clause. Then it opens the form which uses the query as its source.
here's my code:
Code:
Option Compare Database
Option Explicit
Private Sub btnSearch_Click()
Dim BeginningSQL As String
Dim SearchContactWhere As String
Dim ContactSearchFinal As String
' If the textbox has no value then tell user they need to enter something
'otherwise, move to the case statement in the else clause
If IsNull(Me.TxtSearchText) Then
MsgBox "No text entered. Please enter some text to search"
Exit Sub
Else
'MsgBox Me.TxtSearchText, vbInformation, "This is in the string"
Select Case (cboSearchPlace)
Case Is = "Contacts"
BeginningSQL = "SELECT tblContact.chrContactCompany, tblContact.chrContactFirstName, tblContact.chrContactSurname, tblContact.idsContact_ID FROM tblContact WHERE "
SearchContactWhere = "tblContact.chrContactCompany" & " LIKE " & Me.TxtSearchText ' "Or tblContact.chrContactFirstName LIKE " & Forms!frmSearch!TxtSearchText "Or tblContact.chrContactSurname LIKE " & Forms!frmSearch!TxtSearchText
MsgBox SearchContactWhere ' messagebox for error checking
' concatenate the 2 bits of SQL to make the full statement
ContactSearchFinal = BeginningSQL & SearchContactWhere
MsgBox ContactSearchFinal
'replace the query "qrySearchContacts" with the VBA created string
CurrentDb.QueryDefs("qrySearchContacts").SQL = ContactSearchFinal
MsgBox ContactSearchFinal
'open the form which is based on the query "qrySearchContacts"
DoCmd.OpenForm "frmSearchContactsResults"
Case Is = "Sites"
MsgBox "Searching in Sites only", vbInformation
Exit Sub
Case Is = "Products"
MsgBox "Searching in Products only", vbInformation
Exit Sub
Case Else
MsgBox "Please enter a section to search", vbOKOnly
Exit Sub
End Select
End If
End Sub
Last edited: