•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 428,570 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,629 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 6673 | Replies: 7
![]() |
•
•
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation:
Rep Power: 3
Solved Threads: 9
Hi community,
I have the weirdest error happening in my VB6 program when I try to execute an "INSERT INTO" query. Here are the premises:
- the database is Access 2003 this will be migrated to SQL server later.
- conn has been instantiated and opened correctly, I am able to select from several tables in the database, tblUsers among them
Here is the problem, I have a form used to create users and the following code is in the OK button on that form. BTW, I do NOT have all this in the real code, just one option at a time but I pasted them here so you can see what I have tried so far.
If anyone out there has a solution to the problem, or pointers to why this weird error comes up, and would not mind sharing I would really appreciate it. This error give me headaches...
Thanks
Yomet
I have the weirdest error happening in my VB6 program when I try to execute an "INSERT INTO" query. Here are the premises:
VB Syntax (Toggle Plain Text)
Public conn as ADODB.Connection Public rs1 as ADODB.Recordset
- the database is Access 2003 this will be migrated to SQL server later.
- conn has been instantiated and opened correctly, I am able to select from several tables in the database, tblUsers among them
Here is the problem, I have a form used to create users and the following code is in the OK button on that form. BTW, I do NOT have all this in the real code, just one option at a time but I pasted them here so you can see what I have tried so far.
vb Syntax (Toggle Plain Text)
Private Sub cmdOK_Click() Dim UserID As Long Dim SQL As String Dim cmd As ADODB.Command Set cmd = New ADODB.Command cmd.ActiveConnection = conn '1st try - Execute method of the Connection object ' Gives "Sytax error in INSERT INTO statement" conn.Execute "INSERT INTO tblUsers (UserName, Password) " & _ "VALUES ('" & txtUserName & "', " & Chr(34) & txtPassword & Chr(34) & ")" '2nd try - using a string variable instead of straight text ' Gives "Sytax error in INSERT INTO statement" SQL = "INSERT INTO tblUsers (UserName, Password, Active, Comments) VALUES ('" & txtUserName & "', '" & txtPassword & "', -1, '');" conn.Execute SQL, , adExecuteNoRecords '3rd try - using the command object ' Gives "Sytax error in INSERT INTO statement" SQL = "INSERT INTO tblUsers (UserName, Password, Active, Comments) VALUES ('" & txtUserName & "', '" & txtPassword & "', -1, '');" Set cmd = New ADODB.Command cmd.ActiveConnection = conn cmd.CommandText = SQL cmd.CommandType = adCmdText cmd.Execute '4th try - using a query in the access database ' Gives "Too few parameters, 2 expected" cmd.CommandText = "qryInsertUser" 'This query exists in the database cmd.CommandType = adCmdStoredProc cmd.CreateParameter "UserName", adVarChar, adParamInput, , txtUserName cmd.CreateParameter "Password", adVarChar, adParamInput, , txtPassword cmd.NamedParameters = True cmd.Execute '5th try - a colleague inerted records using this circumvent way of running the query ' Gives "Sytax error in INSERT INTO statement" SQL = "INSERT INTO tblUsers (UserName, Password) VALUES ('" & txtUserName & "', '" & txtPassword & "');" rs1.Open SQL, conn, adOpenForwardOnly, adLockReadOnly 'This works perfectly rs1.Open "SELECT * FROM tblUsers WHERE UserName = '" & txtUserName & "'", conn, adOpenDynamic UserID = rs1!UserID rs1.Close rs1.Open "SELECT * FROM tblGroups ORDER BY GroupName", conn, adOpenDynamic rs1.MoveFirst rs1.Find "GroupName = 'Users'" conn.Execute "INSERT INTO tblUserGroups (UserID, GroupID) VALUES (" & UserID & ", " & rs1!GroupID & ")" End Sub
If anyone out there has a solution to the problem, or pointers to why this weird error comes up, and would not mind sharing I would really appreciate it. This error give me headaches...
Thanks
Yomet
•
•
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation:
Rep Power: 3
Solved Threads: 9
One more thing, I tried the same INSERT statement but without the variables, i.e.
Just to make sure that the problem was not in the variables, and got the same Syntax Error.
Thx
Yomet
vb Syntax (Toggle Plain Text)
conn.execute "INSERT INTO tblUsers (UserName, Password) VALUES ('Hello', 'World');", conn
Just to make sure that the problem was not in the variables, and got the same Syntax Error.
Thx
Yomet
Last edited by Yomet : May 11th, 2007 at 2:18 pm.
•
•
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation:
Rep Power: 3
Solved Threads: 9
Well,
I found one workaround using the parametrized query stored in the database, here it is
However, this is cumbersome and I would really try to find out where the Syntax error is coming from.
Thx
Yomet
I found one workaround using the parametrized query stored in the database, here it is
vb Syntax (Toggle Plain Text)
'6th try - using a query in the access database Dim arr(1) cmd.CommandText = "qryInsertUser" 'This query exists in the database cmd.CommandType = adCmdStoredProc arr(0) = "sdfasdr" arr(1) = "kihjui" cmd.Execute UserID, arr()
However, this is cumbersome and I would really try to find out where the Syntax error is coming from.
Thx
Yomet
•
•
Join Date: Mar 2007
Posts: 59
Reputation:
Rep Power: 2
Solved Threads: 5
•
•
•
•
conn.execute "INSERT INTO tblUsers (UserName, Password) VALUES ('Hello', 'World');", conn[/code]
should be
conn.execute "INSERT INTO" & tblUsers & "('" & UserName & "','" & Password & "') VALUES ('Hello', 'World');", conn
remark 1: don't mix the variables with text strings!
remark 2: there are some single qoutes between the doubles!
•
•
Join Date: Mar 2007
Posts: 59
Reputation:
Rep Power: 2
Solved Threads: 5
•
•
•
•
conn.execute "INSERT INTO tblUsers (UserName, Password) VALUES ('Hello', 'World');", conn[/code]
shouldn't that be
conn.execute "INSERT INTO" & tblUsers & "('" & UserName & "','" & Password & "') VALUES ('Hello', 'World');", conn
remark 1: don't mix the variables with text strings!
remark 2: there are some single qoutes between the doubles!
Last edited by PVBert : May 12th, 2007 at 10:07 am.
•
•
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation:
Rep Power: 3
Solved Threads: 9
Hi PVBert,
Thanks for the reply, however, I have some clarifications and a question about it.
Clarifications: The table inserted into is called tblUsers and has the following structure:
Question: Are you saying that I need to delimit the actual field names in single quotes? i.e. that the SQL statement sent to ADO should be
I have never used single quotes for delimiting the actual names of objects in SQL but if you say that this will work I'll try it on Monday.
Thanks
Yomet
Thanks for the reply, however, I have some clarifications and a question about it.
Clarifications: The table inserted into is called tblUsers and has the following structure:
tblUsers =================== UserID AutoNumber UserName String (Not NULL, not empty string) Password String (Not NULL, Empty string OK, Default "") Active Boolean (Not NULL, default True) Comment String (Not NULL, Empty String OK, Default "")
INSERT INTO tblUsers ('UserName', 'Password') Values ('Hello', 'World')I have never used single quotes for delimiting the actual names of objects in SQL but if you say that this will work I'll try it on Monday.
Thanks
Yomet
•
•
Join Date: Mar 2007
Posts: 59
Reputation:
Rep Power: 2
Solved Threads: 5
Nope. Sorry, forget my previous post. I misread your example.
Not the field NAMES but the VALUES should be in single quotes, which you did. But...
After conn.execute try to read the err object...
Not the field NAMES but the VALUES should be in single quotes, which you did. But...
After conn.execute try to read the err object...
vb Syntax (Toggle Plain Text)
on error resume next conn.Execute sql,recaffected if err<>0 then Debug.print err.description else Debug.print recaffected end if conn.close
•
•
Join Date: Mar 2007
Posts: 59
Reputation:
Rep Power: 2
Solved Threads: 5
Don't know if this is something:
Line 7 (and 23):
Line 7 (and 23):
vb Syntax (Toggle Plain Text)
SET cmd.ActiveConnection = conn
Last edited by PVBert : May 13th, 2007 at 11:04 am.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Visual Basic 4 / 5 / 6 Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- System32/Config/ System missing on XP PRO and Home dual boot system (Windows NT / 2000 / XP / 2003)
- Complex Question: Saving large articles to the DB (Perl)
- Hewlett packard 656c printer problems (Windows 9x / Me)
- Help with a script (PHP)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Outlook 2003 and Exchange Server
- Next Thread: Need learn help


Linear Mode