Results 1 to 6 of 6
  1. #1
    Join Date
    May 2008
    Location
    London, UK
    Posts
    46

    Question Unanswered: Message Box for Undo Record & Close Form Button Code

    Hello again Guys and Gals,

    I have created a "Cancel" button on my form which checks to see if the record has been altered and undo's changes before it closes the form using an "If Me.Dirty" comand, which works and seems well suited for my needs on a user entry form I'm working on.

    What I would like it to do is show a message box on screen asking the user to confirm they understand that any data entered into the form will be lost if they continue with an OK and Cancel button.

    So if they click OK the code will continue to run, and if they click cancel it will... well... cancel the action!

    This is the code for my button:
    Code:
    Private Sub cmdCancel_Click()
    On Error GoTo Err_cmdCancel_Click
    If Me.Dirty = True Then
       'If not saved undo changes:
       DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
       'Then close form:
       DoCmd.Close
      End If
    If Me.Dirty = False Then
       'If no changes close form:
       DoCmd.Close
      End If
    Exit_cmdCancel_Click:
      Exit Sub
    Err_cmdCancel_Click:
       'On error no message close:
      Resume Exit_cmdCancel_Click
    End Sub
    I have tried a couple of things but I am really just stabbing in the dark! Anyone got any ideas?

    Thanks!

    Mike

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Code:
    If MsgBox("If you click Yes, I'll open another messagebox.", vbYesNo) = vbYes Then
        MsgBox "I told you I'd open another one!"
    End If
    George
    Home | Blog

  3. #3
    Join Date
    May 2008
    Location
    London, UK
    Posts
    46
    So I could do this:
    Code:
    Private Sub cmdCancel_Click()
    On Error GoTo Err_cmdCancel_Click
    If MsgBox("Are you sure you want to cancel, this will delete any changes made to this record.", vbYesNo) = vbYes Then
    If Me.Dirty = True Then
       'If not saved undo changes:
       DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
       'Then close form:
       DoCmd.Close
      End If
    If MsgBox("Are you sure you want to cancel, this will delete any changes made to this record.", vbYesNo) = vbNo Then
    If Me.Dirty = False Then
       'If no changes close form:
       DoCmd.Close
      End If
    Exit_cmdCancel_Click:
      Exit Sub
    Err_cmdCancel_Click:
       'On error no message close:
      Resume Exit_cmdCancel_Click
    End Sub
    (I'm not at my computer at the mo so can't test!)

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    You have 4 Ifs and only two End Ifs...

    I'd probably go with (based on my spin on your requirements)

    Pseudo code
    Code:
    if dirty then
        if MsgBox("Do you want to saave changes?") = vbYes then
           save
        else
           undo
        end if
    end if
    George
    Home | Blog

  5. #5
    Join Date
    May 2008
    Location
    London, UK
    Posts
    46

    Talking

    Code:
    Private Sub cmdTest_Click()
    On Error GoTo Err_cmdTest_Click
    If Me.Dirty = True Then
        If MsgBox("Are you sure you wish to Cancel? Any unsaved information will be lost. Please Click OK to Continue or Cancel to Return to the form", vbOKCancel) = vbOK Then
            'If ok then undo unsaved changes:
            DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70
            'Then close form:
            DoCmd.Close
            End If
        End If
    If Me.Dirty = False Then
        'If no changes close form:
        DoCmd.Close
    End If
    Exit_cmdTest_Click:
    Exit Sub
    Err_cmdTest_Click:
    Resume Exit_cmdTest_Click
    End Sub
    This works! Woohoo! Thanks for your help GeorgeV!

    It does act slightly differently to my original code, with the original if a user entered a value in a field then deleted it themselves and clicked the "cancel/clear" button it would simply close, now when I do the same it prompts me even though the form is clear? Not a real issue I am just trying to understand why it acts differently!?

    I would like to format the message box, change the tittle, and make it look more like the standard "question" prompts in other applications, could you point me in the right direction regarding how to achive this?

    Thanks Again!

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    So what you're saying is that when you delete a record, the form is seen to be dirty, hence why the code runs, right?

    With regards to formatting the messagebox, have a look at the other properties of the MsgBox object in the help file. From memory, you can set the title like this
    Code:
    MsgBox "mesage text", , "message title"
    George
    Home | Blog

Posting Permissions

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