I have a form in my database where people select multiple query inputs from multiple list boxes but when I try to run it I keep getting a run-time error 3075 that says:
Syntax error (missing operator) in query expression
'tblOffender.[AreaOffice] Like '*'tblOffender.[ReleaseType] Like
'*'tblOffender.[RPV] Like '*'tblOffender.[AtualReleaseDate] 1=1
tblOffender.[IncomingDate] 1=1'.
When I debug, it highlights: qdf.SQL = strSQL
I'm not very knowledgeable about programming (this is my first attempt) but I followed some instructions I found online as accurately as possible. I just can't figure what's wrong.
Could someone please help me with this? I'd very much appreciate it.
Here is my code (in Access 2007):
Private Sub cmdOk_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim blnQueryExists As Boolean
Dim varItem As Variant
Dim strArea As String
Dim strType As String
Dim strRPV As String
Dim strSortOrder
Dim strSQL As String
' Check for the existence of the stored query
Set db = CurrentDb
blnQueryExists = False
For Each qdf In db.QueryDefs
If qdf.Name = "qryCustom" Then
blnQueryExists = True
Exit For
End If
Next qdf
' Create the query if it does not already exist
If blnQueryExists = False Then
Set qdf = db.CreateQueryDef("qryCustom")
End If
Application.RefreshDatabaseWindow
' Turn off screen updating
DoCmd.Echo False
' Close the query if it is already open
If SysCmd(acSysCmdGetObjectState, acQuery, "qryCustom") = acObjStateOpen Then
DoCmd.Close acQuery, "qryCustom"
End If
' Build criteria string for Area
For Each varItem In Me.lstArea.ItemsSelected
strArea = strArea & ",'" & Me.lstArea.ItemData(varItem) & "'"
Next varItem
If Len(strArea) = 0 Then
strArea = "Like '*'"
Else
strArea = Right(strArea, Len(strArea) - 1)
strArea = "IN(" & strArea & ")"
End If
' Build criteria string for Type
For Each varItem In Me.lstType.ItemsSelected
strType = strType & ",'" & Me.lstType.ItemData(varItem) & "'"
Next varItem
If Len(strType) = 0 Then
strType = "Like '*'"
Else
strType = Right(strType, Len(strType) - 1)
strType = "IN(" & strType & ")"
End If
' Build criteria string for RPV
For Each varItem In Me.lstRPV.ItemsSelected
strRPV = strRPV & ",'" & Me.lstRPV.ItemData(varItem) & "'"
Next varItem
If Len(strRPV) = 0 Then
strRPV = "Like '*'"
Else
strRPV = Right(strRPV, Len(strRPV) - 1)
strRPV = "IN(" & strRPV & ")"
End If
' Build sort clause
If Me.cboSortOrder1.Value <> "Not Sorted" Then
strSortOrder = " ORDER BY tblOffender.[" & Me.cboSortOrder1.Value & "]"
If Me.cboSortOrder2.Value <> "Not Sorted" Then
strSortOrder = strSortOrder & ",tblOffender.[" & Me.cboSortOrder2.Value & "]"
If Me.cboSortOrder3.Value <> "Not Sorted" Then
strSortOrder = strSortOrder & ",tblOffender.[" & Me.cboSortOrder3.Value & "]"
End If
End If
Else
strSortOrder = ""
End If
' Release Date Range
Dim strOut As String
strOut = "1=1 "
If Not IsNull(Me.txtStartOut) Then
strOut = strOut & " AND " & _
" >= #" & Me.txtStartOut & "# "
End If
If Not IsNull(Me.txtEndOut) Then
strOut = strOut & " AND " & _
" <= #" & Me.txtEndOut & "# "
End If
' Incoming Date Range
Dim strIn As String
strIn = "1=1 "
If Not IsNull(Me.txtStartIn) Then
strIn = strIn & " AND " & _
" >= #" & Me.txtStartIn & "# "
End If
If Not IsNull(Me.txtEndIn) Then
strIn = strIn & " AND " & _
" <= #" & Me.txtEndIn & "# "
End If
' Build SQL statement
strSQL = "SELECT tblOffender.* FROM tblOffender " & _
"WHERE tblOffender.[AreaOffice] " & strArea & _
"tblOffender.[ReleaseType] " & strType & _
"tblOffender.[RPV] " & strRPV & _
"tblOffender.[ActualReleaseDate] " & strOut & _
"tblOffender.[IncomingDate] " & strIn & _
strSortOrder & ";"
' Apply the SQL statement to the stored query
Set db = CurrentDb
Set qdf = db.QueryDefs("qryCustom")
qdf.SQL = strSQL
Set qdf = Nothing
Set db = Nothing
' Open the Query
DoCmd.OpenQuery "qryCustom"
' Restore screen updating
DoCmd.Echo True
End Sub
Syntax error (missing operator) in query expression
'tblOffender.[AreaOffice] Like '*'tblOffender.[ReleaseType] Like
'*'tblOffender.[RPV] Like '*'tblOffender.[AtualReleaseDate] 1=1
tblOffender.[IncomingDate] 1=1'.
When I debug, it highlights: qdf.SQL = strSQL
I'm not very knowledgeable about programming (this is my first attempt) but I followed some instructions I found online as accurately as possible. I just can't figure what's wrong.
Could someone please help me with this? I'd very much appreciate it.
Here is my code (in Access 2007):
Private Sub cmdOk_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim blnQueryExists As Boolean
Dim varItem As Variant
Dim strArea As String
Dim strType As String
Dim strRPV As String
Dim strSortOrder
Dim strSQL As String
' Check for the existence of the stored query
Set db = CurrentDb
blnQueryExists = False
For Each qdf In db.QueryDefs
If qdf.Name = "qryCustom" Then
blnQueryExists = True
Exit For
End If
Next qdf
' Create the query if it does not already exist
If blnQueryExists = False Then
Set qdf = db.CreateQueryDef("qryCustom")
End If
Application.RefreshDatabaseWindow
' Turn off screen updating
DoCmd.Echo False
' Close the query if it is already open
If SysCmd(acSysCmdGetObjectState, acQuery, "qryCustom") = acObjStateOpen Then
DoCmd.Close acQuery, "qryCustom"
End If
' Build criteria string for Area
For Each varItem In Me.lstArea.ItemsSelected
strArea = strArea & ",'" & Me.lstArea.ItemData(varItem) & "'"
Next varItem
If Len(strArea) = 0 Then
strArea = "Like '*'"
Else
strArea = Right(strArea, Len(strArea) - 1)
strArea = "IN(" & strArea & ")"
End If
' Build criteria string for Type
For Each varItem In Me.lstType.ItemsSelected
strType = strType & ",'" & Me.lstType.ItemData(varItem) & "'"
Next varItem
If Len(strType) = 0 Then
strType = "Like '*'"
Else
strType = Right(strType, Len(strType) - 1)
strType = "IN(" & strType & ")"
End If
' Build criteria string for RPV
For Each varItem In Me.lstRPV.ItemsSelected
strRPV = strRPV & ",'" & Me.lstRPV.ItemData(varItem) & "'"
Next varItem
If Len(strRPV) = 0 Then
strRPV = "Like '*'"
Else
strRPV = Right(strRPV, Len(strRPV) - 1)
strRPV = "IN(" & strRPV & ")"
End If
' Build sort clause
If Me.cboSortOrder1.Value <> "Not Sorted" Then
strSortOrder = " ORDER BY tblOffender.[" & Me.cboSortOrder1.Value & "]"
If Me.cboSortOrder2.Value <> "Not Sorted" Then
strSortOrder = strSortOrder & ",tblOffender.[" & Me.cboSortOrder2.Value & "]"
If Me.cboSortOrder3.Value <> "Not Sorted" Then
strSortOrder = strSortOrder & ",tblOffender.[" & Me.cboSortOrder3.Value & "]"
End If
End If
Else
strSortOrder = ""
End If
' Release Date Range
Dim strOut As String
strOut = "1=1 "
If Not IsNull(Me.txtStartOut) Then
strOut = strOut & " AND " & _
" >= #" & Me.txtStartOut & "# "
End If
If Not IsNull(Me.txtEndOut) Then
strOut = strOut & " AND " & _
" <= #" & Me.txtEndOut & "# "
End If
' Incoming Date Range
Dim strIn As String
strIn = "1=1 "
If Not IsNull(Me.txtStartIn) Then
strIn = strIn & " AND " & _
" >= #" & Me.txtStartIn & "# "
End If
If Not IsNull(Me.txtEndIn) Then
strIn = strIn & " AND " & _
" <= #" & Me.txtEndIn & "# "
End If
' Build SQL statement
strSQL = "SELECT tblOffender.* FROM tblOffender " & _
"WHERE tblOffender.[AreaOffice] " & strArea & _
"tblOffender.[ReleaseType] " & strType & _
"tblOffender.[RPV] " & strRPV & _
"tblOffender.[ActualReleaseDate] " & strOut & _
"tblOffender.[IncomingDate] " & strIn & _
strSortOrder & ";"
' Apply the SQL statement to the stored query
Set db = CurrentDb
Set qdf = db.QueryDefs("qryCustom")
qdf.SQL = strSQL
Set qdf = Nothing
Set db = Nothing
' Open the Query
DoCmd.OpenQuery "qryCustom"
' Restore screen updating
DoCmd.Echo True
End Sub