INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help with Parameter Query to Pass into FROM statement

Help with Parameter Query to Pass into FROM statement

(OP)
Hello everyone

I need help with a parameter query where I need to pass the table name into the FROM statement at run time. I have a number o ERP modules each in a different table so I just to get he table name inserted when I run it. The query I've tried gives an error. see my code below:

CODE

PARAMETERS [SRC] TEXT (50);
SELECT OBJID
FROM (SELECT OBJID FROM ERPTBL) AS ERPSRC
WHERE ERPTBL = [SRC]; 

The error message I receive is that ERPTBL is not recognized. Any help with this is appreciate4d.

RE: Help with Parameter Query to Pass into FROM statement

You can't parameterize the table name. You could use a little DAO code to modify the SQL property of a query as per FAQ701-7433: How to Change SQL property of saved query (DAO).

Another solution would be to create a union query based on all of your different tables with a calculated column that identifies the source table.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Help with Parameter Query to Pass into FROM statement

(OP)
Duane

Can you provide an example for the second solution? Mu tables are"
AR
AP
GL
FA
PO

My field in each table is ObjID

How would I construct the syntax for your 2nd solution?

RE: Help with Parameter Query to Pass into FROM statement


Select 'AR' As MyTable, ObjID From AR
Union
Select 'AP', ObjID From AP
Union
Select 'GL', ObjID From GL
Union
Select 'FA', ObjID From FA
Union
Select 'PO', ObjID From PO

You may - if you want - include other fields from your tables.

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Help with Parameter Query to Pass into FROM statement

The only small modification of Andy's SQL would be to use "Union All" rather than just "Union". I think it might perform a little better.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Help with Parameter Query to Pass into FROM statement

Yes, "Union All [...] might perform a little better. " (since the db engine does not have to eliminate duplicates), but the outcome (data) should be the same. smile (since there will NOT be any duplicates)

Have fun.

---- Andy

There is a great need for a sarcasm font.

RE: Help with Parameter Query to Pass into FROM statement

Andy,
Correct but there might have been duplicate ObjID values within a table.

The final SQL might look like:

CODE --> SAL

SELECT ObjID 
FROM quniModules
WHERE MyTable = "AR"; 

Or you could use some type of parameter (hopefully a control on a form) for the MyTable value.

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Help with Parameter Query to Pass into FROM statement

(OP)
UNION ALL Worked perfectly. Many thanks!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close