Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete Record (A2k)

    With the following code, if I select "No" (vbNo) from the msgbox, the record is still deleted and can't see why.
    I tried "Exit Sub" after each relevant "End If" and still no avail.


    Private Sub cmdDelete_Click()
    On Error GoTo ErrLine
    MsgBox "You Are About To Delete This Record, Do You Want To Continue", vbCritical + vbYesNo, ""
    If vbYes Then
    DoCmd.SetWarnings False
    Me.Undo
    RunCommand acCmdDeleteRecord
    DoCmd.SetWarnings True
    DoCmd.Close acForm, Me.Name, acSaveNo
    End If


    If vbNo Then
    Me.Undo
    Cancel = True
    DoCmd.Close acForm, Me.Name, acSaveNo
    End If


    If Cancel = False Then
    DoCmd.Close acForm, Me.Name, acSaveNo
    End If
    Exit Sub
    ErrLine:
    If Err <> 2501 Then
    MsgBox Err.Description, vbExclamation
    End If
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Delete Record (A2k)

    Hi Dave,

    1. In your code, you call MsgBox as a procedure, instead of as a function: you don't retrieve the return value.
    2. You refer to Cancel, but that is not available inside this procedure (unless you defined a module-wide or global variable Cancel)
    3. As written, you ALWAYS close the form, and you ALWAYS undo all changes to the record. Is that your intention?

    Here is the modification necessary to make it react to the choice in the message box; I have left the DoCmd.Close and DoCmd.Undo in place.

    Private Sub cmdDelete_Click()
    On Error GoTo ErrLine

    Select Case MsgBox("You Are About To Delete This Record, Do You Want To Continue", vbCritical + vbYesNo)

    Case vbYes
    DoCmd.SetWarnings False
    Me.Undo
    RunCommand acCmdDeleteRecord
    DoCmd.SetWarnings True
    DoCmd.Close acForm, Me.Name, acSaveNo

    Case vbNo
    Me.Undo
    DoCmd.Close acForm, Me.Name, acSaveNo

    End Select

    Exit Sub

    ErrLine:
    If Err <> 2501 Then
    MsgBox Err.Description, vbExclamation
    End If
    End Sub

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Delete Record (A2k)

    Select Case MsgBox("You Are About To Delete This Record, Do You Want To Continue", vbCritical + vbYesNo)

    Thanks Hans, the update works fine.
    I've not seen the "Select Case" used in this way before, sure is much simpler.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •