I am trying to use vba where the users typed in something in a text box and the vba would execute the query with what the user typed in as a criteria. However, I keep getting error that says "Expected: end of statement"
Private Sub btnSearch_Click()
On Error GoTo errorHandler
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qdf As QueryDef
Dim sql As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("Expiry")
sqlString = "SELECT Format([Expiry_Date], ""mmmm"") AS [Month], Sum([Contracts].[Contract _Value (S$)]) AS [Contract Value], Count([Contracts].[Contract No]) AS [Number of Contract] FROM [Contracts] WHERE Year([Expiry_Date])= '" & Me.txtExpiryYear & "' GROUP BY Format([Expiry_Date],""mmmm"")"
Debug.Print sql
qdf.sql = sqlString
If Nz(Me.txtExpiryYear, "") = "" Then
MsgBox "Please enter the year"
Resume Exit_Update
End If
Set rs = db.OpenRecordset(sqlString)
qdf.Close
CurrentDb.Close
Exit_Update:
Exit Sub
errorHandler:
If Err.Number = 3075 Then
MsgBox Err.Description
Resume Exit_Update
End If
End Sub