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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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

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 48

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

724 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