Variable Table Name For A Query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BulbFresh
    New Member
    • Feb 2008
    • 10

    Variable Table Name For A Query

    Hi,

    I'm a novice Access user and have a simple question but can't seem to find the answer.
    I have tables imported and named by the user on import e.g. January.
    I then need to run a query on the table that the user selects (same query and each table is in the same format so the fields are known).
    I have a combo box on the form for the user to select the table on which to perform the query. It's just the table name which is the variable i need to insert into my query.

    My SQL for this looks like:
    Code:
    SELECT field1, field2
    FROM Forms!Form1!Combo1
    I've tried enclosing with ( ) , [ ] , " " , ' ' , & & but keep getting errors.
    Syntax error in FROM clause
    or
    Cannot find the input table / query 'Forms!Form1!Co mbo1' make sure it exists.

    I'm sure someone will know the answer...
    Thank you
  • Scott Price
    Recognized Expert Top Contributor
    • Jul 2007
    • 1384

    #2
    Is this in a standard query, or written into VBA code?

    In VBA code the syntax would be: "Select [Field1], [Field2] From " & Forms!Form1.Com bo1 & ";"

    However, you need to make sure that the combo box in question has as it's bound column the actual name of the table you wish to select from. If the combo box is bound to an invisible column containing a number value, your select statement will be looking for a table with the 'name' of that number value.

    Regards,
    Scott

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32645

      #3
      As Scott says, it's hard to know what to suggest if you don't say how you're expecting to run the query code you're referring to.
      What VBA code are you using at the moment?

      Comment

      Working...