calling a sub form bounbd to a form (1 Viewer)

kobiashi

Registered User.
Local time
Today, 20:13
Joined
May 11, 2018
Messages
258
hi, ive written an if statement to check if a sub form is open before requering it, but im having an issue with c alling the object, below is my if statement, what is the correct method of calling the sub form inside a form please.


Code:
If CurrentProject.AllForms("forms!FrmManHours.SubfrmManHours").IsLoaded Then
        Forms!FrmManHours.SubFrmManHours.Form.Requery
    Else
    End If
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:13
Joined
May 7, 2009
Messages
19,230
Dim f As Form
On Error Resume Next
Set f = [Forms]![FrmManHours]![SubFrmManHours].Form
IF Err Then
Msgbox "subform Not Open"
End If
Err.Clear
On Error Goto 0
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:13
Joined
Jul 9, 2003
Messages
16,280
The best way to think about it is that the subform isn't actually a subform, it's a normal form housed within a special control called a subform/subreport control. Not many people know about this control because access automatically creates it when you place the "subform" on the main form.

I like to think of this subform/subreport control as a "window" displaying another form.

The other unfortunate thing is by helping you in creating this subform/subreport control, Microsoft gives this intermediary control the same name as the form you are hosting within it, totally confusing you!

To gain access to it with code you call on this subform/subreport control and then you call on its Form property which gives you access to the form contained within it.

Logically, you would think your code should be something like this:-

frmMain.subFormInMainForm.MyTextBox

But that doesn't work because in that particular case the code is referring to a subform/subreport control, with the same name as your subform.

You need to add an extra piece to the code to tell the code to look at the form within the subform/subreport control:-

frmMain.subFormInMainForm.Form.MyTextBox

Although I criticize Microsoft for it's unfortunate choice of name for the subform/subreport control, this approach actually makes sense, as structuring it in this way, a Form with a subform/subreport control, containing another form. A form and Subform Are not just similar items, they are actually identical! The only difference being the intermediary control the "Subform/Subreport Control".

As you can see, there is nothing new to learn about a subform, it's just a Form! Everything you learn to do with a form works with a subform. The only difference is that the subform is contained within a subform/subreport control which affects the way you think about accessing the "Subform". Once you understand this difference, once you acknowledge and understand the existence of the Subform/Subreport Control, then things fall into place.

Microsoft has been very clever in adopting this structure as when you start writing more advanced VBA, you will see that it lends itself to simplifying your VBA code, because you don't need to provide the name of the form within the subform/subreport control.
 
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:13
Joined
Jul 9, 2003
Messages
16,280
More info here:-

 

Users who are viewing this thread

Top Bottom