472,805 Members | 1,040 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 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 105751
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,534 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,534 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,534 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,534 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,534 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,534 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...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.