Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295

    Using the listbox item selection

    Good Day All,



    I have a listbox "List2" on a form "CustomerList". I activate this form by a button "Button1". Button1 starts a macro which loads the form "CustomerList". I want a use a where condition to open the form with only those items associated with the name selected in the list. I am opening a form "Invoice Payments". "Invoice Payments" data source contains a table "Customers" with a field "Company"


    My approach is to use this code:


    [Customers]![Company] =
    [List2]


    Can anyone assist in this solution

  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
    This will work if the listbox is single select:

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

  3. #3
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Thanks. I will try it. But of course it can't be used in a macro?

  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,521
    Yes, there's a where condition argument for the OpenForm macro action. The syntax is a little different, so you may need to play with it.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Ok I tried with I cannot find the code to make it work, so I am back to coding.

    This is my code but it has a hiccup. can you please comment on it. The error message suggest there is a problem with the syntax in where condition


    Selection = Me.List2.Column(1, Me.List2.ListIndex)

    Condition = "forms![company]" & " = " & Selection

    DoCmd.OpenForm "Invoice Payments", acNormal, , Condition
    End Sub][/CODE]

  6. #6
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Ok I tried with I cannot find the code to make it work, so I am back to coding.

    This is my code but it has a hiccup. can you please comment on it. The error message suggest there is a problem with the syntax in where condition

    Code:
    Selection = Me.List2.Column(1, Me.List2.ListIndex)
    
    Condition = "forms![company]" & " = " & Selection
    
    DoCmd.OpenForm "Invoice Payments", acNormal, , Condition
     End Sub

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you look at the link? The first part is the name of the field to be filtered on, not an incomplete form reference. Presuming the first bit returns the correct reference:

    Condition = "FieldName = " & Selection
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    I did check the link yesterday and used the 1st option like you just gave me; however the value of selection is string type. I therefore had to use option 2 on the link and it is working just as desired. Thank you so much.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Your Welcome pbaldy.

    This morning I am have a little problem with the related code. With this code instead of loading the requested report, a print is sent to the printer. Can you please assess what is wrong?
    Code:
    Private Sub List9_Click()
    selection = Me.List9.Column(2, Me.List9.ListIndex)
    Condition = "[Order ID]  = " & selection
     
    DoCmd.OpenReport "Quotation", acNormal, , Condition
    
    End Sub

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Check the available arguments for OpenReport. acNormal prints it out.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Thanks plandy. it works

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    George is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Feb 2012
    Posts
    295
    Dear All,d

    As stated before my original problem has been resolved. However a problem has developed. I did not post a new query because this problem arises out of that original problem.

    Here's the situation: Although the required records are being generated there are 2 records that are prompting an error message. What I realize that is common to these two records is that they both have apostrophes; i.e: Calypso C'bbean Resort. so I conclude that these have to be handled differently. Can someone explain how these are to be treated?
    Here is my code that works fine for non-apostrophes strings:

    CODE]
    Private Sub List2_Click()

    selection = Me.List2.Column(1, Me.List2.ListIndex)
    Selection2 = Me.List2.Column(2, Me.List2.ListIndex)
    Condition = "company = '" & selection & "'" & "AND" & "[Order ID] =" & Selection2

    DoCmd.OpenForm "Invoice Payments", acNormal, , Condition
    End Sub
    [/CODE]

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Here's a nice tutorial:

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

    Note the use of Chr(34).
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 11-29-2015, 07:24 AM
  2. Replies: 10
    Last Post: 06-18-2013, 02:00 PM
  3. Set Focus to Next ListBox Item
    By burrina in forum Forms
    Replies: 2
    Last Post: 11-10-2012, 08:25 PM
  4. Replies: 1
    Last Post: 09-10-2012, 11:21 PM
  5. Replies: 7
    Last Post: 06-05-2012, 03:22 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