JSalle0826
Registered User.
- Local time
- Today, 13:16
- Joined
- Mar 7, 2012
- Messages
- 14
Hello all,
I am getting a Run Time Error for some code in which I am trying to Run a Query and use a multi-select list box as criteria for the query. I built the Query, and created a form with an unbound control with the list box and the items I want. I created an "OK" button on the form in which I want the query to provide me with email addresses for the departments in which they select on the form.
Here is my code:
Private Sub OK_Click()
Dim Q As QueryDef, DB As Database
Dim Criteria As String
Dim ctl As Control
Dim Itm As Variant
' Build a list of the selections.
Set ctl = Me![List0]
For Each Itm In ctl.ItemsSelected
If Len(Criteria) = 0 Then
Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
Else
Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
& Chr(34)
End If
Next Itm
If Len(Criteria) = 0 Then
Itm = MsgBox("You must select one or more items in the" & _
" list box!", 0, "No Selection Made")
Exit Sub
End If
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("EmployeeQuery2")
Q.SQL = "SELECT Employee.Email, Departments.[Department ID], * FROM Employee INNER JOIN (Departments INNER JOIN [Department Details] ON Departments.[Department ID] = [Department Details].[Department ID]) ON Employee.[Employee ID] = [Department Details].[Employee ID] WHERE Departments.[Department ID]= In(" & Criteria & ");"
Q.Close
' Run the query.
DoCmd.OpenQuery "EmployeeQuery2"
End Sub
I am getting a Run Time Error for some code in which I am trying to Run a Query and use a multi-select list box as criteria for the query. I built the Query, and created a form with an unbound control with the list box and the items I want. I created an "OK" button on the form in which I want the query to provide me with email addresses for the departments in which they select on the form.
Here is my code:
Private Sub OK_Click()
Dim Q As QueryDef, DB As Database
Dim Criteria As String
Dim ctl As Control
Dim Itm As Variant
' Build a list of the selections.
Set ctl = Me![List0]
For Each Itm In ctl.ItemsSelected
If Len(Criteria) = 0 Then
Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
Else
Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
& Chr(34)
End If
Next Itm
If Len(Criteria) = 0 Then
Itm = MsgBox("You must select one or more items in the" & _
" list box!", 0, "No Selection Made")
Exit Sub
End If
' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("EmployeeQuery2")
Q.SQL = "SELECT Employee.Email, Departments.[Department ID], * FROM Employee INNER JOIN (Departments INNER JOIN [Department Details] ON Departments.[Department ID] = [Department Details].[Department ID]) ON Employee.[Employee ID] = [Department Details].[Employee ID] WHERE Departments.[Department ID]= In(" & Criteria & ");"
Q.Close
' Run the query.
DoCmd.OpenQuery "EmployeeQuery2"
End Sub