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
16 105751
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)
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 :(
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 " ' "
NeoPa 32,534
Expert Mod 16PB
Unfortunately the code generated syntax error :(
Try this slightly fixed version : - 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).
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
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.
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?
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?
- DoCmd.SetWarnings(False)
-
and after all finished
-
DoCmd.SetWarnings(True)
Can I take it then that Nico's code worked fine for you?
- DoCmd.SetWarnings(False)
-
and after all finished
-
DoCmd.SetWarnings(True)
Can I take it then that Nico's code worked fine for you?
unfortunately, it didn't
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?
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)
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
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?
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
PEB 1,418
Expert 1GB
Ah it is finished here! OK!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
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...
| |