Results 1 to 7 of 7
  1. #1
    redekopp is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108

    Multiple Selection List Box on form to generate query

    Good morning, i was hoping someone could help me with the code to accomplish this.



    I have a form (frmMultiDealerSalesByYear) with two unbound fields (fiscalyear) <- combo and (liDealreship) <-- multi select list box.

    I want both those forms to help generate a query, where the fiscal year will be one selection of the year of sale, and the dealership could be one or many selections.

    The query name is (qryDealerSalesByFiscalYear) . I have tried examples i found online, but i do not know how to edit them properly to fit my format i am guessing.

    One example i have tried with no luck is:
    Code:
       Dim Q As QueryDef, DB As Database
       Dim Criteria As String
       Dim ctl As Control
       Dim Itm As Variant
    
       ' Build a list of the selections.
       Set ctl = Me![liDealership]
    
       For Each Itm In ctl.ItemsSelected
          If Len(Criteria) = 0 Then
             Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
          Else
             Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
              & Chr(34)
          End If
       Next Itm
    
       If Len(Criteria) = 0 Then
          Itm = MsgBox("You must select one or more items in the" & _
            " list box!", 0, "No Selection Made")
          Exit Sub
       End If
    
       ' Modify the Query.
       Set DB = CurrentDb()
       Set Q = DB.QueryDefs("qryDealerSalesByFiscalYear")
       Q.SQL = "Select * From Orders Where [DealerName] In(" & Criteria & _
         ");"
       Q.Close
    
       ' Run the query.
       DoCmd.OpenQuery "qryDealerSalesByFiscalYear"
    Any help would be fantastic, thanks!

    edit: when i run that code, it comes back with "You cancelled the previous operation" and highlights my docmd.openquery

  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,521
    Probably

    Q.SQL = "Select * From Orders Where [DealerName] In(" & Criteria & _
    ") AND FiscalYear = " & Me.YearComboName

    Note most of us never let user into tables or queries directly, only forms and reports.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If the SQL isn't working, here's a common method to debug it:

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

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    I am not sure why you are getting the error. However, I would not worry about until after you are sure you are passing legitimate arguments/criteria to your query's parameters.

    Does the following make sense to you? Perhaps you can add a Debug.Print line to check that it is correct.
    Code:
       For Each Itm In ctl.ItemsSelected
          If Len(Criteria) = 0 Then
             Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
          Else
             Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
              & Chr(34)
          End If
       Next Itm
    http://www.baldyweb.com/immediatewindow.htm

  5. #5
    redekopp is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2015
    Location
    Saskatoon
    Posts
    108
    Honestly none of the code make sense to me, is there a better way to write it?? I literally copy and paste and then try to re-name properly.

  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,521
    The better way is to understand the code you've received. It's great to get sample code from others. I've gotten a lot of help that way. But the next step is to understand the code before you incorporate it into your app. 2 of us gave you the same method to debug the SQL, but it doesn't appear you've tried it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    What if you added the following line
    debug.print Criteria

    just after
    Next Itm

    Code:
    ...
    Next Itm
    debug.print Criteria
    ...
    then you could look at the result in you Immediate Window. You can use the keyboard shortcut to open the Immediate Window, Ctrl + G

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

Similar Threads

  1. Replies: 8
    Last Post: 09-15-2015, 05:37 AM
  2. Using Multiple Fields in Form to Generate Query
    By crawfish124 in forum Forms
    Replies: 4
    Last Post: 06-04-2014, 10:39 AM
  3. Replies: 4
    Last Post: 12-18-2013, 04:08 PM
  4. List Box Multiple Selection Against Query
    By fionny in forum Queries
    Replies: 3
    Last Post: 06-28-2013, 12:12 PM
  5. Replies: 2
    Last Post: 06-09-2012, 07:59 AM

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