Results 1 to 4 of 4
  1. #1
    Bedsingar is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jul 2011
    Posts
    12

    Define Table with a Variable

    Hello,

    I am trying to understand how I'd go about designing one query (Even if I have to use SQL to do it) that chooses its table based on a variable.

    So:

    I have 12 tables named F01,F02, F03 etc



    and want to preferably have a configuration page (Form Possibly?!) that has a combo box with all the table names in, from which you can set which table the query should use.

    How do I also set it so that the variable selected is stored for future use until updated again via the form?

    So I expect the query code will look something like (Keeping it simple)

    Code:
    SELECT DISTINCT VARIABLEHERE.ProjectNumber, Max(VARIABLEHERE.[Accounting Date]) AS [MaxOfAccounting Date] 
    FROM VARIABLEHERE
    GROUP BY VARIABLEHERE.ProjectNumber;
    Thanks

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    if the structure of all of your tables is the same why is the data in that many different tables? Can you not combine all the data and simply add an additional field saying with an indication of the source of the data? This would make what you're trying to do you would simply change the criteria of a query rather than having to build an entirely new query every time.

  3. #3
    Bedsingar is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jul 2011
    Posts
    12
    Could do, but as the source data comes from 12 sepperate tables I wanted to keep the data sepperate.

    Instead I have created a union query & linked a form to that, so workarround achieved.

    Thanks

    Josh

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I thought your original question was to build a SQL statement from scratch based on some selected criteria. This would be far easier, assuming all your tables have the same structure, if they were in one table. So you created a union query to include all data then built your SQL statement on that?

    This solution certainly works and is in the spirit of what I suggested but as your database grows I think you'll find that union queries can be extremely inefficient and you should consider moving all the data to one table, again assuming the table structure of all of them is the same or very nearly the same.

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

Similar Threads

  1. Where to define VBA Functions?
    By Poyan in forum Access
    Replies: 6
    Last Post: 06-24-2011, 05:20 PM
  2. select statement with variable table name
    By dv89k in forum Queries
    Replies: 1
    Last Post: 05-26-2011, 10:54 PM
  3. SELECT INTO variable table name
    By Ian P in forum Queries
    Replies: 2
    Last Post: 05-29-2010, 12:49 AM
  4. Assign and call variable from table
    By smikkelsen in forum Access
    Replies: 7
    Last Post: 04-01-2010, 09:38 AM
  5. Replies: 3
    Last Post: 02-16-2010, 10:43 PM

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