Solved

Indirect field reference in SQL statement:  Possible??

Posted on 2002-03-05
5
1,683 Views
Last Modified: 2008-02-26
WITHOUT using VBA and only using Access 2000, can I create a two-table query where the contents of a one field in one table can become the column name or field name in another.

TableOne has the following fields:
Field1 Text   10
Field2 Text   10
Field3 Text   10

TableTwo has one record and one field:
FieldName Text  10    <-  Holds the value "Field2"

I want to select only Field2 from table one.  I've tried the Eval() function without success:

select eval([TableTwo]![FieldName]) from TableOne

Thanks - gene
0
Comment
Question by:genehead
5 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 6842806
Don't think so, but perhaps another expert knows how to fool the access SQL interpreter...

Remains my Q: Why ?

Nic;o)
0
 
LVL 1

Expert Comment

by:S2
ID: 6842852
do you mean like this?
SELECT (SELECT tbl2.fld1 FROM tbl2) FROM tbl1;

this would give you the value of tbl2.fld1 the number of records you have in tbl1.

if you need the values of tbl1 that are in the field that has the name in tbl2.fld1 then you will have to compose it in VBA, there is no escaping it.
0
 
LVL 1

Expert Comment

by:matthewroberts
ID: 6843265
Why don't you want to code in VBA and build the sql string on the fly??
0
 
LVL 3

Accepted Solution

by:
pbryan earned 50 total points
ID: 6843559
you could do it with a couple of queries, but like nico says "why?"

--------------------

Query 1:

Union all the fields together
Select "Field1" as FieldName, Field1 as FieldValue from TableOne UNION Select "Field2" as FieldName, Field2 as FieldValue from TableOne UNION Select "Field3" as FieldName, Field3 as FieldValue from TableOne

Query2:
Join union query to TableTwo on fieldname column (this filters the columns)

Query3:
Create crosstab based on Query 2, use FieldName as column header
0
 
LVL 1

Author Comment

by:genehead
ID: 6847945
Accepting pbryan's comment as answer.  This is then kind of solution I was looking for.  I thought of UNION but didn't take it to the crosstab stage.  Good thinking pbryan.  Thanks - Gene
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 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

22 Experts available now in Live!

Get 1:1 Help Now