Is it possible to write subqueries in a query?

A

ArielZusya

I've got three tables as follows:

tblMain
.ID_Main
.RefNum

tblMainEvent
.ID_MainEvent
.Person
.Event
.Status
.EventLog

tblRefNum
.RefNum
.BatchNum

I'd like to insert into tblMainEvent for each record in tblMain that has a
RefNum in tblRefNum with a particular BatchNum. I can write the queries
seperately as follows:

qryAllInBatch1:

SELECT tblMain.ID_Main
FROM tblTrial LEFT JOIN tblMain ON tblRefNum.RefNum = tblMain.RefNum
WHERE (((tblRefNum.BatchNum)=1));

and then:

INSERT INTO tblMainEvent ( Person, Event, Status, EventLog )
SELECT qryAllInBatch1.ID_Main, 1, 1, Now()
FROM qryAllInBatch1 LEFT JOIN tblMainEvent ON
qryAllInBatch1.ID_Main=tblMainEvent.Person
WHERE tblMainEvent.Juror IS NULL;

But I'd like to ultimately embed the SQL in VBA so I'd like to combine these
two into one longer SQL statement. Is it possible to run the second SQL
statement with the first embedded rather than using qryAllInBatch1? Thanks!
 
A

ArielZusya

Wow... twice in one day... I figured this one out on my own. I'm shocked.
No need to respond. In case others are interested:

INSERT INTO tblMainEvent ( Person, Event, Status, EventLog )
SELECT tblMain.ID_Main, 1, 1, Now()
FROM tblMain
LEFT JOIN tblMainEvent ON tblMain.ID_Main=tblMainEvent.Person
WHERE (tblMain.ID_Main
IN (SELECT tblMain.ID_Main FROM tblRefNum
LEFT JOIN tblMain ON tblRefNum.RefNum = tblMain.RefNum
WHERE (((tblRefNum.BatchNum)=1)))) AND (((tblMainEvent.Person) Is Null));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top