Results 1 to 7 of 7
  1. #1
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58

    Generate query based on fields checked in a form

    Sorry is this is redundant. I posted it under queries but did not get a response so I am trying to post this under programming...

    I want to create a query where the fields displayed are selected (check boxes?) on a form.

    For example, a users chooses the First Name, Last Name, and email fields on a form and clicking a button generates a query with those fields only from their underlying tables.




    Any help would be appreciated.
    Thanks.

  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
    I saw it yesterday but was on a mobile device. It would have to be done with VBA code. If the end result is a query, you'd need a DAO QueryDef to change the SQL of a query (or create a new one). Your code would examine each check box and add its field to the SQL if checked. A multiselect listbox would be an alternative to checkboxes, and be more dynamic.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    Thanks for the response. I like the idea of the multi-select list box. However, i have no idea what the code would look like to accomplish tying the fields to an underlying query. Can you help?

  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
    You'd be building the SQL for the query in code, not "tying fields to an underlying query". Start a string like

    strSQL = "SELECT "

    add the chosen fields to that, then

    strSQL = strSQL & " FROM TableName"

    here's how you'd step through the selected items of the listbox, though it will require adapting to your usage:

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

  5. #5
    jdashm is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2016
    Posts
    58
    Thanks for this. I will try this out.

  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
    No problem, post back if you get stuck.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Attached is a DB that I built quite a few years ago (originally an .mdb file). I have deleted all data from the file. If you go to the reports form (Search Form) and look at the logic behind the Multi-Selector Query/report you will see one way of accomplishing this task. It is VBA driven and uses the technique described by Paul above. Kudos to June7 who helped me figure this out --probably 10-12 years ago.

    Alan
    Attached Files Attached Files

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

Similar Threads

  1. Replies: 3
    Last Post: 07-26-2016, 02:12 PM
  2. Replies: 5
    Last Post: 03-25-2015, 10:31 PM
  3. Query results based on checked record.
    By jtm013 in forum Queries
    Replies: 3
    Last Post: 08-14-2014, 10:43 AM
  4. Using Multiple Fields in Form to Generate Query
    By crawfish124 in forum Forms
    Replies: 4
    Last Post: 06-04-2014, 10:39 AM
  5. Replies: 0
    Last Post: 06-03-2014, 05:15 AM

Tags for this Thread

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