0

I have inserted my record successfully with the OleDbCommand and parameters, but if I update the record I get the following error:

Syntax Error!

I have tried put the square brackets around the Field Name, and it still does not work. But if I copy the command to update with Access query, it works.

Code:

Public Function update() As Boolean
    Dim STATE As Boolean = False
    Dim cmd As New OleDbCommand
    cmd.Connection = cn
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "UPDATE [GUEST_DATA_TBL] SET [USD]=@USD, [RIEL]=@RIEL, [EURO]=@EURO, [BAHT]=@BAHT, [AUSD]=@AUSD, [GIFT]=@GIFT, [MEMO]=@MEMO WHERE [ID]=@ID"
    AssignParams(cmd)
    cmd.ExecuteNonQuery()
    Return STATE
End Function

'the parameters

Private Sub AssignParams(cmd As OleDbCommand)
        cmd.Parameters.AddWithValue("@ID", ID)
        cmd.Parameters.AddWithValue("@USD", USD)
        cmd.Parameters.AddWithValue("@RIEL", RIEL)
        cmd.Parameters.AddWithValue("@BAHT", BAHT)
        cmd.Parameters.AddWithValue("@EURO", EURO)
        cmd.Parameters.AddWithValue("@AUSD", AUSD)
        cmd.Parameters.AddWithValue("@GIFT", GIFT)
        cmd.Parameters.AddWithValue("@MEMO", MEMO)
End Sub
2
  • Set the order of the parameters to how they appear in the statement. ID should be last. Commented Apr 29, 2017 at 6:07
  • Also note that the parameters BAHT and EURO need to be switched in your AssignParams method. Commented Apr 29, 2017 at 7:43

2 Answers 2

2

First it's good to see that you are already using parameters but with MS Access, the order of the parameters is important not the names. I use the ? placeholder within my SQL command when using parameters. I also specify the data type so consider using the OleDbParameter Constructor (String, OleDbType) to add your parameters.

I would also consider implementing Using:

Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. However, you can still use a Using block to force the disposal of a managed resource instead of waiting for the garbage collector.

You could implement a check for the value returned by ExecuteNonQuery() to see how many rows were effected.

Lastly, with VB.NET by not specifying a modifier here; Private Sub AssignParams(cmd As OleDbCommand), the compiler by default will use ByVal:

Specifies that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code.

You should be using ByRef:

Specifies that an argument is passed in such a way that the called procedure can change the value of a variable underlying the argument in the calling code.

Your code would look something like this:

Public Function update() As Boolean

    Dim STATE As Boolean = False

    Using con As OleDbConnection = cn,
          cmd As New OleDbCommand("UPDATE [GUEST_DATA_TBL] SET [USD]=?, [RIEL]=?, [EURO]=?, [BAHT]=?, [AUSD]=?, [GIFT]=?, [MEMO]=? WHERE [ID]=?", con)

        con.Open()

        AssignParams(cmd)

        Dim rowsAffected As Integer = cmd.ExecuteNonQuery()

        If rowsAffected > 0 Then
            STATE = True
        End If

    End Using

    Return STATE

End Function

Private Sub AssignParams(ByRef cmd As OleDbCommand)
    cmd.Parameters.Add("@USD", OleDbType.[Type]).Value = USD
    cmd.Parameters.Add("@RIEL", OleDbType.[Type]).Value = RIEL
    cmd.Parameters.Add("@EURO", OleDbType.[Type]).Value =  EURO
    cmd.Parameters.Add("@BAHT", OleDbType.[Type]).Value =  BAHT
    cmd.Parameters.Add("@AUSD", OleDbType.[Type]).Value =  AUSD
    cmd.Parameters.Add("@GIFT", OleDbType.[Type]).Value = GIFT
    cmd.Parameters.Add("@MEMO", OleDbType.[Type]).Value = MEMO
    cmd.Parameters.Add("@ID", OleDbType.[Type]).Value = ID
End Sub

Note that I have used OleDbType.[Type]. You will want to replace [Type] with the data type you've used on your database.

Sign up to request clarification or add additional context in comments.

Comments

0
Private Sub AssignParams(cmd As OleDbCommand)
   cmd.Parameters.AddWithValue("@USD", USD)
   cmd.Parameters.AddWithValue("@RIEL", RIEL)
   cmd.Parameters.AddWithValue("@EURO", EURO)
   cmd.Parameters.AddWithValue("@BAHT", BAHT)       
   cmd.Parameters.AddWithValue("@AUSD", AUSD)
   cmd.Parameters.AddWithValue("@GIFT", GIFT)
   cmd.Parameters.AddWithValue("@MEMO", MEMO)
   cmd.Parameters.AddWithValue("@ID", ID)
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.