The relevant code doesn't get highlighted
I am trying to take criteria from the list box I have created and put it into a query, I have created a query with the following code:
SELECT DISTINCT UNI7LIVE_SCROLE.rolename
FROM UNI7LIVE_SCROLE;
UNION SELECT "All"
FROM UNI7LIVE_SCROLE;
called Select from table row source
The table I am getting the data from is UNI7LIVE_SCROLE
Private Sub Run_Query_Click()
On Error GoTo Err_Run_Query_Click
Dim stdocname As String
On Error GoTo Err_Run_Query_Click
Dim mydb As DAO.Database
'Dim mydb As AccessObject
Dim qdef As DAO.QueryDef
'Dim qdef As AccessObject
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set mydb = CurrentDb()
strSQL = "SELECT * FROM UNI7LIVE_SCROLE.rolename"
'Build the IN string by looping through the listbox
For i = 0 To UNI7LIVE_SCROLE.ROLENAME.ListCount - 1
If UNI7LIVE_SCROLE.ROLENAME.selected(i) Then
If UNI7LIVE_SCROLE.ROLENAME.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & UNI7LIVE_SCROLE.ROLENAME.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [UNI7LIVE_SCROLE.rolename] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
mydb.QueryDefs.Delete "Select from table row source"
Set qdef = mydb.CreateQueryDef("Select from table row source", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "Select from table row source", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.Rolename_listbox.ItemsSelected
Me.Rolename_listbox.selected(varItem) = False
Next varItem
Exit_Run_Query_Click:
Exit Sub
Err_Run_Query_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_Run_Query_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_Run_Query_Click
End If
Exit Sub
'Clear listbox selection after running query
For Each varItem In Me.Rolename_listbox.ItemsSelected
Me.Rolename_listbox.selected(varItem) = False
Next varItem
End Sub
It would be great if anyone could help me
Many thanks
I am trying to take criteria from the list box I have created and put it into a query, I have created a query with the following code:
SELECT DISTINCT UNI7LIVE_SCROLE.rolename
FROM UNI7LIVE_SCROLE;
UNION SELECT "All"
FROM UNI7LIVE_SCROLE;
called Select from table row source
The table I am getting the data from is UNI7LIVE_SCROLE
Private Sub Run_Query_Click()
On Error GoTo Err_Run_Query_Click
Dim stdocname As String
On Error GoTo Err_Run_Query_Click
Dim mydb As DAO.Database
'Dim mydb As AccessObject
Dim qdef As DAO.QueryDef
'Dim qdef As AccessObject
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set mydb = CurrentDb()
strSQL = "SELECT * FROM UNI7LIVE_SCROLE.rolename"
'Build the IN string by looping through the listbox
For i = 0 To UNI7LIVE_SCROLE.ROLENAME.ListCount - 1
If UNI7LIVE_SCROLE.ROLENAME.selected(i) Then
If UNI7LIVE_SCROLE.ROLENAME.Column(0, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & UNI7LIVE_SCROLE.ROLENAME.Column(0, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [UNI7LIVE_SCROLE.rolename] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
mydb.QueryDefs.Delete "Select from table row source"
Set qdef = mydb.CreateQueryDef("Select from table row source", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "Select from table row source", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.Rolename_listbox.ItemsSelected
Me.Rolename_listbox.selected(varItem) = False
Next varItem
Exit_Run_Query_Click:
Exit Sub
Err_Run_Query_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_Run_Query_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_Run_Query_Click
End If
Exit Sub
'Clear listbox selection after running query
For Each varItem In Me.Rolename_listbox.ItemsSelected
Me.Rolename_listbox.selected(varItem) = False
Next varItem
End Sub
It would be great if anyone could help me
Many thanks