Hi All,
I've been using the following code successfully in Access 2003 & now I need to migrate to Access 2010. The purpose of the code is to use the items that the user selects in the list box to build the criteria of a query. Access 2010 keeps giving me a syntax error when I try to run the query & I don't know why:
My code is:
On Error GoTo Err_Command151_Click
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelectContractSummary3")
' Loop through the selected items in the list box and build a text string
For Each varItem In Me!List58.ItemsSelected
strCriteria = strCriteria & ".'" & Me!List58.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Msg from ZP!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM qryContractListSummarybyDateContract3TYPEBREAK " & _
"WHERE qryContractListSummarybyDateContract3TYPEBREAK.ReportableName IN(" & strCriteria & ");"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenReport "rptMarginReportSummary4", acPreview, , strSQL
' Empty the memory
Set db = Nothing
Set qdf = Nothing
Exit_Command151_Click:
Exit Sub
Err_Command151_Click:
MsgBox Err.Description
Resume Exit_Command151_Click
End Sub
The syntax error I get in Access 2010 is:
Syntax Error in query expression 'SELECT * FROM
qryContractListSummarybyDateContract3TYPEBREAK WHERE
qryContractListSummarybyDateContract3TYPEBREAK.ReportableName IN('Adbri
Masonry NSW');'
Can anyone please help?
Thanks!
I've been using the following code successfully in Access 2003 & now I need to migrate to Access 2010. The purpose of the code is to use the items that the user selects in the list box to build the criteria of a query. Access 2010 keeps giving me a syntax error when I try to run the query & I don't know why:
My code is:
On Error GoTo Err_Command151_Click
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelectContractSummary3")
' Loop through the selected items in the list box and build a text string
For Each varItem In Me!List58.ItemsSelected
strCriteria = strCriteria & ".'" & Me!List58.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Msg from ZP!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM qryContractListSummarybyDateContract3TYPEBREAK " & _
"WHERE qryContractListSummarybyDateContract3TYPEBREAK.ReportableName IN(" & strCriteria & ");"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenReport "rptMarginReportSummary4", acPreview, , strSQL
' Empty the memory
Set db = Nothing
Set qdf = Nothing
Exit_Command151_Click:
Exit Sub
Err_Command151_Click:
MsgBox Err.Description
Resume Exit_Command151_Click
End Sub
The syntax error I get in Access 2010 is:
Syntax Error in query expression 'SELECT * FROM
qryContractListSummarybyDateContract3TYPEBREAK WHERE
qryContractListSummarybyDateContract3TYPEBREAK.ReportableName IN('Adbri
Masonry NSW');'
Can anyone please help?
Thanks!