A family of Microsoft relational database management systems designed for ease of use.
We literally don't see it either, since you did not post the code from the button click event.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have a form parameter in RptMth191, list box with row source "1901"; "1902"; etc. with extended multiselect and a cmdOK button to run the query command below. Every time I click I get the run-time error above. I don't see what I'm missing, please help. Thanks.
A family of Microsoft relational database management systems designed for ease of use.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
We literally don't see it either, since you did not post the code from the button click event.
Sorry, I thought the snip it picture that I posted would be viewable. Below is the command code:
Option Compare Database
Private Sub cmdOK_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryStlmt1901-07")
For Each varItem In Forms!StlmtSearch1.RptMth191.ItemsSelected
strCriteria = strCriteria & ",'" & Forms!StlmtSearch1.RptMth191.ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the RPT MTH list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM SETLMT_RPT1901-1907 " & _
"WHERE SETLMT_RPT1901-1907.RPT_MTH IN(" & strCriteria & ");"
THE ERROR HIGHLIGHTS THE BELOW LINE:
qdf.SQL = strSQL
DoCmd.OpenQuery "qryStlmt1901-07"
Set db = Nothing
Set qdf = Nothing
End Sub
Thanks for your suggestions. I've tried all of the above. Here is the truth of the matter, I'm actually coping code that works from another source; the only difference I can see is the strCriteria that is used in the code that works is "North"; "South"; etc. My strCriteria is 1901; 1902; etc. So I've tried putting "1901"; "1902"; etc and using strCriteria as String like the first code above. Then I've changed to strCriteria as Integer and the list is back to 1901; 1902; etc. Can this be the cause of the issue?
Below is the code that was copied from another source that actually works; (again the strCriteria as String in the list box is "North"; etc):
Option Compare Database
Private Sub cmdOK_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
For Each varItem In Me!lstRegions.ItemsSelected
strCriteria = strCriteria & ",'" & Me!lstRegions.ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryMultiSelect"
Set db = Nothing
Set qdf = Nothing
End Sub
Below is my code adapted to the most recent suggestions above; (now I'm getting a 13 error and the 3075):
Option Compare Database
Option Explicit
Private Sub cmdOK_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As Integer
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryStlmt1901-07")
For Each varItem In [Forms]![StlmtSearch1].[RptMth191].ItemsSelected
If strCriteria <> "" Then
strCriteria = strCriteria & ","
End If
strCriteria = strCriteria & ",'" & [Forms]![StlmtSearch1].[RptMth191].ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the RPT MTH list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM [SETLMT_RPT1901-1907] " & _
"WHERE [SETLMT_RPT1901-1907].[RPT_MTH] IN( & strCriteria & );"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryStlmt1901-07"
Set db = Nothing
Set qdf = Nothing
End Sub
Thanks again for your help.
You can't have a leading comma in the IN clause. So:
For Each varItem In Forms!StlmtSearch1.RptMth191.ItemsSelected
If strCriteria <> "" Then
strCriteria = strCriteria & ","
End If
strCriteria = strCriteria & "'" & Forms!StlmtSearch1.RptMth191.ItemData(varItem) & "'"
Next varItem
I see a few problems: