How to ensure I go back to the right form

Nightowl4933

Tryin' to do it right...
Local time
Today, 06:47
Joined
Apr 27, 2016
Messages
151
Hi,

I have a form (frmZ) that can be opened from one of several forms (frmA, frm B, etc.)

When I close frmZ I want to goback to whichever form it was opened from, but (as usual) I'm struggling :o

I was thinking of using the CommandButton OnClick event to get the form name, but I don't know what to do with it then.

Thanks,

Pete
 
Use the OpenArgs property to pass the name of the calling form in to form z
 
when you open the form using docmd.openform add the name of the form as its OpenArgs parameter:

docmd.OpenForm formname:="frmZ",OpenArgs:=Me.Name

and on frmZ unload event, test this value if it is not blank and set focus to that form:

Private Sub Form_Unload(Cancel As Integer)
If Me.OpenArgs & "" <> "" Then
Forms(Me.OpenArgs & "").SetFocus
End Sub
 
Thanks arnelgp, but can I use that and use a criteria argument, as well?

Pete
 
ofcourse, by all means:

docmd.OpenForm formname:="frmZ",OpenArgs:=Me.Name,WhereCondition:="your Criteria here"
 
Got it, thanks.

I'm probably missing something here, but how do I get back to the original form?

I would use the On Click event procedure for th CommandButton, so is this where I'd use String from the OpenArgs?

Thanks
 
yes on the Click event of the commandbutton.

to go back to the calling form, add the code i gave you when frmZ unloads.
 
Sorry, but I'm definitely missing the point here.

If I'm adding the code to the Unload event procedure, how does that tie in with the Click event of the CommandButton on frmZ?

Thank you (and for your patience!)
 
what does the botton do, close the form, the unload event is called everytime you close a form.
 
I think your question's rhetorical, but let me explain a little better and see if I am getting...

I can open frmZ from both frmA and frmB. To ensure I go back to frmA or frmB when I've finished with frmZ I need to use the OpenArgs function.

To do this, I add OpenArgs to the end of the DoCmd instruction, like this...

DoCmd.OpenForm "frmZ", , , strSearchString, , , OpenArgs: Me.Name

...from the CommandButton on both frmA and frmB.

In frmZ, I add the the following to the On Unload [Event procedure]...

Private Sub Form_Unload(Cancel As Integer)
If Me.OpenArgs & "" <> "" Then
Forms(Me.OpenArgs & "").SetFocus
End Sub

...but what I don't understand is what triggers the return to the other forms. Surely I must put something in the Click event for the CommandButton, or do I just close the current form, e.g. DoCmd

Thank you, you lovely patient people :-)

Pete
 
Last edited:
Erm, I think the answer to my question is 'Yes'? It seems to work when I Close the form.

Silly me!
 

Users who are viewing this thread

Back
Top Bottom