Re: Multi-Select ListBox
- From: "Tom" <anonomys@xxxxxxxx>
- Date: Thu, 19 May 2005 21:45:17 -0400
Doug:
I still get the same error... once I click OK, the line "qdf.SQL" is
highlighted.
' Apply the new SQL statement to the query
qdf.SQL = strSQL
BTW, thanks for the info about the reserved word.
Any ideas what else might be wrong?
--
Thanks,
Tom
"Douglas J. Steele" <NOSPAM_djsteele@xxxxxxxxxxxxxxxxx> wrote in message
news:ea%23a%23VNXFHA.3032@xxxxxxxxxxxxxxxxxxxxxxx
> One possibility is the field Date in your SQL: that's a reserved word, and
> should never be used for your own purposes.
>
> If you can't change the database design, try putting square brackets
> around that word:
>
> strSQL = "SELECT ID, Region, State, [Date] FROM tblData " & _
> "WHERE tblData.Region IN(" & strCriteria & ");"
>
>
>
> --
> Doug Steele, Microsoft Access MVP
> http://I.Am/DougSteele
> (no e-mails, please!)
>
>
>
> "Tom" <anonomys@xxxxxxxx> wrote in message
> news:enaQfLNXFHA.3996@xxxxxxxxxxxxxxxxxxxxxxx
>>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
>>
>> --
>> Thanks,
>> Tom
>>
>>
>>
>
>
.
- References:
- Multi-Select ListBox
- From: Tom
- Re: Multi-Select ListBox
- From: Douglas J. Steele
- Multi-Select ListBox
- Prev by Date: Re: protect data in a form, but allow for data entry too
- Next by Date: Re: JPG File Pasted on a Page of a Tab Control
- Previous by thread: Re: Multi-Select ListBox
- Next by thread: Re: Multi-Select ListBox
- Index(es):
Relevant Pages
|