Solved Access VBA - Multiple subform instances, call procedures from different form (1 Viewer)

ironfelix717

Registered User.
Local time
Today, 07:55
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

  • Example1.accdb
    368 KB · Views: 154

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:55
Joined
May 21, 2018
Messages
8,463
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:55
Joined
May 21, 2018
Messages
8,463
FYI, I added the variables for clarity but simply
me.sfrm2.form.loaddata "some data"
would work
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:55
Joined
May 7, 2009
Messages
19,175
see also this demo.
 

Attachments

  • Example1 (2).accdb
    640 KB · Views: 186

ironfelix717

Registered User.
Local time
Today, 07:55
Joined
Sep 20, 2019
Messages
193
@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

Top Bottom