Parameter Value prompts for Table Name

Posted on 2014-04-23
Last Modified: 2014-04-23
There may be an answer already for this question, if so, please direct me to that posting as I was having difficulty to know what key words to search for:

My question is, is it possible to create a query where the parameter calls for a specific table (not a field)?  I know it can be used to call a specific date or text parameter from a table, but I was wanting to actually call a specific table to run the query.

Since I update "[Current Month/Year]" table to a table I've named "[New]" and "[Previous Month/Year]" table to a table I've named "[Old]", to ensure I've copied the correct information into each table, I compare the tables via a query (e.g., do the sums of the tables match?).  Each month the "[Current Month/Year]" table or "[Previous Month/Year]" table names change.

When I run the comparison query, is there a way to call the "[Current Month/Year]" table or "[Previous Month/Year]" table instead of having to modify the comparison query each month? Or is this something that would need to be created in VBA?  If so, so you have some tips in how to write it.  I'm not proficient in VBA.

Thank you.
Question by:kristibigo

Expert Comment

ID: 40018585
What if you implement a vba solution; where you build the query string after prompting for the table name?
LVL 35

Accepted Solution

PatHartman earned 167 total points
ID: 40018629
It is not possible to change the structure of a querydef on the fly.  Querydefs are "compiled" objects.  The first time they are run, Access calculates an execution plan to specify how it will accomplish retrieving the requested data and saves the plan.  Changing the table would change the plan so that is simply not allowed.  It isn't allowed in views or stored procedures in SQL Server either.  Nor can you change the compiled code in an executable program.

To solve your problem, you would need to build the SQL string at run time using VBA.  There is nothing to equivocate.  There is no other option.  You MUST build the queries with VBA unless you want to create them manually every month (which it sounds like you've gotten tired of doing).  That's job security but most people would not want to have to do that.  To get the table names, you would either have the user choose the tables using controls on a form or you would choose them using program logic.  Then build the string and run it.

You probably don't want to hear this but your approach to Access is wrong.  Access is not a collection of spreadsheets.  It is a relational database.  In a relational database, we don't add tables/columns on the fly.  We add ROWS.  If you go to a "single" table solution, you would then use queries to choose sets of data from the single table.  Access treats tables and Select queries as interchangeable so pretty much anything you can do with a table, you can do with a select query and vice versa.  The way you are working, you are in a constant state of building new tables and queries.  It is at least as much work as using spreadsheets would be, possibly more since Access isn't intended to be used this way.

If you want help to develop the schema that will support your processing requirements, please start a new thread and we will be happy to help you.
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 166 total points
ID: 40018636
Not by getting the input from an inputbox, which is what pops up when you do a parameter query.  But you could create a form, and enter the table names in textboxes or select from combo boxes on the form.  You would then use a command button that would build the SQL String in the click event of the command button.

Having said that, if you need to do this, you probably need to rethink your data structure.  You should not need tables for [Current Month/Year] and [Previous Month/Year],  all of that data should be in a single table, and you should create queries that identify the Current Month/Year and Previous Month/Year records, to do your comparison.
LVL 29

Assisted Solution

MikeOM_DBA earned 167 total points
ID: 40018645
In SQL*Plus you could try this:
ACCEPT Currdt PROMPT 'Enter Current Month / Year (MMYYYY):'
COL prevdt NEW_VALUE Prevdt

SELECT TO_CHAR ( ADD_MONTHS ( TO_DATE ( '&&currdt', 'MMYYYY'), -1), 'MMYYYY') Prevdt
SELECT * FROM Current_Month_&&currdt
SELECT * FROM Previous_Month_&&prevdt;

Open in new window

Or compute from SYSDATE:
COL currdt NEW_VALUE Prevdt
COL prevdt NEW_VALUE Prevdt

     , TO_CHAR ( ADD_MONTHS ( SYSDATE, -2), 'MMYYYY') Prevdt
SELECT * FROM Current_Month_&&currdt
SELECT * FROM Previous_Month_&&prevdt;

Open in new window

PS: Also you could code a procedure and use dynamic sql to get the results.

Author Closing Comment

ID: 40018714
Thank you for your input and advice.  Yes, I understand that I'm not using Access as it is intended. However, I have to tie the database to other tables, and in order to ensure the information has been updated and checked properly, I have to compare what was to what is in order to see the differences.  From there I verify if the changes should have taken place.

You've answered my question that there is not the option create a parameter to call a specific table name.  

I will also attempt to use the example SQL above to see if it will help the process.  My purpose:  to eliminate as much human error as possible.  (Meaning, editing the query each month has given incorrect results due to the tables not being correctly updated manually). Thank you everyone!

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question