• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1225
  • Last Modified:

Parameter Value prompts for Table Name

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.
0
kristibigo
Asked:
kristibigo
3 Solutions
 
COACHMAN99Commented:
What if you implement a vba solution; where you build the query string after prompting for the table name?
0
 
PatHartmanCommented:
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.
0
 
Dale FyeCommented:
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.
0
 
MikeOM_DBACommented:
In SQL*Plus you could try this:
ACCEPT Currdt PROMPT 'Enter Current Month / Year (MMYYYY):'
COL prevdt NEW_VALUE Prevdt
BREAK ON REPORT

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

Open in new window

Or compute from SYSDATE:
COL currdt NEW_VALUE Prevdt
COL prevdt NEW_VALUE Prevdt
BREAK ON REPORT

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

Open in new window


PS: Also you could code a procedure and use dynamic sql to get the results.
0
 
kristibigoAuthor Commented:
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!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now