How to cancel (quit) a form open from within form load (1 Viewer)

Bilbo_Baggins_Esq

Registered User.
Local time
, 18:18
Joined
Jul 5, 2007
Messages
586
I'm curious to know, is there some way for the Form load event to cancel its own opening process?

Scenario
in form 1, a button is clicked to open form 2
Form 2 has an on load event to populate the necessary information fields in form 2
The on load process for form 2 determines there is no action to complete within form 2

Can the open process be cancelled within the form 2 on load event and return to form 1?

If so, does anybody have a sample of what that code might look like?

Before you suggest it, yes, I know I could simply run the tests in form 1 and disable the form 2 button if there is nothing to do in form 2.
However, for this application, the functionaliy in form 2 is VERY RARELY used and the calculations necessary are many and complex.
Form 1 is the launching point for several other processes and the calculations necessay to disable that one button would have to run every single time form 1 loads.
Thusly, it would be far better to only run the necessary calculations on the rare occasioins during which form 2 might be needed, when form 2 loads.

Also, I could add a button in form 2 to close form 2, but would prefer to avoid it if possible.
 

spikepl

Eledittingent Beliped
Local time
Today, 01:18
Joined
Nov 3, 2010
Messages
6,142
and the calculations necessay to disable that one button would have to run every single time form 1 loads.
No. You only need to run the calcs when the button is pressed. This is what you do now, afterall, just in Form2.

Edit: The Open event can be canceled.
 

Bilbo_Baggins_Esq

Registered User.
Local time
, 18:18
Joined
Jul 5, 2007
Messages
586
No. You only need to run the calcs when the button is pressed. This is what you do now, afterall, just in Form2.

Edit: The Open event can be canceled.

Well, of couse, I did consider that too.
However, then I end up with a crap load of variables I have to pass from form 1 to form 2 or run the calculations a second time in form 2.

If just seems easier to cancel the form open.

I just can't seem to figure out the code to cancel the form 2 open from within the form 2 onload.

EDIT: Plus, there are other forms that will open from form 2 in which work will be completed that changes the values reflected in form 2.
If I perform the calculations when returning to form 2, and the onload event determines that all work has been completed, I can use that cancel within the form 2 onload to simply skip form 2 and run back to form 1
 

Bilbo_Baggins_Esq

Registered User.
Local time
, 18:18
Joined
Jul 5, 2007
Messages
586
Hmm, well, I do refer to the docs, but in this case, it honestly did not occur to me to use a subsequent event.

I'll try it, but I suipect a simple DoCmd.CloseForm will fail since the form is not yet technically open.
 

Bilbo_Baggins_Esq

Registered User.
Local time
, 18:18
Joined
Jul 5, 2007
Messages
586
well, thgat hasn't gotten me anywhere.

As I suspected, you can't close a form until it has been opened.
The CancelEvent didn't work either.

I appreciate your efforts to coach me toward self help, but I'm not figuring this out.
A little more specific steerage or a specific function might help out here...
 

Bilbo_Baggins_Esq

Registered User.
Local time
, 18:18
Joined
Jul 5, 2007
Messages
586
So, I think I figured out something that seems to work, but I'm not sure if the gurus here would think it "proper".

In form 2's OnLoad I do all the calculations and then put a condition tester.
If there is any work to do, Form 2's OnLoad ends and Form 2 remains open.
If there is no work to do, I fire a message box then run a DoCmd to open Form 1.

First I tried putting a simple DoCmd.CloseForm for Form 2 in Form 1's OnLoad, but that didn't work.

Then, under the guess that Form 1 could not "Be Current" until after Form 2 finished what it is doing, I moved the DoCmd.CloseForm for Form 2 to Form 1's Form_Current event.

That seems to work for my purposes.
It is not technically a cancellation of the form open, but the end result is the same (apparently).
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 00:18
Joined
Sep 12, 2006
Messages
15,729
use the open event, rather than load

Code:
....
if testfail then
   cancel = true
   exit sub
end if

note that the calling form will have to handle an error 2501
 

shrove86

New member
Local time
, 19:18
Joined
Oct 2, 2018
Messages
5
Hey. I know I am years late to this thread, however, I discovered a simple solution that may help future users. In the vba for the OnLoad of the form you are opening, I created an if statement. Based on the if statement it will either close or continue opening the form. See code below.


frmOpen = MsgBox("Operator doesn't exist. Record will be automatically created.", vbYesNo)
If Not (frmOpen) Then
DoCmd.Close
Else
Me.[First Name] = FName
Me.[Last Name] = LName
End If
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
, 18:18
Joined
Feb 28, 2001
Messages
27,393
Shrove86, the preferred method is to close from the FORM_OPEN event, as later events will have to run their course AND will be visible to the end user.
 

shrove86

New member
Local time
, 19:18
Joined
Oct 2, 2018
Messages
5
If you close from the FORM_OPEN event, does it run after the form has actually opened?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
, 18:18
Joined
Feb 28, 2001
Messages
27,393
The order of events for opening a form is shown clearly in this article:

https://support.office.com/en-us/ar...-objects-e76fbbfe-6180-4a52-8787-ce86553682f9

For opening your forms, these are the events:

Open ==> Load ==> Resize ==> Activate ==> Current

Of these, only Form_Open(Cancel as Integer) has a Cancel option. If you cancel the Form_Open event by returning -1 or TRUE to the Cancel parameter, then none of the other events occurs because the form does not open. It immediately gets dereferenced by the GUI. If you do not cancel the Open event, then using any variant of VBA to close the form via something like DoCmd.Close (form), then you will probably see something because I think ALL of those events have to fire before the close takes effect.

does it run after the form has actually opened?

That is an interesting question. At form_open time, the form has been referenced by the Access GUI code but none of the controls, sections, or form child window have yet come into existence. In essence, the GUI has simply located the form's data within the FE "container" and is checking to see if you have special instructions for it.

In the form LOAD event, it has read the form's data that describes controls and background but has yet to give it to the graphics processor that is part of the window drawing code (of Windows, not of Access).

I think that visibility doesn't occur until the Activate event. The Form_Current event is where the GUI actually looks at the .Recordsource and .Controlsource and .Rowsource items to see about "filling in" the controls.

Think of these events as Access saying to you, "OK, I've prepared to do something to this form. Did you have anything special to do here?"
 

shrove86

New member
Local time
, 19:18
Joined
Oct 2, 2018
Messages
5
Ok I misunderstood the order. I believed Load to come before Open. Makes sense now why you would want to "close"(or actually cancel) it in open form.

My code carries a first and last name from the 1st form to the 2nd. I have a question for the user where if the name is misspelled, they can cancel the action. If the name gets filled in on the 2nd form, a new record is created.

The way I have written my code, it would work in either Open or Load. Maybe if I get the chance I will go back and move it to Open and run it there instead.

Thanks for the teaching moments.
 

Users who are viewing this thread

Top Bottom