[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Problem with 2nd insert line

Posted on 2013-11-15
16
Medium Priority
?
398 Views
Last Modified: 2013-11-21
Hi,
In Access 2013, I put these
insert into ite_tab (name,ite_name) values('Ite 1','');
insert into ite_tab (name,ite_name) values('Ite 2','');

in the query. But I get the error shown in the figure
t605.png
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 39650571
You'll have to execute each INSERT individually (two strings, two commands) instead of both as one string.
0
 

Expert Comment

by:Pratima_Pharande
ID: 39650572
With Access SQL you can't combine two INSERT statements. You could run each of them separately.

 But if you need to do it with a single statement, you will need to use a more complex query  like below

INSERT INTO ite_tab
SELECT 'Ite 1',''
FROM Dual
UNION ALL
SELECT 'Ite 2',''
FROM Dual;
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39650660
Sorry  Pratima, I get

"Syntax error in FROM clause" due to the following

insert into ite_tab (name,ite_name) select 'Ite 1','' FROm dual
union all select 'Ite 2','' FROm dual
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Bill Ross
ID: 39650893
Create 2 queries - one for each insert statement as this would be clearer.  You also are using the field name which is a reserved word in MS Access so surround it with [].  Lastly, no need to insert an empty string into ite_name.  Leave  it out,

Query1 SQL
insert into ite_tab ([name]) values('Ite 1');

Query2 SQL
insert into ite_tab ([name]) values('Ite 2');

If you must use just one query then:

INSERT INTO ite_tab
SELECT 'Ite 1' as [Name] from ite_tab
UNION ALL
SELECT 'Ite 2' as [Name] from ite_tab;

Regards,

Bill
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39651211
Sorry, I still get the same error - "Syntax error in FROM clause", due to this

insert into ite_tab ([name],[ite_name]) select 'Ite 1' as [name],'' as [ite_name] from ite_tab
union all select 'Ite 2' as [name],'' as [ite_name] from ite_tab
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39651280
Your syntax is still incorrect.  Try to duplicate it exactly as shown below and above.

INSERT INTO ite_tab
SELECT 'Ite 1' as [Name] from ite_tab
UNION ALL
SELECT 'Ite 2' as [Name] from ite_tab;

Regards,

Bill
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39651423
But I want to insert into 2 columns in the table.
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39651513
Hi,

Did you try the code?

Inserting empty data into a column happens automatically when you insert the record.  It is like not inserting anything at all into the column and is not necessary.  In any case if you want to insert blank data into a second column change the SQL statement to:

INSERT INTO ite_tab
SELECT 'Ite 1' as [Name], NULL as [ite_name] from ite_tab
UNION ALL
SELECT 'Ite 2' as [Name], NULL as [ite_name] from ite_tab;

This will give the same result as the prior code.

Regards,

Bill
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39653257
Many thanks all.
Sorry, I still get the same error due to this
insert into ite_tab (fld_name,ite_name) select 'Ite 1' as [fld_name],NULL as [ite_name] from ite_tab
union all select 'Ite 2' as [fld_name],NULL as [ite_name] from ite_tab

Open in new window

after having adjusted the table column name from name to fld_name.
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39653277
Hi,

I was wrong.  

You cannot combine INSERT and UNION queries in the same SQL statement.  Qlemo is correct above.

This is by definition.  

You will need to run 2 queries per my original answer above.  You could also create a VBA or macro to do the work.

Regards,

Bill
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 372 total points
ID: 39653283
If I recall correctly, you can use UNION if used that way:
insert into ite_tab (fld_name,ite_name) select * from
(select 'Ite 1' as [fld_name],NULL as [ite_name] from ite_tab
union all
select 'Ite 2' as [fld_name],NULL as [ite_name] from ite_tab) x

Open in new window

0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39653322
Hi Qlemo,

That does not work in MS Access.  It works is SQL Server but the query generator in MS Access causes a parameter failure.

Regards,

Bill
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39653376
Thanks a lot to all.
Bill,
do you have more details to use VBA to do the same thing?
0
 
LVL 14

Expert Comment

by:Bill Ross
ID: 39653391
Hi,

Sure.  Open any module or create a new one.  Enter the following:

Public Sub RunMyQueries()
  DoCmd.SetWarnings False
  DoCmd.OpenQuery "Query1"
  DoCmd.OpenQuery "Query2"
  DoCmd.SetWarnings True
End Sub

This will create a public subroutine that can be called with a button on a form or in a macro, etc.

It turns off the warning messages, runs the queries and turns the warnings back on.

You could also just add the 4 lines to the OnClick event on a form.

Regards,

Bill
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39653863
Sorry, I've got

run-time error '7873'

when trying to run this
Option Compare Database

Public Sub RunMyQueries()
  DoCmd.SetWarnings False
  DoCmd.OpenQuery "insert into ite_tab (fld_name,ite_name) select 'Ite 1' as [fld_name],NULL as [ite_name] from ite_tab"
  DoCmd.SetWarnings True
End Sub

Open in new window

0
 
LVL 14

Assisted Solution

by:Bill Ross
Bill Ross earned 128 total points
ID: 39654397
HI,

Is there some reason you do not want to save the query?  The OpenQuery command requires a saved Query.

The correct syntax if you want to run the SQL statement is:

Public Sub RunMyQueries()
  DoCmd.SetWarnings False
  DoCmd.RunSQL "insert into ite_tab (fld_name,ite_name) VALUES ('Ite 1',NULL);"
  DoCmd.SetWarnings True
End Sub
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

656 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