Results 1 to 14 of 14

Thread: [RESOLVED] Syntax error in UPDATE statement

Hybrid View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Resolved [RESOLVED] Syntax error in UPDATE statement

    Hi there, having a bit of a problem with one of my SQL statements.

    I'm trying to get the table to update the password field.
    Basically the user logs in on one form and then the form visibility is set to false.
    The variables for that form are set to Public shared, so i can use then on this form. In this case they are frmlogin.staffid and frmlogin.level

    I've tried copying and pasting the SQL into access and it works fine, so not too sure whats going wrong.

    The exact error i get is in the title.

    The other fields in the table are not relevant (Forename and Surname) so i haven't included them in the query.


    Code:
    'If the new password textboxes don't contain the same password then clear them and get them to do it again.
                If txtnewpass.Text = txtnewpass1.Text Then
    
                    Dim dbCommand As OleDbCommand = New OleDbCommand
    
                    'create and open the connection object
                    dbconnection = New OleDbConnection(strconnection)
                    dbconnection.Open()
    
                    Using dbCmd As New OleDbCommand
                        dbCommand.Connection = dbconnection
    
                        'Set the SQL up for updating the staff table
                        dbCommand.CommandText = "UPDATE staff SET Password = @newpassword WHERE staff_ID = @Staff_ID AND [Level] = @level  AND Password = @oldpass"
    
                        dbCommand.CommandType = CommandType.Text
                        'Add in the new / updated values
    
                        With dbCommand.Parameters
    
                            .AddWithValue("@newpassword", txtnewpass.Text)
                            .AddWithValue("@level", frmLogin.level)
                            .AddWithValue("@staff_ID", frmLogin.StaffID)
                            .AddWithValue("@oldpass", txtoldpass.Text)
                        End With
    
                        '   and finally execute the SQL to update the record in customers table
                        Try
                            dbCommand.ExecuteNonQuery()
    
                        Catch ex As Exception
    
                            MsgBox(ex.Message, MsgBoxStyle.Critical)
                        End Try
    
                    End Using
    
                    dbconnection.Close()
                    'Give user feedback 
                    lblaction.Text = "Password successfully changed"
    
                Else
                    MessageBox.Show("Passwords do not match")
                    cleartextboxes(grpchangepass)
    
    
                End If
    Can anyone spot anything wrong with it?
    Thanks in advance.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Syntax error in UPDATE statement

    The problem is that Password is a reserved word, and as such should not be used as a field/table name (it confuses the query parser within the database system).

    If you change the field name (in the database and your code), it should work correctly.

    For more information, see the article What names should I NOT use for tables/fields/views/stored procedures/...? from our Database Development FAQs/Tutorials (at the top of this forum)

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Syntax error in UPDATE statement

    Password is a reserverd word.

    Place Password field name in square brackets ( [Password] ) or better yet don't use that as a fieldname maybe use Pword or Passwrd
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: Syntax error in UPDATE statement

    Cheers guys, i remembered to put the square brackets round 'Level' but not password.
    Knew it would be something stupid like that.
    Thanks again.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] Syntax error in UPDATE statement

    Sorry to bother you all again, but it's not updating it into the database?
    I get no error when it runs, but it doesn't actually update it.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Syntax error in UPDATE statement

    Add this
    Code:
    Try
         Dim ResutlCount As Integer = dbCommand.ExecuteNonQuery()
         MessageBox.Show("Rows Effected: " & ResultCount.ToString()
    Catch ex As Exception
    
         MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
    What is the result in the messagebox.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] Syntax error in UPDATE statement

    Quote Originally Posted by GaryMazzone View Post
    Add this
    Code:
    Try
         Dim ResutlCount As Integer = dbCommand.ExecuteNonQuery()
         MessageBox.Show("Rows Effected: " & ResultCount.ToString()
    Catch ex As Exception
    
         MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
    What is the result in the messagebox.
    Rows Effected: 0

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Syntax error in UPDATE statement

    So that means that nothing was updated in the database.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] Syntax error in UPDATE statement

    I've basically used the same code for my other tables and they seem to work. All i did was change the SQL bit and it doesn't work now?

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Syntax error in UPDATE statement

    Nothing is being updated because nothing matches your WHERE conditions.

  11. #11
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Syntax error in UPDATE statement

    First this is access so named parameters mean nothing... Access is postional only so the order of your parameters in the SQL is

    1. NewPassword
    2. StaffId
    3. Level
    4. OldPassword

    the Parameter.Add statements need to be in that same order.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] Syntax error in UPDATE statement

    How come its not finding any matches?

    I have run a "Select * WHERE" query using those parameters in access and it finds the match.
    I have tried taking out the parameter and putting values into the code to see if it can find it that way, still not finding any matches.

    The values in the variables are definitely correct as i've put a break in the code and checked it. I see no reason why this shouldn't work, and it's really annoying me now.

  13. #13
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Syntax error in UPDATE statement

    Do you understand post #11?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2010
    Posts
    22

    Re: [RESOLVED] Syntax error in UPDATE statement

    Quote Originally Posted by GaryMazzone View Post
    Do you understand post #11?
    Yeah, sorry i fixed that before, but still get 0 rows updated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width