Trying to run multiple SELECT
statement inside a VALUES
statement of INSERT INTO
. SELECT
gets me the required record from the other tables but gives me "Query input must contain at least one table or query" error.
I did some research and found that I can use either use SELECT
or VALUES
with the INSERT INTO
statement. I am looking to use SELECT subquery inside the VALUES statement because I have multiple fields in my table that takes in values from different tables and have conditions just like below in my code where it looks for an ID. Below is a part of the query that I wrote that I have limited to only 3 fields. Actual table has 9 fields. I am testing it out and trying to keep it simple here.
extPMBActID_SQL = "SELECT * FROM PMB;"
Set rsDB = CurrentDb.OpenRecordset(extPMBActID_SQL, dbOpenDynaset)
instRptGen_SQL = "INSERT INTO testTable (ID, arbName, testName) VALUES ('" & _
Left(rsDB.Fields("ActivityID"), 2) & "', '" & rsDB.Fields("ActivityName") & _
"', (SELECT AreaName FROM Area WHERE AreaID = " & _
Left(rsDB.Fields("ActivityID"), 2) & "))"
db.Execute instRptGen_SQL, dbFailOnError
The first parameter in the VALUES statement fetches the correct record and also the one inside the SELECT subquery. I am not sure why it is not finding the table.
I looked around on how to use multiple SELECT inside VALUES but found nothing and having this subquery can help me reduce a major chunk of code into just a line and also save me from using tons of variables.