G
Guest
Hi there,
I use the following code to create a query using a multi-select list box.
Question: I want to add a second parameter, that receives its data from a
second list box on the same form. The second list box simply allows the user
to select a province (Only One). I want to incorporate both listbox values
selected as two criteria selections for the query to generate. How can i do
this using this code?
If Me.txtFormLoaded.Value = 1 Then
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String, strWhere As String
Dim i As Integer
Set db = CurrentDb
'*** create the query based on the information on the form
strSQL = "SELECT * FROM qryReportListAccreditedVetsArea "
strWhere = "Where AccreditationID IN( "
For i = 0 To lstAccreditation.ListCount - 1
If lstAccreditation.Selected(i) Then
strWhere = strWhere & lstAccreditation.Column(0, i) & ", "
End If
Next i
strWhere = Left(strWhere, Len(strWhere) - 2) & ");"
strSQL = strSQL & strWhere
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "qryMyQuery"
Set qdf = db.CreateQueryDef("qryMyQuery", strSQL)
'*** open the query
DoCmd.OpenReport "rptListtAccreditedVeterinarianArea",
acViewPreview, Me.RecordSource = "qryMyQuery"
'DoCmd.OpenQuery "qryMyQuery", acNormal, acEdit
End If
'Exit_cmdRunQuery_Click:
' Exit Sub
'Err_cmdRunQuery_Click:
' If Err.Number = 3265 Then '*** if the error is the query is
missing
' Resume Next '*** then skip the delete line and
resume on the next line
' Else
' MsgBox Err.Description '*** write out the error and
exit the sub
' Resume Exit_cmdRunQuery_Click
'End If
I use the following code to create a query using a multi-select list box.
Question: I want to add a second parameter, that receives its data from a
second list box on the same form. The second list box simply allows the user
to select a province (Only One). I want to incorporate both listbox values
selected as two criteria selections for the query to generate. How can i do
this using this code?
If Me.txtFormLoaded.Value = 1 Then
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String, strWhere As String
Dim i As Integer
Set db = CurrentDb
'*** create the query based on the information on the form
strSQL = "SELECT * FROM qryReportListAccreditedVetsArea "
strWhere = "Where AccreditationID IN( "
For i = 0 To lstAccreditation.ListCount - 1
If lstAccreditation.Selected(i) Then
strWhere = strWhere & lstAccreditation.Column(0, i) & ", "
End If
Next i
strWhere = Left(strWhere, Len(strWhere) - 2) & ");"
strSQL = strSQL & strWhere
'MsgBox strSQL
'*** delete the previous query
db.QueryDefs.Delete "qryMyQuery"
Set qdf = db.CreateQueryDef("qryMyQuery", strSQL)
'*** open the query
DoCmd.OpenReport "rptListtAccreditedVeterinarianArea",
acViewPreview, Me.RecordSource = "qryMyQuery"
'DoCmd.OpenQuery "qryMyQuery", acNormal, acEdit
End If
'Exit_cmdRunQuery_Click:
' Exit Sub
'Err_cmdRunQuery_Click:
' If Err.Number = 3265 Then '*** if the error is the query is
missing
' Resume Next '*** then skip the delete line and
resume on the next line
' Else
' MsgBox Err.Description '*** write out the error and
exit the sub
' Resume Exit_cmdRunQuery_Click
'End If