Frankie_The_Flyer
Registered User.
- Local time
- Today, 09:04
- Joined
- Aug 16, 2013
- Messages
- 39
Hi Accessers!
I've got a code that allows me to select one or many names from a listbox on a form and return data relevant to the name(s) selected from a query.
The following code is triggered by a button on the form...
Private Sub Toggle4_Click()
'Set it all up for CSM selection
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
'Identify Query
Set db = CurrentDb()
Set qdf = db.QueryDefs("Qry_CSM")
'Find selected data in Listbox
For Each varItem In Me!CSM_List.ItemsSelected
strCriteria = strCriteria & "'" & Me!CSM_List.ItemData(varItem) & "',"
Next varItem
' Warning if none selected
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
'OstrCriteria = Right(strCriteria, Len(strCriteria) - 1)
'Prepare String for query
strSQL = "SELECT * FROM tbl_CSM " & "WHERE tbl_CSM.CSM IN(" & strCriteria & "); "
' put it in the Query
Debug.Print strSQL
qdf.SQL = strSQL
qdf.Close
Call Toggle10_Click
End Sub
(Toggle 10 goes to the code for the second listbox which has the same code with different tables refered to giving two selections in the query.)
What I want to do is replace the "warning if nothing found" with a code to show data against all the names in the list box if nothing is selected in the listbox.
Any ideas please??
I've got a code that allows me to select one or many names from a listbox on a form and return data relevant to the name(s) selected from a query.
The following code is triggered by a button on the form...
Private Sub Toggle4_Click()
'Set it all up for CSM selection
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
'Identify Query
Set db = CurrentDb()
Set qdf = db.QueryDefs("Qry_CSM")
'Find selected data in Listbox
For Each varItem In Me!CSM_List.ItemsSelected
strCriteria = strCriteria & "'" & Me!CSM_List.ItemData(varItem) & "',"
Next varItem
' Warning if none selected
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
'OstrCriteria = Right(strCriteria, Len(strCriteria) - 1)
'Prepare String for query
strSQL = "SELECT * FROM tbl_CSM " & "WHERE tbl_CSM.CSM IN(" & strCriteria & "); "
' put it in the Query
Debug.Print strSQL
qdf.SQL = strSQL
qdf.Close
Call Toggle10_Click
End Sub
(Toggle 10 goes to the code for the second listbox which has the same code with different tables refered to giving two selections in the query.)
What I want to do is replace the "warning if nothing found" with a code to show data against all the names in the list box if nothing is selected in the listbox.
Any ideas please??