Syntax error in UPDATE statement.

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 11
Reputation: Birdie010 is an unknown quantity at this point 
Solved Threads: 0
Birdie010 Birdie010 is offline Offline
Newbie Poster

Syntax error in UPDATE statement.

 
0
  #1
Sep 27th, 2008
I'm trying to determine why I receive this error message when attempting to update the database.."Syntax error in UPDATE statement", please assist!

Thanks!


  1. Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  2. Dim SQLStmt As String
  3. Dim UpdateOK As Integer
  4. If txtFirstName.Text = "" And txtLastName.Text = "" Then
  5. MsgBox("All Account records must have at least a last or first name. Companies' names should be entered in the LastName field, leaving the FirstName field empty...")
  6. txtLastName.Focus()
  7. Return
  8. End If
  9. Dim UpdateCnxn As New OleDbConnection( _
  10. "Provider=Microsoft.ACE.OLEDB.12.0;" _
  11. & "Data Source=DB.mdb")
  12. UpdateCnxn.Open()
  13. 'escape any quotes to avoid problems with SQL...
  14. txtLoginId.Text.Replace("'", "''")
  15. txtPassword.Text.Replace("'", "''")
  16. txtFirstName.Text.Replace("'", "''")
  17. txtLastName.Text.Replace("'", "''")
  18. If lblId.Text = "New" Then
  19. SQLStmt = String.Format("insert into ACCOUNTS (LoginId, LoginPwd, Primary Role, FirstName, LastName) values ('{0}', '{1}', '{2}', '{3}', '{4}')", _
  20. txtLoginId.Text, txtPassword.Text, cbMenuType.Text, txtLastName.Text, txtFirstName.Text)
  21. Else
  22. SQLStmt = String.Format("update ACCOUNTS set LoginId='{0}', LoginPwd='{1}', Primary Role='{2}', FirstName='{3}', LastName='{4}' where Id={5}", _
  23. txtLoginId.Text, txtPassword.Text, cbMenuType.Text, txtFirstName.Text, txtLastName.Text, lblId.Text)
  24. End If
  25. Dim UpdateCommand As New OleDbCommand(SQLStmt, UpdateCnxn)
  26. Debug.Write("UpdateSQLStmt=" & SQLStmt & vbCrLf)
  27. UpdateOK = UpdateCommand.ExecuteNonQuery()
  28. UpdateCnxn.Close()
  29. ClearInputAreas()
  30. txtSearch.Text = ""
  31. lblAdvice.Text = "Enter some characters of the last name, or company name, to search for an Account. Or, enter the Account # if you have it. Click the New button to create a new account record."
  32. txtSearch.Focus()
  33.  
  34.  
  35. End Sub
Last edited by cscgal; Sep 27th, 2008 at 8:07 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Syntax error in UPDATE statement.

 
0
  #2
Sep 27th, 2008
Hi, Check the Field Names in the data base with the name you given in the program.

if field name contains space use Square Brackets (like [Field Name])
KSG
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: Birdie010 is an unknown quantity at this point 
Solved Threads: 0
Birdie010 Birdie010 is offline Offline
Newbie Poster

Re: Syntax error in UPDATE statement.

 
0
  #3
Sep 27th, 2008
Thanks for replying!

I changed Primary Role, to PrimaryRole, to avoid the bracket issue with the database, but I continue to receive the same error!

Any other suggestions
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: Birdie010 is an unknown quantity at this point 
Solved Threads: 0
Birdie010 Birdie010 is offline Offline
Newbie Poster

Re: Syntax error in UPDATE statement.

 
0
  #4
Sep 27th, 2008
Correction to my last reply!!!!!

I'm now receiving this error message, please assist:

"Data type mismatch in criteria expression."


CODE

  1. Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
  2. Dim SQLStmt As String
  3. Dim UpdateOK As Integer
  4. If txtFirstName.Text = "" And txtLastName.Text = "" Then
  5. MsgBox("All Account records must have at least a last or first name. Companies' names should be entered in the LastName field, leaving the FirstName field empty...")
  6. txtLastName.Focus()
  7. Return
  8. End If
  9. Dim UpdateCnxn As New OleDbConnection( _
  10. "Provider=Microsoft.ACE.OLEDB.12.0;" _
  11. & "Data Source=DB.mdb")
  12. UpdateCnxn.Open()
  13. 'escape any quotes to avoid problems with SQL...
  14. txtLoginId.Text.Replace("'", "''")
  15. txtPassword.Text.Replace("'", "''")
  16. txtFirstName.Text.Replace("'", "''")
  17. txtLastName.Text.Replace("'", "''")
  18. If lblId.Text = "New" Then
  19. SQLStmt = String.Format("insert into ACCOUNTS (LoginId, LoginPwd, PrimaryRole, FirstName, LastName,) Id values '{0}', '{1}', '{2}', '{3}', '{4}'),", _
  20. txtLoginId.Text, txtPassword.Text, cbMenuType.Text, txtFirstName.Text, txtLastName.Text)
  21. Else
  22. SQLStmt = String.Format("update ACCOUNTS set LoginId='{0}', LoginPwd='{1}', PrimaryRole='{2}', FirstName='{3}', LastName='{4}' where Id= '{5}'", _
  23. txtLoginId.Text, txtPassword.Text, cbMenuType.Text, txtFirstName.Text, txtLastName.Text, lblId.Text)
  24. End If
  25. Dim UpdateCommand As New OleDbCommand(SQLStmt, UpdateCnxn)
  26. Debug.Write("UpdateSQLStmt=" & SQLStmt & vbCrLf)
  27. UpdateOK = UpdateCommand.ExecuteNonQuery()
  28. UpdateCnxn.Close()
  29. ClearInputAreas()
  30. txtSearch.Text = ""
  31. lblAdvice.Text = "Enter some characters of the last name, or company name, to search for an Account. Or, enter the Account # if you have it. Click the New button to create a new account record."
  32. txtSearch.Focus()
  33.  
  34.  
  35. End Sub
Last edited by cscgal; Sep 28th, 2008 at 1:31 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: Syntax error in UPDATE statement.

 
0
  #5
Sep 28th, 2008
Hi,

I guess the error may be at line 19
  1. SQLStmt = String.Format("insert into ACCOUNTS (LoginId, LoginPwd, PrimaryRole, FirstName, LastName,) Id values '{0}', '{1}', '{2}', '{3}', '{4}'),", _
  2. txtLoginId.Text, txtPassword.Text, cbMenuType.Text, txtFirstName.Text, txtLastName.Text)

I think this not in the format of
INSERT INTO TABLE (Field1, Field2, ...)
VALUES (Val1, Val2, ..)

May be its your typing mistake.

Corrected may be
  1. SQLStmt = String.Format( "insert into ACCOUNTS (LoginId, LoginPwd, PrimaryRole, FirstName, LastName) values ('{0}', '{1}', '{2}', '{3}', '{4}')", _
  2. txtLoginId.Text, txtPassword.Text, cbMenuType.Text, txtFirstName.Text, txtLastName.Text)
Before execute the Query check the Query SQL.

Another possible Error May be:
You are considering all the fields are String and give them for single quote. It may be differ in the original table.

For example you have given id as Text. but it may be numeric type. So Check the Type of the Field in the Access Table
Last edited by selvaganapathy; Sep 28th, 2008 at 11:57 am.
KSG
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC