"property not found" error when canceling unload event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gershwyn
    New Member
    • Feb 2010
    • 122

    "property not found" error when canceling unload event

    I have an unbound data entry form that has two buttons on it. The first automatically saves the data and then closes the form. The second should close the form, and I rely on the form's unload event to prompt the user to confirm that they want to close the form without saving any changes. I keep track of whether any changes have been made with a boolean called Changed.

    If the user makes changes and closes the form, then decides to cancel that and go back, they get a "property not found" error. If the user closes the form using the X, they do not get such a method. Can anyone explain what is actually causing this error and how to work around it?

    The close button code is simply this:
    Code:
    Private Sub btnClose_Click()
      DoCmd.Close acForm, "frmDataEntry"
    End Sub
    And this is the unload event:

    Code:
    Private Sub Form_Unload(Cancel As Integer)
      If Changed Then
        rVal = MsgBox("Data has been changed. Do you want to discard changes?", vbYesNoCancel, "Close confirmation")
        Select Case rVal
          Case vbCancel:
            Cancel = True
          Case vbNo:
            SaveChanges
        End Select
      End If
    End Sub
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Access does keep track of changed data with the "Dirty" property, so the Changed Boolean is obsolete.

    I would code:
    Code:
    If Me.Dirty then
      if msgbox ("Do you want to discard the changes?",vbYesNo) = vbNo then
        Cancel = True
      else
        docmd.close
      end if
    else
      docmd.close
    endif
    Getting the idea ?

    Nic;o)

    Comment

    • missinglinq
      Recognized Expert Specialist
      • Nov 2006
      • 3533

      #3
      Actually you cannot use the Dirty Property with an unbound form, such as the OP is using here. Unbound forms do not have this property and using it will pop an error, 2455 if I remember correctly.

      Are you saying that you get this error when the user clicks on the Cancel Button or the No Button in the message box? And if the No Button what is the code for SaveChanges?

      Welcome to Bytes!

      Linq ;0)>

      Comment

      • julietbrown
        New Member
        • Jan 2010
        • 99

        #4
        I'm not sure what's going on, but it sounds suspiciously like a problem I was having a while back ... long discussion on 'Bytes', but I'm such a Newbie I don't know how to direct you to that. Maybe one of the experts/administrators can do that?

        Anyway, I was getting a mystery message "Property not found", in just an ordinary message box, with only an OK button and no error code or offer of help, or "debug/end" option ... so no info at all about what was causing it. In the end I deleted the error handling line from the Macro involved ... All works fine, now ... But of course this may come back to bite me at some point (deleting error handling isn't generally the best way to debug code!!)

        Comment

        • gershwyn
          New Member
          • Feb 2010
          • 122

          #5
          Thank you for all your answers.

          The error occurs when the user hits the Cancel button. Every other option seems to work fine. The SaveChanges subroutine builds an append query to insert the values on the form into my table, and is working as I intended it to.

          What I don't understand is that if the user closes the form with the X button, they are presented with the same prompt and there is no error if they choose Cancel. Is there any difference between the user closing the form themselves and executing a DoCmd.Close, as far as how program execution works? I traced through it and could not detect a difference. The error pops up as soon as Form_Unload exits.

          I suppose if I have to I could get away with only giving them the Yes and No options, though I feel like some users would get nervous when faced with such a question as "Are you sure you want to discard changes?" and Cancel is the safest answer. It's a small thing, but I hate to not include it just because I can't figure out how to make it work.

          Comment

          • nico5038
            Recognized Expert Specialist
            • Nov 2006
            • 3080

            #6
            Guess you can drop the use of the Cancel option, as you just need to close the form and skip the save of the record....

            Nic;o)

            Comment

            • gershwyn
              New Member
              • Feb 2010
              • 122

              #7
              The point of the Cancel option is to *not* close the form, allowing them to return to data entry. The "Yes" and "No" options both close the form, the only difference being whether the data is saved or not before doing so.

              Comment

              • TheSmileyCoder
                Recognized Expert Moderator Top Contributor
                • Dec 2009
                • 2322

                #8
                Do you have any other event handlers on your form? I tried to "recreate" your form with 2 simple unbound textbox and a close button. I would get a specific error, I.e. a error with a number & description.
                Which I solved by some error handling:
                Code:
                Private Sub btn_Close_Click()
                    On Error GoTo err_Handler
                    
                    DoCmd.Close acForm, "frm_Test"
                    
                Exit Sub
                err_Handler:
                    If Err.Number = 2051 Then
                        Resume Next
                    End If
                    MsgBox "Error occurered: " & Err.Number & " - " & Err.Description
                    
                    
                End Sub
                On another note, you can simplify the close command. Docmd.Close will close the active object. Makes it simpler. You could alternatively do
                Code:
                DoCmd.Close acform, Me.Name
                That would work even if you change the forms name and makes it clear your closing the current form.


                If this doesn't work out for you, try to post all the code befind your form.

                Comment

                • gershwyn
                  New Member
                  • Feb 2010
                  • 122

                  #9
                  The error handling does make things work the way I had intended them to (although I do wish I understood better what was causing the error in the first place.)

                  Thank you again to all who answered.

                  On another note, you can simplify the close command. Docmd.Close will close the active object.
                  That is what I used originally, but tried a few other variations in case the error was somehow related to that line.

                  Comment

                  • TheSmileyCoder
                    Recognized Expert Moderator Top Contributor
                    • Dec 2009
                    • 2322

                    #10
                    While im a bit unsure as to the Property Not found error you were seing, the 2051 error description says something like "The Close command was cancelled", which makes perfect sense since it was cancelled. While it may be annoying here, in most cases we do want the program to throw an error when some line of code fails (which it kinda did, since its function was to close the form, and it didn't).

                    Im guessing the "X" button has some error handling attached to it, that the DoCmd.Close does not. Im unsure if there is a way to emulate clicking teh X as opposed to writing DoCmd.Close.

                    Comment

                    Working...