Hi,

I have the following code which deletes a row after confirmation. As I know a little bit with message boxes, I could not figure it out. Hence if I say 'Yes' or 'No' the selected row is deleted. Please fix the problem.

MsgBox("Do you really want to delete this row ?", MsgBoxStyle.YesNo)
            With MyCommand
                .Connection = modConnection.MyConnection
                .CommandText = "Delete from Contacts where TAID=" & Val(LVW.Tag) & ""
                .ExecuteNonQuery()
            End With

The first line of code requires the correct code.
Thanks.

MsgBox() function returns a DialogResult of the given type (in your case its "Yes" or "No"). See how its used:

If (MsgBox("Do you really want to delete this row ?", MsgBoxStyle.YesNo) = DialogResult.Yes) Then
            With MyCommand
                .Connection = modConnection.MyConnection
                .CommandText = "Delete from Contacts where TAID=" & Val(LVW.Tag) & ""
                .ExecuteNonQuery()
            End With
End If

Now it'll only delete if the dialogresult is "Yes" i.e when the "Yes" button is clicked.

Thanks

MsgBox() function returns a DialogResult of the given type (in your case its "Yes" or "No"). See how its used:

Just it shows only one button "OK" button. And when press this button, it deletes. There is now 'Yes" & "No" options available on message box. Please do help.

The following is the full code:

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Dim MyCommand As New OleDbCommand
        If LVW.Tag <> "" Then
            MsgBox("Do you really want to delete this row ?", MsgBoxStyle.YesNo = DialogResult.Yes) Then
            With MyCommand
                .Connection = modConnection.MyConnection
                .CommandText = "Delete from Contacts where TAID=" & Val(LVW.Tag) & ""
                .ExecuteNonQuery()
            End With
            LVWLoader()
            LVW.Tag = ""

        Else : MsgBox("Please select the data that to be deleted", MsgBoxStyle.Critical)
        End If
    End Sub

Edited 6 Years Ago by Learner7: n/a

I can't understand you. However, the MsgBox code you are using should be:

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Dim MyCommand As New OleDbCommand
        If LVW.Tag <> "" Then
           [B]If MsgBox("Do you really want to delete this row ?", MsgBoxStyle.YesNo) = DialogResult.Yes Then[/B]
            With MyCommand
                .Connection = modConnection.MyConnection
                .CommandText = "Delete from Contacts where TAID=" & Val(LVW.Tag) & ""
                .ExecuteNonQuery()
            End With
            LVWLoader()
            LVW.Tag = ""
        End If

        Else : MsgBox("Please select the data that to be deleted", MsgBoxStyle.Critical)
        End If
    End Sub

I can't understand you. However, the MsgBox code you are using should be:

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Dim MyCommand As New OleDbCommand
        If LVW.Tag <> "" Then
           [B]If MsgBox("Do you really want to delete this row ?", MsgBoxStyle.YesNo) = DialogResult.Yes Then[/B]
            With MyCommand
                .Connection = modConnection.MyConnection
                .CommandText = "Delete from Contacts where TAID=" & Val(LVW.Tag) & ""
                .ExecuteNonQuery()
            End With
            LVWLoader()
            LVW.Tag = ""
        End If

        Else : MsgBox("Please select the data that to be deleted", MsgBoxStyle.Critical)
        End If
    End Sub

It is working fine now. Thanks a lot for the help.

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