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..

Recommended Answers

All 3 Replies

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
commented: Yep. +8

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.

commented: Also yep! +12

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

= ? "

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.