Results 1 to 5 of 5
  1. #1
    Noobie89 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    3

    Create query using combo box parameter as the chosen field

    Hi everyone,
    I looked all over internet to understand how to use combo box parameter as the chosen field in the upcoming query creation.

    Sub CreateQuery()



    Dim db As DAO.Database
    Dim qDef As DAO.QueryDef

    Set db = CurrentDb
    Set qDef = db.CreateQueryDef("MyQuery")
    qDef.SQL = "SELECT * FROM "
    qDef.Close
    db.Close
    Set qDef = Nothing
    Set db = Nothing
    Application.RefreshDatabaseWindow

    End Sub

    How can I use a combo box parameter after SELECT or FROM in SQL SYNTAX?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,523
    You can either put the text boxes in the SQL:
    qdef.sql = "select * from table where [field]='" & me.txtBox & "'"
    qdef.close
    Docmd.openQuery qdef.name

    or

    set rst= qdef.openrecordset

    or use param in qdef:

    Qdef.parameters(0) = me.txtBox
    ser rst= qdef.openrecordset

    or

    just open the query, no code:
    docmd.openQuery "qsQuery"

  3. #3
    Noobie89 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    3
    But this would only work as a criteria. I have a combo box of all the tables and another of all the fields dependent of which table is chosen in the first combo box. I want to use these two parameters to create a query. Something like this:
    qDef.SQL = "SELECT x FROM y "
    x =
    [Forms]![Form1]![Combo5].[Value]
    y = [Forms]![Form1]![Combo7].[Value]

  4. #4
    Noobie89 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    3
    Found it!
    Sub CreateQuery()

    Dim db As DAO.Database
    Dim qDef As DAO.QueryDef

    Set db = CurrentDb
    Set qDef = db.CreateQueryDef("MyQuery")
    qDef.SQL = "SELECT * FROM " + Me.Combo2.Text
    qDef.Close
    db.Close
    Set qDef = Nothing
    Set db = Nothing
    Application.RefreshDatabaseWindow

    End Sub

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,910
    I would be using concatenation with &

    Code:
    "SELECT " & [Forms]![Form1]![Combo5] & " FROM " & [Forms]![Form1]![Combo7]
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 5
    Last Post: 10-06-2015, 06:37 PM
  2. Replies: 1
    Last Post: 07-21-2015, 03:38 PM
  3. Populate combo box based on value chosen in another combo box
    By Gary Childress in forum Database Design
    Replies: 3
    Last Post: 01-12-2013, 09:44 PM
  4. Replies: 2
    Last Post: 08-17-2012, 09:28 AM
  5. Replies: 1
    Last Post: 02-23-2012, 09:00 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