Results 1 to 7 of 7
  1. #1
    dux4all is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    2

    Using a field list combo box as query criteria?


    I would like to make a flexible query where users would select the desired criteria field from a field list combo box in a form and then pick the criteria from another combo box. My question is how do you use the field list output for a query criteria? Any suggestions would be appreciated.

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    To the best of my knowledge, you can't do this type of thing without VBA. Presuming it's for a form or report, I'd use this technique:

    BaldyWeb wherecondition

    First building a string using your combos:

    strCriteria = Me.FirstComboName & " = " & Me.SecondComboName

    and using that string in the wherecondition argument.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I should add that it gets more complicated if your fields are of varying data types. As you can see in that link, different data types require different delimiters.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Join Date
    May 2010
    Posts
    339
    Could you use the combo box after update event to trigger a query, and pass the criteria?

    Code:
    Private Sub Combo0_AfterUpdate()
         DoCmd.OpenQuery "SomeQuery"
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Access_Blaster View Post
    Could you use the combo box after update event to trigger a query, and pass the criteria?

    Code:
    Private Sub Combo0_AfterUpdate()
         DoCmd.OpenQuery "SomeQuery"
    End Sub
    I don't see how that accomplishes the goal in the OP, but perhaps you can post a sample db demonstrating what you mean?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    dux4all is offline Novice
    Windows XP Access 2007
    Join Date
    Jun 2010
    Posts
    2

    better idea of the problem??

    Thanks for all the suggestions. I'll try to better explain my needs. I do believe this will require VBA. I have created a sample table below:
    Code:
    ID   Color   Shape   Size
     1   red   circle   small       
    2   green   square   small       
    3   red   rectangle   medium       
    4   yellow   square   large
    I would like to create a form using a field list combo box to have the user select Color, Shape, or Size as the criteria field from the combo box(1). Say the user selects Color from the field list combo box another combo box(2) would list red, green, yellow. These two combo boxes would make up the WHERE statement of my query: WHERE combo box(1)=combo box(2). The field list combo box is easy. I just don't know how to get that information from the form into the query. My real table has dozens of fields and that is why I don't want to create/maintain several "static" queries, when a "flexible" one sounds like a good idea.

    Thanks again!

  7. #7
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I understood the request, so I'll stand by my first response. You could also use a DAO QueryDef to change the SQL of your query. Given the number of options and presumably varying data types, neither solution will be trivial. Another option if you want to give the user the option to filter on multiple fields is demonstrated in the sample db here:

    http://www.baldyweb.com/BuildSQL.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. Combo Box - Field List Values
    By jennyaccord in forum Forms
    Replies: 5
    Last Post: 07-29-2011, 01:49 PM
  2. Replies: 1
    Last Post: 05-05-2010, 01:54 AM
  3. Replies: 1
    Last Post: 03-22-2010, 03:37 PM
  4. Query multiple field & criteria
    By fua in forum Access
    Replies: 2
    Last Post: 11-04-2009, 08:22 PM
  5. query - criteria based on another row field value
    By techexpressinc in forum Queries
    Replies: 1
    Last Post: 05-29-2009, 04:17 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