BeforeUpdate vbYesNoCancel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blad3runn69
    New Member
    • Jul 2007
    • 59

    BeforeUpdate vbYesNoCancel

    vbYesNoCancel msgbox function gets called from sub Form_BeforeUpda te if user clicks form exit button or the form window X button and the form is dirty.

    Howto halt the exit sub (ie. form close) if the user clicks canel?

    thanks.
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Originally posted by blad3runn69
    vbYesNoCancel msgbox function gets called from sub Form_BeforeUpda te if user clicks form exit button or the form window X button and the form is dirty.

    Howto halt the exit sub (ie. form close) if the user clicks canel?

    thanks.
    Hi. Set the Cancel parameter to True within your BeforeUpdate event code:
    [CODE=vb]Private Sub Formname_Before Update(Cancel As Integer)
    Cancel=True
    (other code to handle event)
    End Sub[/CODE]
    -Stewart

    Comment

    • blad3runn69
      New Member
      • Jul 2007
      • 59

      #3
      Hi Stewart, thank you for your help, you have helped me more than you know :).

      If me.dirty then...

      I insert Cancel = True into the beforeUpdate sub it doesn't work
      the results are

      1. close through form exit button sub
      cancel still closes form,
      if I try to put Cancel = True into the exit button sub I get a variable not defined error

      2. close through window x form button
      prompts msgbox You can't save the record @ this time... close form yes/no (close :))

      thank you for your help

      Comment

      • dima69
        Recognized Expert New Member
        • Sep 2006
        • 181

        #4
        Originally posted by blad3runn69
        Hi Stewart, thank you for your help, you have helped me more than you know :).

        If I insert Cancel = True into the beforeUpdate sub it doesn't work
        the results are

        1. close through form exit button sub
        cancel still closes form,
        if I try to put Cancel = True into the exit button sub I get a variable not defined error

        2. close through window x form button
        prompts msgbox You can't save the record @ this time... close form yes/no (close :))

        thank you for your help
        In order to prevent form closing you have to use Form_Unload event (it has Cancel argument). For instance:
        [CODE=vb]Private Sub Form_BeforeUpda te(Cancel as integer)
        If MsgBox("Do you want to save ...") = vbYes then
        NoCloseFlag = FALSE
        Else
        NoCloseFlag = TRUE
        End If
        End Sub

        Private Sub Form_Unload(Can cel as integer)
        Cancel = NoCloseFlag
        End Sub[/CODE]

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          Thanks, Dima69, and apologies to Blad3runner69 for misleading you over other steps that would be needed.

          -Stewart

          Comment

          • blad3runn69
            New Member
            • Jul 2007
            • 59

            #6
            cool stuff, thankyou very much for your help dima69, it is muchly appreciated, no apologies needed Stewart, thank you it all helps! :)

            Comment

            • blad3runn69
              New Member
              • Jul 2007
              • 59

              #7
              ah ok I need to use the unload event, sorry my bad thanks again for your help

              Comment

              • blad3runn69
                New Member
                • Jul 2007
                • 59

                #8
                meh... it seems ypu have to continually program agains the inherent nature of access, it is a data monster, still looing for a simple beforeupdate vbyesncancel...

                Comment

                • MikeTheBike
                  Recognized Expert Contributor
                  • Jun 2007
                  • 640

                  #9
                  Originally posted by blad3runn69
                  meh... it seems ypu have to continually program agains the inherent nature of access, it is a data monster, still looing for a simple beforeupdate vbyesncancel...
                  Hi

                  Not sure precisly what you are looking for but, following on from previous post, might this help?
                  Code:
                  Option Compare Database
                  Option Explicit
                  Dim bCancelClose As Boolean
                  
                  Private Sub Form_BeforeUpdate(Cancel As Integer)
                      If Me.Dirty Then
                          If MsgBox("Cancel Close?", vbYesNoCancel + vbQuestion, "Close") = vbCancel Then bCancelClose = True
                      End If
                  End Sub
                  
                  Private Sub Form_Unload(Cancel As Integer)
                      Cancel = bCancelClose
                      bCancelClose = False
                  End Sub
                  ??

                  MTB

                  Comment

                  • blad3runn69
                    New Member
                    • Jul 2007
                    • 59

                    #10
                    thanks for your help Mike The Bike, Stewart and dima69.
                    simple in theory, a form has exit/X buttons.

                    if the user clicks exit/X and the form is dirty then
                    msgbox 'save before exit?', yesnocancel

                    choosing cancel closes msgbox and returns user to the form in an unchanged state (eg. close notepad when there are unsaved changes & you are prompted with yesnocancel, click cancel the msgbox closes, no save, no close and no undo).

                    howto incorporate cancel into events? (holy moly batman :)

                    Thank you all again for all your help, I will look at your ideas+code, it is always muchly appreciated :).

                    Comment

                    • blad3runn69
                      New Member
                      • Jul 2007
                      • 59

                      #11
                      bump... 'alternativley, you can use a MsgBox with vbYesNoCancel, and handle the 3 states. In my view, that's less clear, but you can do it.'

                      helpa newbie yoda :)

                      Comment

                      • dreidy
                        New Member
                        • Mar 2008
                        • 1

                        #12
                        Hi,
                        I am having a similar issue.
                        I have a form linked to a table. One of the fields is required. If the user tries to leave the record (either by navigating to another record or by closing the form) without entering information for that field, i post a message asking if they want to continue (and lose the data) or cancel (and remain in the form on that record so they can enter the required information). If the user continues, the BeforeUpdate event works properly, using a Me.Undo. However, if the user closes the form (X button) and gets the message and decides to cancel the action of closing the form so they can enter the required information, they get an error. The message states that they will need to fill in [table].[required_field] because it can not be set to null.

                        I tried Cancel = True in the BeforeUpdate event, and then i tried a suggestion from this thread by setting the Form_Unload event, Cancel = True. None of this seems to be working. Any other suggestions?

                        Comment

                        • jennyfifi
                          New Member
                          • Aug 2009
                          • 1

                          #13
                          I was registered at your forum. I have printed the test message. Do not delete, please.

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Hi Jennyfifi,

                            Your questions are welcome here on bytes, but if you post links to websites as your signature you will not be able to participate on this forum since this is considered to be spam and will not be tolerated. Please refrain from posting links as your signature.

                            I hope you find all the help you need here.

                            -Moderator Frinny

                            Comment

                            Working...