Results 1 to 5 of 5
  1. #1
    celiowin is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jul 2011
    Posts
    6

    Pass List selected item as a Field Name in Select Query

    Hi,
    I have a listbox containing field names and i would like to pass the selected item to the SELECT QUERY > Field.
    But when i hit the query button on the form it returns the result containing the selected field name from the listbox instead of the data in that column.



    My SQL View looks like
    SELECT Forms!subform!mylist AS Expr1 FROM myTable

    Am i passing the parameter from the list to the field as a wrong data type?

  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
    To my knowledge you can't do that. It did what I would have expected. The only way I know of to do what you're trying to do is build the SQL in VBA and either use it there or use a DAO QueryDef to change the SQL of a saved query.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    celiowin is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jul 2011
    Posts
    6
    So the DAO QueryDef will overwrite my the setup in SELECT QUERY in access?
    Then i'll need to switch from ADO to DAO

  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
    Depends on what you want the SQL for; some things I would just use the SQL in code, others I'd set a query to the resulting SQL.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You can't do it directly but depending on what you're doing you can construct a SQL statement and process the contents

    For instance let's say you have a form called frmTest
    on that form you have a field TestField

    You have a table named tblTestData
    in that table you have the fields
    DATAID (autonumber)
    DATATEXT (text)
    DATANUMBER (number)

    if you wanted to construct a sql statement on a button click (for instance) you could have

    dim ssql as string

    ssql = "SELECT " & forms!frmtest!testfield & " FROM tblTestData"

    then if you were to process this SQL string as a recordset you could do what you wanted, I haven't ever done it but you might be able to save the SQL string as an actual database object.

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

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2011, 09:30 PM
  2. Replies: 1
    Last Post: 11-23-2010, 01:30 PM
  3. Can't alays select an item from a list box
    By bomber in forum Access
    Replies: 0
    Last Post: 10-16-2009, 10:01 AM
  4. Replies: 8
    Last Post: 09-24-2009, 02:56 PM
  5. Replies: 0
    Last Post: 08-26-2009, 11:51 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