Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Parameter Value prompts for Table Name

Posted on 2014-04-23
Medium Priority
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 39

Accepted Solution

PatHartman earned 668 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
Dale Fye earned 664 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 668 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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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