Solved

Parameter Value prompts for Table Name

Posted on 2014-04-23
5
973 Views
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.
0
Comment
Question by:kristibigo
5 Comments
 
LVL 7

Expert Comment

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

Accepted Solution

by:
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.
0
 
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.
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
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
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
 

Author Closing Comment

by:kristibigo
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!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now