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
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.