Results 1 to 6 of 6
  1. #1
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76

    multiple list box problem continued

    Hi, I post my solution for the multiple list box pass selection to query before, however, I just found a error while test my project. If I unselect all my selection in the list box, it will show me that my strSQL statment is error, expect select, update or insert... can anyone help me to figure it out?

    Code:
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim varItem As Variant
    Dim strCriteria As String
    Dim lngLen As Long
    Dim strDelim As String
    Dim strWhere As String
    strDelim = """"
    Dim strSQL As String
    Set db = CurrentDb()
    Set qdf = db.QueryDefs("Selection2")
    With Me.Spc_Slc
     For Each varItem In .ItemsSelected
        If Not IsNull(varItem) Then
        strWhere = strWhere & strDelim & .ItemData(varItem) & strDelim & ","
        strCriteria = strCriteria & """" & .Column(1, varItem) & """, "
        Else
         MsgBox "please select at least one species.", vbExclamation
        End If
     Next varItem
    End With
    lngLen = Len(strWhere) - 1
    If lngLen > 0 Then
       strWhere = "Species IN (" & Left$(strWhere, lngLen) & ")"
       lngLen = Len(strCriteria) - 2
       If lngLen > 0 Then
        strSQL = "SELECT Species, SpeciesRef FROM Species WHERE Species.Species IN (" & strCriteria & ");"
       End If
    End If
    qdf.SQL = strSQL <==debug error here


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    This will help you see what the final SQL is:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    Sorry, I'm new to the Access, no idea how to use the immediate Window, I just type ?strSQL into the immediate window and it returns nothing, is that means my SQL returns Null? But if I didn't unselect all my selection from list box, I still got the result I want...
    Quote Originally Posted by pbaldy View Post
    This will help you see what the final SQL is:

    http://www.baldyweb.com/ImmediateWindow.htm

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Using the ? method would only work with a breakpoint or in debug mode. Following your code, what do you suppose it will do at the end if you don't make any selections? Specifically here:


    If lngLen > 0 Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ice051505 is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Feb 2013
    Posts
    76
    I changed the code under lngLen>0, but it still give me the same error
    Here is the part of my new code, can you check for me?

    Code:
    lngLen = Len(strWhere) - 1
    If lngLen > 0 Then
       strWhere = "Species IN (" & Left$(strWhere, lngLen) & ")"
       lngLen = Len(strCriteria) - 2
       strSQL = "SELECT Species, SpeciesRef FROM Species WHERE Species.Species IN (" & strCriteria & ");"
    Else
       If IsNull(varItem) Then
       MsgBox "You must select at least one species!"
       DoCmd.CancelEvent
       End If
    End If
    qdf.SQL = strSQL

    Quote Originally Posted by pbaldy View Post
    Using the ? method would only work with a breakpoint or in debug mode. Following your code, what do you suppose it will do at the end if you don't make any selections? Specifically here:


    If lngLen > 0 Then

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Note the test I use here to make sure something is selected:

    http://www.baldyweb.com/multiselect.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Background Problem on Multiple List Subform
    By dhogan444 in forum Forms
    Replies: 2
    Last Post: 10-24-2012, 10:02 AM
  2. Replies: 23
    Last Post: 12-06-2011, 09:18 AM
  3. Header Groupings Continued on second page
    By herbc0704 in forum Reports
    Replies: 1
    Last Post: 12-29-2010, 12:25 PM
  4. Exception Reporting - Continued
    By shexe in forum Queries
    Replies: 11
    Last Post: 09-20-2010, 11:20 AM
  5. print continued at bottom of report
    By ecpike in forum Reports
    Replies: 2
    Last Post: 02-25-2010, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums