Get name of opening form and pass as variable (1 Viewer)

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
I have a form (frmCalledForm) that can be opened by multiple other forms (frmCalling1, frmCalling2, frmCalling3, etc.).

The calling forms stay open when frmCalledForm is opened.

When I finish entering data on frmCalledForm I want to save and close both frmCalledForm and the form that called it.

Here is the code I currently have on the Unload event of frmCalledForm:

Code:
Private Sub Form_Unload(Cancel As Integer)
    On Error Resume Next
    DoCmd.Close acForm, "frmCalling1", acSaveYes
End Sub

It works great but only for frmCalling1.

How can I set this up so that the code acquires the name of whatever form called frmCalledForm?

Thanks a ton.

Tim
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 12:18
Joined
Jul 9, 2003
Messages
16,271
Post a copy of your database, (remove any confidential information first) and I will add some code that will accomplish this task.

Sent from my SM-G925F using Tapatalk
 

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
Post a copy of your database, (remove any confidential information first) and I will add some code that will accomplish this task.

Sent from my SM-G925F using Tapatalk

Thank You!

the code is the Unload event of frmLineStop.

To test it initially you'll have to start at frmInspectionEvent and choose Mill Inspection as that is currently the only form that has a button to call frmLineStop.
 

Attachments

  • QC DB 3-21-19.zip
    250.7 KB · Views: 62

isladogs

MVP / VIP
Local time
Today, 12:18
Joined
Jan 14, 2017
Messages
18,209
In a standard module, create a public string variable strFormName or similar.
In your 3 calling forms, add a line strFormName =Me.Name just before you open the called form.
Then replace the "frmCalling1" with strFormName in your Unload event
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:18
Joined
May 21, 2018
Messages
8,525
Since it is not dialog, a better way is to simply trap the close event of the other form. This provides a tighter encapsulation of the code. So the calling form simply listens for the other form to close.

Code:
Option Compare Database
Option Explicit
Public WithEvents myFrm As Access.Form
Private Sub Command1_Click()
  DoCmd.OpenForm "frmCalled"
  Set myFrm = Forms("frmCalled")
  myFrm.OnClose = "[Event Procedure]"
End Sub

Private Sub myFrm_Close()
  DoCmd.Close acForm, Me.Name
End Sub
 

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
Since it is not dialog, a better way is to simply trap the close event of the other form. This provides a tighter encapsulation of the code. So the calling form simply listens for the other form to close.

Code:
Option Compare Database
Option Explicit
Public WithEvents myFrm As Access.Form
Private Sub Command1_Click()
  DoCmd.OpenForm "frmCalled"
  Set myFrm = Forms("frmCalled")
  myFrm.OnClose = "[Event Procedure]"
End Sub

Private Sub myFrm_Close()
  DoCmd.Close acForm, Me.Name
End Sub

Thanks. I - for the moment have it working with isladogs suggestion (thank you) just because I could immediately wrap my brain around the logic.

But I did just take the time to read up on "Public WithEvents" which I had never seen before I don't think. Looks like a good tool to have in hand. Just so I have asked my obvious inexperienced question, I was trying to figure out where you declared myFrm above and then I realized that "Public WithEvents myFrm As Access.Form" is the actual declaration. That may not seem like a big deal to some but I was pretty proud of grasping that. Feel like I digested a little more useful info.

With that understanding of my comprehension how do I utilize this line of code, particularly what is an example that can replace "[Event Procedure]" in the line myFrm.OnClose = "[Event Procedure]"?

Or.....What exactly happens in that line of code?
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 12:18
Joined
Jul 9, 2003
Messages
16,271
I couldn't work out which forms you want to utilise in your database. So I've done you a simple example:-

You need to add the class module "CallCalled" to your database, and then in the form that you are opening, often called a pop-up form, add the code from form4's open event.

You don't actually need this part of the forms on Open Event, I left it in by way of example:- 'Stop the Form Opening in the Nav Pane

All you need is the Declarations section at the top of the forms code module:-
Code:
'*******************************************************************************************************
'*************************************** Declarations Section  *****************************************
'*******************************************************************************************************

'***** Object (Class) variable to keep the Class open and available for the life of this Form **********
Private CallCalled As clsCallCalled 'this is set in the Forms Open Event

And then in the forms on Open Event:-

Code:
'================== This Code is Generic to any Form using the clsCallCalled ===========================
Set CallCalled = New clsCallCalled 'Set the Class (Object) in the declarations section of this Form

Then set up your unload event like this:-

Code:
Private Sub Form_Unload(Cancel As Integer)
'https://docs.microsoft.com/en-us/office/vba/api/access.docmd.close

    If CallCalled.fMainFormLoaded Then
        DoCmd.Close acForm, CallCalled.prpMainFormName, acSaveYes
    End If
End Sub

You can see a working example in the attached zip file:- Get name of opening form.zip
 

Attachments

  • Get name of opening form.zip
    55.4 KB · Views: 64
Last edited:

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
In a standard module, create a public string variable strFormName or similar.
In your 3 calling forms, add a line strFormName =Me.Name just before you open the called form.
Then replace the "frmCalling1" with strFormName in your Unload event


Thanks!

I ended up using this. Straight ahead and clear. I learned a lot and my limited amount of knowledge with VBA just expanded.

Thanks again.
 

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
I couldn't work out which forms you want to utilise in your database. So I've done you a simple example:-

You can see a working example in the attached zip file:- Get name of opening form.zip

Wow....Thanks!

And yet another useful way to "skin the cat." Ya'll are really helpful.

I'm learning a ton and I appreciate it very much.

Tim
 

isladogs

MVP / VIP
Local time
Today, 12:18
Joined
Jan 14, 2017
Messages
18,209
On behalf of all of us, you're welcome.
Its always good seeing a variety of solutions

As you've gone for my approach, you might want to add the line strFormName = "".
The easiest way would be to put immediately after the line DoCmd.Close acForm, strFormName.
If that causes issues, then in the Form_Load events of frmCalling1/2/3
 

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
On behalf of all of us, you're welcome.
Its always good seeing a variety of solutions

As you've gone for my approach, you might want to add the line strFormName = "".
The easiest way would be to put immediately after the line DoCmd.Close acForm, strFormName.
If that causes issues, then in the Form_Load events of frmCalling1/2/3

What does that line accomplish?
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:18
Joined
Sep 21, 2011
Messages
14,223
MajP,

Does all that code go in the calling form?

Since it is not dialog, a better way is to simply trap the close event of the other form. This provides a tighter encapsulation of the code. So the calling form simply listens for the other form to close.

Code:
Option Compare Database
Option Explicit
Public WithEvents myFrm As Access.Form
Private Sub Command1_Click()
  DoCmd.OpenForm "frmCalled"
  Set myFrm = Forms("frmCalled")
  myFrm.OnClose = "[Event Procedure]"
End Sub

Private Sub myFrm_Close()
  DoCmd.Close acForm, Me.Name
End Sub
 

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
As you've gone for my approach, you might want to add the line strFormName = "".
The easiest way would be to put immediately after the line DoCmd.Close acForm, strFormName.
If that causes issues, then in the Form_Load events of frmCalling1/2/3

Does that clear the variable in the one instance and makes sure it is clear in the other instance?
 

isladogs

MVP / VIP
Local time
Today, 12:18
Joined
Jan 14, 2017
Messages
18,209
Does that clear the variable in the one instance and makes sure it is clear in the other instance?

It was just for tidying up.
If you call the variable again from another form the value is automatically overwritten. But if you don't its hanging around in memory.
That will only be an issue if you decide to use the same variable again elsewhere and wonder why its giving unexpected results
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:18
Joined
May 21, 2018
Messages
8,525
With that understanding of my comprehension how do I utilize this line of code, particularly what is an example that can replace "[Event Procedure]" in the line myFrm.OnClose = "[Event Procedure]"?

Or.....What exactly happens in that line of code?

In general when you are building an event procedure on a form you hit the pull down and bring in the word "[Event Procedure]" or choose the ellipsis and choose Macro, Event Procedure, or Expression builder. If you choose something to go into that property it tells the class to Raise the event. Think of it like an announcement. If it is the onclose property, you told the form to announce that it has closed by writing to a memory location. The procedure you write usually called an event procedure or event handler is not "called' by the event, the procedure "listens" for the event to get raised. If the called form had "[Event Procedure]" in the onclose property it would already raise the event. But if it did not have anything in that property then the called form would close and not announce (raise the event) that it closed. This line of code ensures that the called form will raise the onclose event, and the calling form can then listen (handle) for it.
 

Zydeceltico

Registered User.
Local time
Today, 07:18
Joined
Dec 5, 2017
Messages
843
In general when you are building an event procedure on a form you hit the pull down and bring in the word "[Event Procedure]" or choose the ellipsis and choose Macro, Event Procedure, or Expression builder. If you choose something to go into that property it tells the class to Raise the event. Think of it like an announcement. If it is the onclose property, you told the form to announce that it has closed by writing to a memory location. The procedure you write usually called an event procedure or event handler is not "called' by the event, the procedure "listens" for the event to get raised. If the called form had "[Event Procedure]" in the onclose property it would already raise the event. But if it did not have anything in that property then the called form would close and not announce (raise the event) that it closed. This line of code ensures that the called form will raise the onclose event, and the calling form can then listen (handle) for it.

Got it. The "announcement" analogy in order to "handle" (which I read about earlier) works for me.

Thanks
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 12:18
Joined
Jul 9, 2003
Messages
16,271
Thanks Uncle G! But I have it working now. I hope you didn't spend too much time.

Tim

I think You will find the main advantage of my solution is that it works on subforms. In other words if you place a command button on a subform and pop open a pop-up form, then my solution will close the Pop-Up form. I don't think the other two Solutions will, although I haven't tested them. The example file I provided has got a form with a subform on it so that you can test the code when called from a subform...

Edit:-
For clarity, the subform is on form1, So there are two command buttons, one on form1, and one on the subform containe in form1
 
Last edited:

Users who are viewing this thread

Top Bottom