Solved Access VBA - Multiple subform instances, call procedures from different form

ironfelix717

Registered User.
Local time
Today, 11:34
Joined
Sep 20, 2019
Messages
193
Hi,

I once saw a popup form being used like a class and multiple instances of that form could be instanced.
This is a bit different from my question, but I have a feeling there are ties.

I have 2 subforms on a main form. Both subform controls have the same source form.
I would like to call a routine inside the subform's source form and have that only affect a specific subform control...

Better example:
Main Form = "Main Form"
Subform Name = "Form2"
Subform control 1 = MainForm.sfrm1
Subform control 2 = MainForm.sfrm2
Routine to call: Form2.LoadData(args)

I need a way to call Form2.LoadData(args) and have that only apply to MainForm.sfrm1 control.
Then call Form2.LoadData(different args) and have that apply only to MainForm.sfrm2.

Allegedly there is a way to call a routine from a subform object via its form property, but this is not working apparently: DOCs

See attached for a real example.


Best Regards
 

Attachments

Code:
Private Sub cmd1_Click()
  Dim frm As Form_Form2
  Set frm = Me.sfrm1.Form
  frm.LoadData "This is a header, this is a label"
End Sub
Private Sub cmd2_Click()
  Dim frm As Form_Form2
  Set frm = Me.sfrm2.Form
  frm.LoadData "This is a Plane, this is a Car"

End Sub
Code:
Public Sub LoadData(TheData As String)
  Me.lbHeader.Caption = Split(TheData, ",")(0)
  Me.lbLabel.Caption = Split(TheData, ",")(1)
End Sub
 
FYI, I added the variables for clarity but simply
me.sfrm2.form.loaddata "some data"
would work
 
@MajP
@arnelgp

Two unique and awesome solutions. I find MajP's solution more flexible, but appreciate both answers.
Thanks a lot!
 

Users who are viewing this thread

Back
Top Bottom