473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Insert Into query using VBA

36
Can someone please tell me why INSERT INTO generates syntax error

Private Sub Command7_Click()
Dim strUSER As String
strUSER = Nz(DLookup("[user]", "table2", "[user] ='" & username.Value & "'"), "nouser")
If strUSER = "nouser" Then
INSERT INTO table2;
VALUES (username.Value,password.Value);
MsgBox "User added"
Else
MsgBox "user already exist"
End If
Dec 3 '06 #1
16 105888
nico5038
3,080 Expert 2GB
Try to execute the query like:

If strUSER = "nouser" Then
currentdb.execute ("INSERT INTO table2 (username, password) VALUES ('" & me.Username & "','" & me.txtPassword & "') ;
MsgBox "User added"
else

VBA isn't capable of handling SQL syntax. The query will need to be constructed and then executed e.g. with docmd.runSQL or the above currentdb.execute.
The INSERT statement needs some study I see, firt try them with the grapical query editor and then move the SQLtext for stringing to your code.

Nic;o)
Dec 3 '06 #2
omozali
36
Try to execute the query like:

If strUSER = "nouser" Then
currentdb.execute ("INSERT INTO table2 (username, password) VALUES ('" & me.Username & "','" & me.txtPassword & "') ;
MsgBox "User added"
else

VBA isn't capable of handling SQL syntax. The query will need to be constructed and then executed e.g. with docmd.runSQL or the above currentdb.execute.
The INSERT statement needs some study I see, firt try them with the grapical query editor and then move the SQLtext for stringing to your code.

Nic;o)
Unfortunately the code generated syntax error :(
Dec 3 '06 #3
omozali
36
Unfortunately the code generated syntax error :(

I tried this

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES (username.value,password.value)")

generates: too few parameters, expect 2

I also tried
CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ('username.value','password.value')")

Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

3rd attempt

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ("'" & username.value & "'","'" & password.value & "'")")

would generate error at the first " ' "
Dec 3 '06 #4
NeoPa
32,556 Expert Mod 16PB
Unfortunately the code generated syntax error :(
Try this slightly fixed version :
Expand|Select|Wrap|Line Numbers
  1. currentdb.execute ("INSERT INTO table2 (username, password) VALUES ('" & me.Username & "','" & me.txtPassword & "') ;"
It's exactly the same except Nico left off the last double-quote (easily done).
Dec 3 '06 #5
omozali
36
I tried this

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES (username.value,password.value)")

generates: too few parameters, expect 2

I also tried
CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ('username.value','password.value')")

Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

3rd attempt

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ("'" & username.value & "'","'" & password.value & "'")")

would generate error at the first " ' "
an update for those interested , this code works

Private Sub Command7_Click()
Dim strUSER As String
Dim UN As String
Dim PW As String
UN = username.Value
PW = password.Value
strUSER = Nz(DLookup("[user]", "table2", "[user] ='" & username.Value & "'"), "nouser")
If strUSER = "nouser" Then
Rem CurrentDb.Execute ("INSERT INTO table2 ([user], [pass])" & _
"VALUES (UN,PW)")
DoCmd.RunSQL ("INSERT INTO table2 ([user], [pass]) VALUES (username.value,password.value)")

No idea why the previous one didn't


ACCESS VB is very tricky language to learn and it make no sense to me why microsoft made so many variations for commands that would only confuse us
Dec 3 '06 #6
NeoPa
32,556 Expert Mod 16PB
I tried this

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES (username.value,password.value)")

generates: too few parameters, expect 2

I also tried
CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ('username.value','password.value')")

Things get added to the table but instead the value of username I actualy get the text username.value in the table!!!

3rd attempt

CurrentDb.Execute ("INSERT INTO table2 ([user], [pass]) VALUES ("'" & username.value & "'","'" & password.value & "'")")

would generate error at the first " ' "
Let us know if Nico's (fixed) version works first.
If not then we can revisit it, but I suspect all will be fine.
Dec 3 '06 #7
omozali
36
one small thing when the code run a small window would appear saying
you are about to a append 1 row and asks for confirmation

any way to remove this annoying window?
Dec 3 '06 #8
NeoPa
32,556 Expert Mod 16PB
one small thing when the code run a small window would appear saying
you are about to a append 1 row and asks for confirmation

any way to remove this annoying window?
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings(False)
  2. and after all finished
  3. DoCmd.SetWarnings(True)
Can I take it then that Nico's code worked fine for you?
Dec 3 '06 #9
omozali
36
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SetWarnings(False)
  2. and after all finished
  3. DoCmd.SetWarnings(True)
Can I take it then that Nico's code worked fine for you?

unfortunately, it didn't
Dec 4 '06 #10
NeoPa
32,556 Expert Mod 16PB
unfortunately, it didn't
In that case - are you happy that you have a working answer?
Or are you looking for more help sorting that out?
Dec 4 '06 #11
nico5038
3,080 Expert 2GB
Please start with creating the INSERT query in the query editor till it's working.
Than start with transforming that SQL into the string needed.

Nic;o)
Dec 4 '06 #12
omozali
36
In that case - are you happy that you have a working answer?
Or are you looking for more help sorting that out?

Sorry for the late responce. my situation is ok now thanks
Dec 16 '06 #13
NeoPa
32,556 Expert Mod 16PB
Sorry for the late responce. my situation is ok now thanks
Do you still have the warning come up (We can help to remove the warning)?
Or are you happy with everything?
Dec 17 '06 #14
PEB
1,418 Expert 1GB
Can someone please tell me why INSERT INTO generates syntax error

INSERT INTO table2;
VALUES (username.Value,password.Value);
MsgBox "User added"
End If
Can't use SQL just like this in VBA! Use docmd.runsql YourSQL
Dec 17 '06 #15
PEB
1,418 Expert 1GB
Ah it is finished here! OK!
Dec 17 '06 #16
NeoPa
32,556 Expert Mod 16PB
I thought I'd add a link to an article (How to Debug SQL String) that may help the many people who are still viewing this thread many years later.

As Nico indicated many years ago, if you approach the problem step-by-step in the appropriate order then it is so much easier to find and fix problems.
Oct 23 '21 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: avinash | last post by:
hello myself avinash i am developing on application having vb 6 as front end and sql server 7 as back end. when i use insert query to insert data in table then the date value of that query is...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
5
by: Annie | last post by:
hello guys, I have little experience working with C# and MS Access ... I am having an insert query with one datetime field and a boolean and couple of text and number fields as below: ...
0
by: Hrvoje Vrbanc | last post by:
Hello all! I'm new to ASP.NET 2.0 and I tried connecting and inserting to a SQL database by using SqlDataSource control. I build an INSERT query using the query builder tool but I'm puzzled...
0
by: clickon | last post by:
I want to use an INSERT Query as the value for the UpdateCommand property of an SQL datasource, anyone know if this will actually work ? The reason i want to do it is bacause i am taking values...
1
by: =?Utf-8?B?RGV2YW4=?= | last post by:
Hi, I have a table adapter for a database with an insert query. I want to be able to get the PRIMARY key of the inserted record for that insert statement. I know that SCOPE_IDENTITY is the...
3
by: star111792 | last post by:
hi, i have just started learning ASP. i m doing database connectivity in ASP. my problem is with the insert query. i have a page "form.asp" for taking input from user. after entering data...
1
by: Luqman | last post by:
I have created a Insert Query in Sql Data Source using Oracle Database, with the parameters, and its connected with DetailView Control. When I try to Insert through DetailView Control, Illegal...
2
bard777
by: bard777 | last post by:
I have a form kicking off an INSERT query. I need to check another table for a matching value and use a field from that table as the value for one of the fields in my insert. Table 1 (insert from...
1
by: Marco van der M | last post by:
Hello everyone, I am trying to make an insert query that inserts information using form values and a query that gets the id from the name of a field in the form. I got a form that shows object...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.