Hi i wonder how can i delete my data using access as my database?

for example i want to delete

        Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & ConAccessDatabase & "';")
    Dim cmd As OleDbCommand


    cmd = New OleDb.OleDbCommand("delete * from [" & EBU2DB_LOB & "] where Brand ='" & Form_Month_End_Report.Combo_LOB_Brand.Text & "' & LOB ='" & Form_Month_End_Report.Combo_LOB_LOB.Text & "' & Months = '" & Form_Month_End_Report.Combo_LOB_Month.Text & "'", con)

    Dim response As String = MsgBox("Do you want Delete ? ", MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
        If con.State = ConnectionState.Closed Then con.Open()
        cmd.ExecuteNonQuery()
    End If

this is my code.
but nothing seem to happen to my data..

Try this

Dim cmd As New OleDbCommand("", con)

cmd.CommandText = "DELETE FROM [" & EBU2DB_LOB & "] " _
                & " WHERE Brand  = ? " _
                & "   AND LOB    = ? " _
                & "   AND Months = ? "

cmd.Parameters.AddWithValue("@Brand ", Form_Month_End_Report.Combo_LOB_Brand.Text)
cmd.Parameters.AddWithValue("@LOB   ", Form_Month_End_Report.Combo_LOB_LOB.Text)
cmd.Parameters.AddWithValue("@Months", Form_Month_End_Report.Combo_LOB_Month.Text)

If MsgBox("Do you want to Delete?", MsgBoxStyle.YesNo, "Confirm") = MsgBoxResult.Yes Then
    If con.State = ConnectionState.Closed Then con.Open()
    cmd.ExecuteNonQuery()
End If
Comments
Yep.

You can do as Jim has said and shave a VERY small amount of time off by placing everything inside of the If statement:

If MsgBox("Do you want to Delete?", MsgBoxStyle.YesNo, "Confirm") = MsgBoxResult.Yes Then
    Dim cmd As new OleDBCommand("",con)
    cmd.CommandText = "DELETE FROM [" & EBU2DB_LOB & "] " _
            & " WHERE Brand  = ? " _
            & "   AND LOB    = ? " _
            & "   AND Months = ? "
    cmd.Parameters.AddWithValue("@Brand ", Form_Month_End_Report.Combo_LOB_Brand.Text)
    cmd.Parameters.AddWithValue("@LOB   ", Form_Month_End_Report.Combo_LOB_LOB.Text)
    cmd.Parameters.AddWithValue("@Months", Form_Month_End_Report.Combo_LOB_Month.Text)
    If con.State = ConnectionState.Closed Then con.Open()
    cmd.ExecuteNonQuery()
End If

This will only create the command and set the command text when you want to delete the item.

Edited 3 Years Ago by Begginnerdev

Comments
Also yep!

Nice it works fine :)
i just get confuse in this one

= ? "

thank you

This question has already been answered. Start a new discussion instead.