Runtime Error 2501- Getting annoying now. (1 Viewer)

xyba

Registered User.
Local time
Today, 00:20
Joined
Jan 28, 2016
Messages
189
I'm getting a 2501 Open Form action cancelled error when I click the login button on my login form. On selecting debug the highlighted code is:

Code:
DoCmd.OpenForm "Navigation Form"

The Navigation form has an On Open event which calls a tempvars macro:

Code:
Private Sub Form_Open(Cancel As Integer)
If TempVars!CurrentLevel = "Admin" Then
    'ShortcutMenu = True
    btnNewUser.Visible = True
    UUbtn.Visible = True
    UDbtn.Visible = True
    Else
    'ShortcutMenu = False
    btnNewUser.Visible = False
    UUbtn.Visible = False
    UDbtn.Visible = False
End If

End Sub

When I comment this code out the form opens as expected without an error, so this is where I started investigating.

I've confirmed all the controls in the above code are still valid and spelled correctly. I've copied and pasted last known working code from a backup and I've also deleted and recreated the tempvar but I still get the 2501 error.

All was fine before I made some formatting changes to my forms today. No controls have been renamed and no code has been touched so I'm at a loss.

I am resigned to going back to an earlier front end backup but, before I do, if someone could somehow point me in the right direction on what I can do about this I would be grateful.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:20
Joined
Oct 29, 2018
Messages
21,454
Hi. Just for troubleshooting purposes, try to leave the code in but open the form manually rather than from the button on your login form just to see if you get any errors.
 

xyba

Registered User.
Local time
Today, 00:20
Joined
Jan 28, 2016
Messages
189
Hi. Just for troubleshooting purposes, try to leave the code in but open the form manually rather than from the button on your login form just to see if you get any errors.

Hi. It won't open manually. I've attached a screenshot of the error message.
 

Attachments

  • error.png
    error.png
    9.3 KB · Views: 194

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:20
Joined
Oct 29, 2018
Messages
21,454
Hi. It won't open manually. I've attached a screenshot of the error message.
This is why you were getting that error 2501 when you click the button on the login form. To fix it, you'll have to fix this error. Do you know what ActiveX control the error is referring to?
 

xyba

Registered User.
Local time
Today, 00:20
Joined
Jan 28, 2016
Messages
189
This is why you were getting that error 2501 when you click the button on the login form. To fix it, you'll have to fix this error. Do you know what ActiveX control the error is referring to?

No, I don't know.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 07:20
Joined
May 7, 2009
Messages
19,229
move your code to the Load event.
on open event all controls are not yet initialized or does not exists.
 

xyba

Registered User.
Local time
Today, 00:20
Joined
Jan 28, 2016
Messages
189
move your code to the Load event.
on open event all controls are not yet initialized or does not exists.

That worked, eventually. I couldn't change it initially because the property sheet was locked up.

But that seems to have resolved it. Strange because it worked perfectly in the On Open event before.

At least it's working now.

Thank you for your help.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:20
Joined
Oct 29, 2018
Messages
21,454
That worked, eventually. I couldn't change it initially because the property sheet was locked up.

But that seems to have resolved it. Strange because it worked perfectly in the On Open event before.

At least it's working now.

Thank you for your help.
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:20
Joined
Feb 28, 2001
Messages
27,147
Arnel is spot-on with his explanation.

For the record, most events fire AFTER the event conditions are detected. Therefore, in the OnOpen event, the form has been opened and its properties have been identified. It has an environment. But it isn't until OnLoad that the form CONTENT has been loaded to memory and prepared for eventual use. And it isn't until OnCurrent that a current record has been identified, loaded to the form's .Recordset, and its bound values have been distributed to corresponding bound controls.

If the event says "Beforexxxx" then "xxxx" hasn't happened yet. But if it says "Afterxxxx" then "xxxx" is a done deal. If it says neither "Before" nor "After" then it usually means "After" as a rule of thumb.
 

Micron

AWF VIP
Local time
Yesterday, 19:20
Joined
Oct 20, 2018
Messages
3,478
I'm getting a 2501 Open Form action cancelled error
FYI - there is another trap you can fall into that will result in this error:
- form1 code opens form2
- form2 code decides that form2 should not open thus the Open event is cancelled.
- execution returns to form1 code
- error 2501 is raised because the command to open the form didn't execute


The solution in that case (IMHO) is to just trap the error.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 18:20
Joined
Feb 28, 2001
Messages
27,147
Micron offers a good point. If the form decides it doesn't want to open, it will raise an "Event Cancelled" error, which is a trappable error to the thing that tried to open the form. I.e. trappable in the sense that because the form didn't open, Access believes (correctly) that it should notify you that something went wrong - and in this case, it does so by signaling error 2501 - which IS specific.

Since you were planning to open your login form by clicking a login button (which by definition HAS to be on another form), you have EXACTLY the case that Micron described. The real fault isn't in the button. It is in the form opened by the button.
 

Users who are viewing this thread

Top Bottom