Microsoft Access VBA - Runtime Error '3075'

I encountered a '3075' runtime error. I am new to VBA! > & L .; May I know where everything went wrong? I canโ€™t solve it ...

For example, if I entered the name "Sally" in the text box (txtMainName), a pop-up window appears when I click on the search button.

Error:

Runtime Error '3075':

A syntax error (missing statement) in the query expression 'AND [Applicantโ€™s primary name] As โ€œSallyโ€.

Public Sub Search_Record() Dim stDocName As String Dim stLinkCriteria As String Dim stLinkCriteria1 As String Dim stLinkCriteria2 As String stLinkCriteria = "" stDocName = "frmDisplayInfo" If (Me!txtMainName <> "") Then stLinkCriteria1 = "[Main Applicant Name] Like ' " & Me![txtMainName] & "'" stLinkCriteria = stLinkCriteria & " And " & stLinkCriteria1 End If If (Me!txtIDNo <> "") Then stLinkCriteria2 = "[ID No] Like ' " & Me![txtIDNo] & "'" stLinkCriteria = stLinkCriteria & " And " & stLinkCriteria2 End If '(*This part is highlighted*) DoCmd.OpenForm stDocName, , , stLinkCriteria DoCmd.Maximize End Sub 
+4
source share
3 answers

Runtime error 3975 Invalid operator , according to a quick Google search on access vba runtime error 3075 (which you could do yourself).

The problem is that you only assign the value "" stCriteria . If the first if is stCriteria , stCriteria then set to stCriteria AND stCriteria1 , which is not valid.

The same thing happens if the second if is executed. (In fact, if both are actually running, stCriteria now contains AND stCriteria1 AND stCriteria2 , which is even worse.)

The solution is to modify your code to add AND if necessary:

 stLinkCriteria = "" stDocName = "frmDisplayInfo" If (Me!txtMainName <> "") Then stLinkCriteria = "[Main Applicant Name] Like ' " & Me![txtMainName] & "'" End If If (Me!txtIDNo <> "") Then If stLinkCriteria <> "" then stLinkCriteria = stLinkCriteria & " AND " End If stLinkCriteria2 = "[ID No] Like ' " & Me![txtIDNo] & "'" stLinkCriteria = stLinkCriteria & " And " & stLinkCriteria2 End If 

For further use, the way to solve these problems is to actually check the value of the variable (in this case stCriteria ) just before the line causing the error (here it is the DoCmd.OpenForm line) is executed, you do this by setting a breakpoint on line where the error occurs, run the application until a breakpoint is reached, and then check the value of the variable. This shows what exactly contains the variable that may reveal the problem.

+6
source

From the logic it seems that your stLinkCriteria starts with an "and", which causes an error:

You should probably put:

 If (Me!txtMainName <> "") Then stLinkCriteria1 = "[Main Applicant Name] Like ' " & Me![txtMainName] & "'" if stLinkCriteria <> "" then <==== add this stLinkCriteria = stLinkCriteria & " and " <==== and this end if <==== and this stLinkCriteria = stLinkCriteria & stLinkCriteria1 <=== change this End If 

Hope this works - otherwise the logic looks good.

+3
source

I get this error from the extra space between the point and the field name in:

 DoCmd.RunSQL "UPDATE cp INNER JOIN [char_lib] ON [cp]. [library_id] = [char_lib].[id] set.. 

Note to Microsoft: Create more accurate and useful error messages. Thank you I have no doubt that you will get better.

0
source

All Articles