1

I'm trying to pass this Update command to a database, it completes ok with no errors but it doesnt update the database and I can't understand why?

    Dim Cmd As OleDbCommand
    Dim SQL As String
    Dim objCmd As New OleDbCommand

    Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Settings.MasterPath)
    Dim Item = "08/08/2015"


            SQL = ("UPDATE [Hol Dates] SET [" & EmployeeList.Text & "]= @Htype WHERE [HDate]=@Hdate")

    Cmd = New OleDbCommand(SQL, Con)
    objCmd = New OleDbCommand(SQL, Con)
    objCmd.Parameters.AddWithValue("@Hdate", item)
    objCmd.Parameters.AddWithValue("@Htype", "None")
    Con.Open()
    Dim ans = MsgBox("Do you want to unbook this holiday?", MsgBoxStyle.YesNo)

    If ans = MsgBoxResult.Yes Then
        objCmd.ExecuteNonQuery()
    Else
        Exit Sub
    End If

    Con.Close()
    Con.Dispose()
11
  • Are you sure you are passing Hdate correctly ? I mean there is a possibility that Database date is in one format (e.g. dd/mm/yyyy) and you are passing different format value. And I suggest you should fire a query using Access first in Access Query Window. That'll give you an idea Commented Aug 14, 2015 at 7:42
  • I've tried formatting the date to dd/mm/yyyy just to make sure, but it still doesnt update Commented Aug 14, 2015 at 7:46
  • I suggest you should fire a query using Access first in Access Query Window. That'll give you an idea Commented Aug 14, 2015 at 7:51
  • Could you try opening your connection before assigning into the command object? Commented Aug 14, 2015 at 7:54
  • Are 'item' and 'Item' actually different variables? Might be better to declare it as a datetime so that it is passed into the command as such. Your current declaration may be setting it as a string. Commented Aug 14, 2015 at 7:55

1 Answer 1

2

You need to reverse the order in which you add the parameters to the OleDbCommand object. OleDb allows us to assign names to parameters but it ignores the names and only pays attention to the order in which the parameters appear in the CommandText.

Therefore, since your SQL statement refers to Htype and then Hdate you need to add the parameters in that same order.

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

1 Comment

Thanks! I guess I didn't really notice the order I was doing it in. Learn something new every day!

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.