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

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:25
Joined
May 21, 2018
Messages
8,527
Got it. The "announcement" analogy in order to "handle" (which I read about earlier) works for me.
a custom event makes it easier to see how it works. In the called form I put a combobox with choices red, white, blue. It raises and event when the color changes and passes the color.

Code:
Option Compare Database
Option Explicit
'Called Form
Public Event ColorChange(Color As String)
Private Sub cmboColor_AfterUpdate()
  RaiseEvent ColorChange(Me.cmboColor)
End Sub

The calling form will change color whenever the called form raises the event.

Option Compare Database
Option Explicit

Code:
'Code in frmCalling
Public WithEvents myFrm As Form_frmCalled
Private Sub Command1_Click()
  DoCmd.OpenForm "frmCalled"
  Set myFrm = Forms("frmCalled")
End Sub
Private Sub myFrm_ColorChange(Color As String)
  If Color = "red" Then Me.Detail.BackColor = vbRed
  If Color = "blue" Then Me.Detail.BackColor = vbBlue
  If Color = "white" Then Me.Detail.BackColor = vbWhite
End Sub

The called form knows nothing about which form called it or which event handler traps the event.
 

Zydeceltico

Registered User.
Local time
Today, 12:25
Joined
Dec 5, 2017
Messages
843
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

And I clearly see that happening in the future. Thanks
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 17:25
Joined
Jul 9, 2003
Messages
16,280
And I clearly see that happening in the future. Thanks

The other advantage I should mention is that if somehow the calling form gets closed, now this shouldn't happen, you should always make your pop-up form modal, but sometimes you don't want it to be modal and in that case, if the calling form gets closed for some reason, and then you close the modal form, you get an error. You still get an error message with my method, but it's a handled Error, a message will pop up telling the user that the Calling form has been closed.
 

Users who are viewing this thread

Top Bottom