How to dynamically load, access and unload subforms in microsoft access

Kobojunkie picture Kobojunkie · Aug 9, 2012 · Viewed 11k times · Source

I am trying top transition from ASP.NET to programming in access and I am used to thinking in terms of Usercontrols when I think of subforms in Access. What I would like to do is allow user to click a button to load a subform that contains controls user can enter additional data into. I would appreciate any information or resource that would help me with understanding how this is done in MS Access -- how to load, unload and access data in the subforms as well. Thanks in advance

Answer

Albert D. Kallal picture Albert D. Kallal · Aug 9, 2012

Well, as a general rule such loading of sub forms is done automatic and does not require any coding on your part. So you in general are best not to worry about this issue and save all that coding time for helping the poor and needy in your neighborhood.

I should however point out that if you building web forms in Access, then the sub forms are dynamic loaded ONLY when they are used. So if you place a sub form behind a tab control, then the resulting web form (they are XAML forms when published and form code is converted into JavaScript), then the sub form is dynamic loaded for you into the browser. No doubt this setup does result in web forms loading a lot faster. So for web forms, such loading is dynamic and on demand for Access Web forms.

However, my guessing here is you talking about Access client forms and not Access web forms. Given that case, on the client side, load time is rather fast and is quite rare to worry or need or waste developer time on doing this.

However, there are some cases, such having to load say 5 sub forms, and such time can start to add up to the point where a user might start to notice a delay. In this case, you can dynamic load a sub form, and you do this by setting the source object property of sub-form object.

So keep in mind that a sub form is only a "control" and is not tied to an actual form. IN most Access applications I seen the name of this sub form control is the same name as the sub form, but it certainly does not have to be.

So, to dynamic load a sub form, such as when changing to a different tab on a form, the code looks like this:

  Private Sub TabCtl2_Change()

     If Me.TabCtl2.Value = 1 Then
        If Me.frmListContacts.SourceObject = "" Then
           Me.frmListContacts.SourceObject = "frmListContacts"
        End If

  End Sub

So, if you place a sub form control on a form, but leave the source object setting blank, then no form will be loaded or display for that sub form. And in the above, once I loaded the form, then the source object setting will not be blank, and thus I don't attempt to set/load the sub form more than once.

As noted, in the vast majority of cases when loading a form, you need that sub form to load and display anyway, so in most typical applications such above code is not required.