T
Tony
I have to create form based on query, which in turn needs to have criteria
based on list box form the form. I have difficulties passing criteria. I have
added hidden field on the form which will hold criteria string to be used in
query. When I am testing my code by entering fixed text with just one value
strCriteria = "5901"
all is working, but when I use several values
strCriteria = "5901" & " Or " & "5102"
it does not. What I am doing wrong, how I need to modify my code
For Each valSel In Me!CarOrSetNo.ItemsSelected
strCriteria = strCriteria & """" & Me!CarOrSetNo.ItemData(valSel) &
""" Or "
Next valSel
to have it working ?
Code from my form below:
=====================================
Private Sub Command2_Click()
Dim QueryString As String
Dim stDocName As String
Dim strCriteria As String
strCriteria = ""
For Each valSel In Me!CarOrSetNo.ItemsSelected
strCriteria = strCriteria & """" & Me!CarOrSetNo.ItemData(valSel) &
""" Or "
Next valSel
' strCriteria = Left(strCriteria, Len(strCriteria) - 4)
strCriteria = "5901" & " Or " & "5102"
QueryString = strCriteria
On Error GoTo Err_Command2_Click
Me.StringforQuery = QueryString
MsgBox Me.StringforQuery
' stDocName = "q_Rpt_Incident_bySelectedCar"
' DoCmd.Requery stDocName
stDocName = "r_IncidentDetails_bySelectedCar"
DoCmd.OpenReport stDocName, acPreview
Exit_Command2_Click:
Exit Sub
Err_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_Click
End Sub
=======================================
Thanks for help.
Tony
based on list box form the form. I have difficulties passing criteria. I have
added hidden field on the form which will hold criteria string to be used in
query. When I am testing my code by entering fixed text with just one value
strCriteria = "5901"
all is working, but when I use several values
strCriteria = "5901" & " Or " & "5102"
it does not. What I am doing wrong, how I need to modify my code
For Each valSel In Me!CarOrSetNo.ItemsSelected
strCriteria = strCriteria & """" & Me!CarOrSetNo.ItemData(valSel) &
""" Or "
Next valSel
to have it working ?
Code from my form below:
=====================================
Private Sub Command2_Click()
Dim QueryString As String
Dim stDocName As String
Dim strCriteria As String
strCriteria = ""
For Each valSel In Me!CarOrSetNo.ItemsSelected
strCriteria = strCriteria & """" & Me!CarOrSetNo.ItemData(valSel) &
""" Or "
Next valSel
' strCriteria = Left(strCriteria, Len(strCriteria) - 4)
strCriteria = "5901" & " Or " & "5102"
QueryString = strCriteria
On Error GoTo Err_Command2_Click
Me.StringforQuery = QueryString
MsgBox Me.StringforQuery
' stDocName = "q_Rpt_Incident_bySelectedCar"
' DoCmd.Requery stDocName
stDocName = "r_IncidentDetails_bySelectedCar"
DoCmd.OpenReport stDocName, acPreview
Exit_Command2_Click:
Exit Sub
Err_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_Click
End Sub
=======================================
Thanks for help.
Tony