Solved Why does this piece of code give me an error? (2 Viewers)

mamradzelvy

Member
Local time
Today, 17:12
Joined
Apr 14, 2020
Messages
145
Hello,
I got this neat little thing somebody offered as a solution a while ago to a problem i had.
It's a "do you want to save changes?" type prompt, however, on selecting "no" it always results in an error, what's wrong with it?
the error is being caused by the docmd.close line apparently and error is 2501

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 Dim Answer As Integer
 If Me.Dirty = True Then
    Dim Response As Integer
    ' Displays a message box with the yes and no options.
    Response = MsgBox(Prompt:="Přejete si uložit data?", Buttons:=vbYesNo)
    ' If statement to check if the yes button was selected.
    If Response = vbNo Then
        DoCmd.RunCommand acCmdUndo
        DoCmd.Close
    End If
 Else
    ' The yes button was selected.
    DoCmd.Close
    Globals.Logging "Zapujceni"
  End If
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:12
Joined
May 7, 2009
Messages
19,233
apparently its the wrong place to close the form when it is still being Validated.
 

ontopofmalvern

Registered User.
Local time
Today, 16:12
Joined
Mar 24, 2017
Messages
64
It is a little long winded try

Code:
If msgbox("Přejete si uložit data?", vbYesNo) = vbNo Then
      'no button selected
      DoCmd.RunCommand acCmdUndo
      DoCmd.Close
Else
    ' The yes button was selected.
    DoCmd.Close
    Globals.Logging "Zapujceni"
End If

Should work and is easier to read, if problem persists add some error handling code or run it line at a time.
 

mamradzelvy

Member
Local time
Today, 17:12
Joined
Apr 14, 2020
Messages
145
It is a little long winded try

Code:
If msgbox("Přejete si uložit data?", vbYesNo) = vbNo Then
      'no button selected
      DoCmd.RunCommand acCmdUndo
      DoCmd.Close
Else
    ' The yes button was selected.
    DoCmd.Close
    Globals.Logging "Zapujceni"
End If

Should work and is easier to read, if problem persists add some error handling code or run it line at a time.
still results in the same error
 

ontopofmalvern

Registered User.
Local time
Today, 16:12
Joined
Mar 24, 2017
Messages
64
I'm guessing the undo command is leaving the form in a state it doesn't like, i.e. something like a required field is is empty or something isn't valid. I think I'd comment out the DoCmd.Close line and see what form looks like when you go back to it, i.e. do any of the controls have contents that are invalid?
 

Minty

AWF VIP
Local time
Today, 16:12
Joined
Jul 26, 2013
Messages
10,368
You need a bit of a change to that
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
 Dim Answer As Integer
 If Me.Dirty = True Then
    Dim Response As Integer
    ' Displays a message box with the yes and no options.
    Response = MsgBox(Prompt:="Přejete si uložit data?", Buttons:=vbYesNo)
    ' If statement to check if the yes button was selected.
    If Response = vbNo Then
        Cancel = True
        Me.Undo     
    End If
 Else
    ' The yes button was selected.
     Globals.Logging "Zapujceni"
  End If
  DoCmd.Close
End Sub
 

bob fitz

AWF VIP
Local time
Today, 16:12
Joined
May 23, 2011
Messages
4,719
The form's BeforeUpdate is usually the best place to do the validation but as arnelgp pointed out, you can not close a form before it has updated.
 

Isaac

Lifelong Learner
Local time
Today, 08:12
Joined
Mar 14, 2017
Messages
8,774
Minty is correct - your previous undo command will act like Word, only undoing the last bit of text. Me.Undo should be more 'restorative' to before it was Dirty.
 

Micron

AWF VIP
Local time
Today, 11:12
Joined
Oct 20, 2018
Messages
3,478
2501 is one of those ambiguous errors, having multiple meanings. Please always post the message as well, not just the number because even if that were not true, there are too many error numbers for (most) of us to remember. It might even be coming from another form - especially if there's no error handler in this form.
 

mamradzelvy

Member
Local time
Today, 17:12
Joined
Apr 14, 2020
Messages
145
You need a bit of a change to that
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Answer As Integer
If Me.Dirty = True Then
    Dim Response As Integer
    ' Displays a message box with the yes and no options.
    Response = MsgBox(Prompt:="Přejete si uložit data?", Buttons:=vbYesNo)
    ' If statement to check if the yes button was selected.
    If Response = vbNo Then
        Cancel = True
        Me.Undo    
    End If
Else
    ' The yes button was selected.
     Globals.Logging "Zapujceni"
  End If
  DoCmd.Close
End Sub
This still results in the same error 2501 "close action was cancelled" would be a rough translation.
 

mamradzelvy

Member
Local time
Today, 17:12
Joined
Apr 14, 2020
Messages
145
I realized i didn't mention this till now, the form i use this on specifically is a Bound Edit form.
 

Micron

AWF VIP
Local time
Today, 11:12
Joined
Oct 20, 2018
Messages
3,478
It would seem that code that comes before or after this has a call to close something but it is being canceled. I don't think your problem is with the code you just posted. This usually happens when you do something like from one form, call another form (or report) and when that opens, in form2 code you close it because something is amiss. Then this fact is returned to the calling code, which then reports to you something like "the open event was canceled". I'd look in Globals.Logging (that is a call to a procedure in a module?) to see if the fault lies there. I realize it's not an opening that is being 'canceled' but is a close, but I don't see why you shouldn't look at other code for the same sort of thing happening.

Another possibility is that your code is not compiling correctly and the close you had before is still the version in the compiler. It shouldn't hurt to manually compile your code (I do it after pretty much every edit). If you don't find that other code is where the error is triggered from and a compile doesn't help, you might want to try opening Access using the decompile switch. It's not a bad idea to backup a db before running a decompile operation on it.
 

Minty

AWF VIP
Local time
Today, 16:12
Joined
Jul 26, 2013
Messages
10,368
I would simply have a close button that does the
DoCmd.Close
The forms before update event will still fire before the button code action happens.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:12
Joined
May 7, 2009
Messages
19,233
it is simply saying, you cant close... im still validating.
 

mamradzelvy

Member
Local time
Today, 17:12
Joined
Apr 14, 2020
Messages
145
@Minty Thank you, i actually had that but i kept the docmd.close also in the beforeupdate and thus got the error.
You are the backbone of most things that work in my first project!
 

Users who are viewing this thread

Top Bottom