Solved

Trying to take use variables in Access Jet SQL, possible?

Posted on 2007-03-23
6
275 Views
Last Modified: 2008-02-01
I'm trying to select a value from a table, but I'm trying to use a value from another table to tell which column to use. So basically I'm trying to use a variable. Here's what I'm trying to do:

SELECT TABLE2.(TABLE1.COL1) FROM TABLE2

What this is saying is that the value for table1.col1 is a heading for table2. Can I do this in Access via SQL?
0
Comment
Question by:bemara57
6 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18781719
you need to code it as an IIF i believe

e.g.  Select IIF(table1.col1 = 'x',X,Y) from table2
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18781771
You can use VBA...

strSQL = "SELECT Table2." & dlookup("Col1", "TABLE1", "SomeField = SomeValue") & " FROM Table2"
                                                                                                ^^^-- criteria, if needed

The concatenated dlookup will allow for a variable field name.
This sql string can then be used in a recordsource, rowsource, etc.
0
 
LVL 39

Expert Comment

by:stevbe
ID: 18782003
you can use DLookup directly in a query, no need to use VBA.
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 39

Expert Comment

by:stevbe
ID: 18782007
how do you know which record in table1 to use?
0
 
LVL 44

Accepted Solution

by:
GRayL earned 500 total points
ID: 18783252
To clarify the answer from lowfatspread:

Select IIF(table1.col1="fld1",table2.fld1) FROM table1,table2 ON table1.ID=table2.ID;

There has to be some value (ID?) in table2 which restricts table1 to a single record.  This any clearer?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18784591
thanks GRayL

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

730 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