Subquery inside an Access VBA DoCmd - sql

I am trying to run the following in Access / VBA
DoCmd.RunSQL SQLStatement:="INSERT INTO [Assets All] (RoomID) VALUES (SELECT RoomID from findNewRoomID )"
findNewRoomID is a Query and is part of the Form's Record Source, it only returns one result.
The error message says there is a syntax error in the subquery.
I have also tried with the SQL code of findNewRoomID but it didn't work either.

When using the VALUES clause of an INSERT INTO statement, a comma-delimited list of values should be supplied, and this can only be used to insert a single record into the target table.
If instead you wish to pass append the results of a SELECT query which may return multiple records, you would simply use:
DoCmd.RunSQL "INSERT INTO [Assets All] (RoomID) SELECT RoomID from findNewRoomID"

Related

How use SELECT in append query in Access SQL?

In MS Access, I created a query by create Menu-> Query Design (with name Query3).
I want use it in an SQL command in another query but when I run it got this error:
Syntax error on query expression 'select f1'
SQL command
INSERT INTO boors (boors.Nemad, boors.Volumn, boors.Price,
boors.LastPrice, boors.LastPerc, boors.LastPr,
boors.LastPer, boors.MinPrice, boors.MaxPrice,
boors.distance, boors.inout, boors.Power)
values (select f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12 FROM Query3)
It appears that you are mixing the SQL used for inserting values and inserting from a table/query. As you are doing the latter, your SQL should look like:
INSERT INTO boors (Nemad, Volumn)
SELECT F1, F2
FROM Query3
Regards,

Unknown field name error. Can't figure out SQL statement

I'm sure it's something small, but I can't figure out why this SQL statement isn't working.
INSERT INTO tempTable
SELECT *
FROM
(
SELECT * FROM [table] AS x RIGHT JOIN
(
SELECT DISTINCT a.[Part number], Count(a.[Part number]) AS [Part Count]
FROM [table] AS a
GROUP BY a.[Part number]
)
AS y
ON x.[Part number] = y.[Part number]
);
Every time I run the query in access, I get the error "The INSERT INTO statement contains the following unknown field name: 'Part number'. Make sure you have typed the name correctly, and try the operation again.
When I take out the INSERT INTO wrapping and use just the select statement, it returns the results I want to insert into tempTable.
tempTable was created using this select statement, so I don't think it's an issue of mismatching column names.
Can anybody see an error with my statement?
Are you saying that you created the temp table by using the exact same select query in a Make Table query? Have you looked at tempTable in design view and confirmed the field names? Without seeing all of your fields, my guess is that your select statement outputs multiple fields with the same name.
Additionally, note that 'table' is a reserved word and should not be used as an object name, and the use of spaces in field names is considered poor practice. Just because you CAN do something, doesn't mean that you SHOULD.

Insert statement in sql with one field set as autonumber

I am having three columns in my table branch i.e id(Autonumber),code(text),desc(text).I am trying to execute this sql
insert into branch(code,desc) values('"+b+"','"+c+"')";
which gives me error syntax error..please help
One of your columns has name DESC, which is Reserved Keyword. In order to peoperly execute the INSERT statement, you need to delimite the column by using brackets eg
insert into branch(code,[desc]) values ('"+b+"','"+c+"')";
MSACCESS Reserved Keywords List
One more thing, your code is prone to SQL Injection. Please do parameterized the query.

Writing to multiple columns simulatenously with SQL

I am having trouble writing a VBA macro within Microsoft Access. What I am trying to do is use SQL to create an output table, but I want to write to multiple columns simultaneously.
This gets me all the values I need for one column:
Docmd.RunSQL “INSERT INTO Output (TargetCol1) SELECT [Field1] FROM [Table1] WHERE [Criteria1] = ‘Value’ GROUP BY Field1”
When I try to run this multiple times to get the values I need for other columns. INSERT INTO writes the data as new records, so I end up with blank spaces, like this:
Field1----Field2
Value----<Null>
Value----<Null>
Value----<Null>
<Null>----Value
<Null>----Value
What I want is:
Field1----Field2
Value---- Value
Value---- Value
Value----<Null>
I tried to create variables and create kind of a nested statement but I receive a ‘Compile error, object required’ on my first line when I try to run what I have written:
Set x = Docmd.RunSQL “INSERT INTO Output (TargetCol1) SELECT [Field1] FROM [Table1] WHERE [Criteria1] = ‘Value’ GROUP BY Field1”
Set y = Docmd.RunSQL “INSERT INTO Output (TargetCol2) SELECT [Field2] FROM [Table1] WHERE [Criteria2] = ‘Value’ GROUP BY Field2”
Docmd.runsql “INSERT INTO Output (TargetCol1, TargetCol2) Values (x,y)”
Why not:
INSERT INTO Output (TargetCol1,TargetCol2) SELECT [Field1,Field2] FROM [Table1] [Criteria1] = 'Value'"
Set is used for objects, and you do not have one in Set x = Docmd.RunSQL. Order by is irrelevant for a table.
The easiest way to set up queries is using the query design window. it will guide you through creating the query and you can then switch to SQL view to get SQL.
I suggest you do not use RunSQL : What's the difference between DoCmd.SetWarnings and CurrentDB.Execute

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.
Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId