T
Tom
I use a "Multi-Select" Listbox to select multiple values (consecutive or
non-consecutive)... the selected values are then used as query criteria.
Please refer to function below:
*********************************
Private Sub cmdOK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text
string
For Each varItem In Me!lstRegions.ItemsSelected
strCriteria = strCriteria & ",'" &
Me!lstRegions.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "qryMultiSelect"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
*********************************
Now, I want to add a 2nd ListBox (ListBoxChooseReport) to the function
above. The rowsource of that ListBox is e.g. "City; State". So, I
introduced a CASE statement into the function a shown in the modified
function below:
*********************************
Private Sub cmdOK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text
string
For Each varItem In Me!lstRegions.ItemsSelected
strCriteria = strCriteria & ",'" &
Me!lstRegions.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' INTRODUCED CASE STATEMENT
'=====================
Select Case Me.ListBoxChooseReport.Value
Case "City": 'Shows all but STATE field
' Build the new SQL statement incorporating the string
strSQL = "SELECT ID, Region, City, Date FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
Case "State": 'Shows all but CITY field
' Build the new SQL statement incorporating the string
strSQL = "SELECT ID, Region, State, Date FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
End Select
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "qryMultiSelect"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
*********************************
With the CASE statement, I get the Run-Time error 3129: "Invalid SQL
statement; expected DELETE, INSERT, PROCEDURE, SELECT, or UPDATE.
Any ideas how I can fix it?
Tom
non-consecutive)... the selected values are then used as query criteria.
Please refer to function below:
*********************************
Private Sub cmdOK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text
string
For Each varItem In Me!lstRegions.ItemsSelected
strCriteria = strCriteria & ",'" &
Me!lstRegions.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "qryMultiSelect"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
*********************************
Now, I want to add a 2nd ListBox (ListBoxChooseReport) to the function
above. The rowsource of that ListBox is e.g. "City; State". So, I
introduced a CASE statement into the function a shown in the modified
function below:
*********************************
Private Sub cmdOK_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryMultiSelect")
' Loop through the selected items in the list box and build a text
string
For Each varItem In Me!lstRegions.ItemsSelected
strCriteria = strCriteria & ",'" &
Me!lstRegions.ItemData(varItem) & "'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' INTRODUCED CASE STATEMENT
'=====================
Select Case Me.ListBoxChooseReport.Value
Case "City": 'Shows all but STATE field
' Build the new SQL statement incorporating the string
strSQL = "SELECT ID, Region, City, Date FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
Case "State": 'Shows all but CITY field
' Build the new SQL statement incorporating the string
strSQL = "SELECT ID, Region, State, Date FROM tblData " & _
"WHERE tblData.Region IN(" & strCriteria & ");"
End Select
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
DoCmd.OpenQuery "qryMultiSelect"
' Empty the memory
Set db = Nothing
Set qdf = Nothing
End Sub
*********************************
With the CASE statement, I get the Run-Time error 3129: "Invalid SQL
statement; expected DELETE, INSERT, PROCEDURE, SELECT, or UPDATE.
Any ideas how I can fix it?
Tom