User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation: Yomet is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
Yomet Yomet is offline Offline
Junior Poster

Problems with INSERT INTO and ADO

  #1  
May 11th, 2007
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:

  1. Public conn as ADODB.Connection
  2. 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.

  1. Private Sub cmdOK_Click()
  2. Dim UserID As Long
  3. Dim SQL As String
  4. Dim cmd As ADODB.Command
  5.  
  6. Set cmd = New ADODB.Command
  7. cmd.ActiveConnection = conn
  8.  
  9. '1st try - Execute method of the Connection object
  10. ' Gives "Sytax error in INSERT INTO statement"
  11. conn.Execute "INSERT INTO tblUsers (UserName, Password) " & _
  12. "VALUES ('" & txtUserName & "', " & Chr(34) & txtPassword & Chr(34) & ")"
  13.  
  14. '2nd try - using a string variable instead of straight text
  15. ' Gives "Sytax error in INSERT INTO statement"
  16. SQL = "INSERT INTO tblUsers (UserName, Password, Active, Comments) VALUES ('" & txtUserName & "', '" & txtPassword & "', -1, '');"
  17. conn.Execute SQL, , adExecuteNoRecords
  18.  
  19. '3rd try - using the command object
  20. ' Gives "Sytax error in INSERT INTO statement"
  21. SQL = "INSERT INTO tblUsers (UserName, Password, Active, Comments) VALUES ('" & txtUserName & "', '" & txtPassword & "', -1, '');"
  22. Set cmd = New ADODB.Command
  23. cmd.ActiveConnection = conn
  24. cmd.CommandText = SQL
  25. cmd.CommandType = adCmdText
  26. cmd.Execute
  27.  
  28. '4th try - using a query in the access database
  29. ' Gives "Too few parameters, 2 expected"
  30. cmd.CommandText = "qryInsertUser" 'This query exists in the database
  31. cmd.CommandType = adCmdStoredProc
  32. cmd.CreateParameter "UserName", adVarChar, adParamInput, , txtUserName
  33. cmd.CreateParameter "Password", adVarChar, adParamInput, , txtPassword
  34. cmd.NamedParameters = True
  35. cmd.Execute
  36.  
  37. '5th try - a colleague inerted records using this circumvent way of running the query
  38. ' Gives "Sytax error in INSERT INTO statement"
  39. SQL = "INSERT INTO tblUsers (UserName, Password) VALUES ('" & txtUserName & "', '" & txtPassword & "');"
  40. rs1.Open SQL, conn, adOpenForwardOnly, adLockReadOnly
  41.  
  42. 'This works perfectly
  43. rs1.Open "SELECT * FROM tblUsers WHERE UserName = '" & txtUserName & "'", conn, adOpenDynamic
  44. UserID = rs1!UserID
  45. rs1.Close
  46. rs1.Open "SELECT * FROM tblGroups ORDER BY GroupName", conn, adOpenDynamic
  47. rs1.MoveFirst
  48. rs1.Find "GroupName = 'Users'"
  49. conn.Execute "INSERT INTO tblUserGroups (UserID, GroupID) VALUES (" & UserID & ", " & rs1!GroupID & ")"
  50. 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation: Yomet is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
Yomet Yomet is offline Offline
Junior Poster

Re: Problems with INSERT INTO and ADO

  #2  
May 11th, 2007
One more thing, I tried the same INSERT statement but without the variables, i.e.
  1. 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.
Reply With Quote  
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation: Yomet is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
Yomet Yomet is offline Offline
Junior Poster

Re: Problems with INSERT INTO and ADO

  #3  
May 11th, 2007
Well,

I found one workaround using the parametrized query stored in the database, here it is
  1. '6th try - using a query in the access database
  2. Dim arr(1)
  3. cmd.CommandText = "qryInsertUser" 'This query exists in the database
  4. cmd.CommandType = adCmdStoredProc
  5. arr(0) = "sdfasdr"
  6. arr(1) = "kihjui"
  7. 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
Reply With Quote  
Join Date: Mar 2007
Posts: 59
Reputation: PVBert is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
PVBert PVBert is offline Offline
Junior Poster in Training

Re: Problems with INSERT INTO and ADO

  #4  
May 12th, 2007
Originally Posted by Yomet View Post
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!
Reply With Quote  
Join Date: Mar 2007
Posts: 59
Reputation: PVBert is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
PVBert PVBert is offline Offline
Junior Poster in Training

Re: Problems with INSERT INTO and ADO

  #5  
May 12th, 2007
Originally Posted by Yomet View Post
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.
Reply With Quote  
Join Date: Nov 2005
Location: Montreal, QC (Almost)
Posts: 130
Reputation: Yomet is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
Yomet Yomet is offline Offline
Junior Poster

Re: Problems with INSERT INTO and ADO

  #6  
May 13th, 2007
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:
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 "")
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 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
Reply With Quote  
Join Date: Mar 2007
Posts: 59
Reputation: PVBert is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
PVBert PVBert is offline Offline
Junior Poster in Training

Re: Problems with INSERT INTO and ADO

  #7  
May 13th, 2007
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...
  1. on error resume next
  2. conn.Execute sql,recaffected
  3. if err<>0 then
  4. Debug.print err.description
  5. else
  6. Debug.print recaffected
  7. end if
  8. conn.close
Reply With Quote  
Join Date: Mar 2007
Posts: 59
Reputation: PVBert is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
PVBert PVBert is offline Offline
Junior Poster in Training

Re: Problems with INSERT INTO and ADO

  #8  
May 13th, 2007
Don't know if this is something:
Line 7 (and 23):
  1. SET cmd.ActiveConnection = conn
Last edited by PVBert : May 13th, 2007 at 11:04 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 11:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC