We help IT Professionals succeed at work.

MS Access: Sub Form Visibility Control

I have a main form ("MF") with a big number of controls on it including one sub form. This sub form named "SF" and is related with two of the controls in the main form named "C1" and "C2".

When form is loaded SF is not visible.

When C1 or C2 got focus SF visibility set to True.

Now what I need is to set SF visibility to False if  C1 or C2 lost focus, but the new focused control is not SF.

It seemed simple at the begining and it seemed straight forward, but it is not.

I used

Private Sub C1_GotFocus()
    Me.SF.Visible = True
End Sub

Private Sub C2_GotFocus()
    Me.SF.Visible = True
End Sub

Private Sub C1_LostFocus()
    Me.SF.Visible = False
End Sub

Private Sub C2_LostFocus()
    Me.SF.Visible = False
End Sub

This works great for all controls on the MF (Main Form).

The problem is that applies  SF(Sun Form) too while I want to work on it.

SF is a list that I can pick a value from it and put it in the C1 or C2.

Now as SF disappears after C1 or C2 loses control, I cannot pick anything.

a workaround would be to omit the lost focus events for C1 and C2 and setting every other single control on the main form get focus control to hide SF.

While this works as I need. It seems not to be a good solution.

Is there a more straight forward way to accomplish this?

Remember Main form controls are too many too be coded this way.

Watch Question

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014
FWIW: Most users don't like "magic" controls that show/hide based on some unknown action or data state. You're much better off enabling/disabling that subform when you don't want users to interact with it.

LostFocus occurs on C1 and C2 before GotFocus would occur on your Subform control, so I'm not sure how you'd do this. Only way I could see to reliably do this would be to add code to the GotFocus event of all controls on that form to toggle the subform.

But - again - do you really want to do this?
Owner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Replying from iPad, so could not test this.

Select the C1, C2, and SF controls on your form (in design view).  Then set the Tag property of those controls to "SF".

Then select all of the controls on the main form and in the GotFocus property enter:


Then, create a private function in the code behind the form:

Private Function DisplaySF()

    me.SF.Visible = Instr(Screen.ActiveControl.Tag, "SF") > 0
    'or you could ignore the TAG property and try:
'   me.SF.Visible = (Screen.ActiveControl.Name = "C1") OR
'                   (Screen.ActiveControl.Name = "C2") OR
'                   (Screen.ActiveControl.Name = "SF")

End Sub

Open in new window

untested, but this should work.


Thanks LVL49 and LVL85,

Please not that I want to avoid using Get Focus event for all controls. it does not seem to be the right way to do it. I stated in the question that I can do it but I need a different solution :)

Thank you for your contribution anyway.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Not sure why you believe:

"It seems not to be a good solution."

Although I agree with Scott that enabling/disabling the control is more user friendly than hiding it, the GotFocus method is a "good solution" to your requirement.  And, as I recommended in my original post, you don't actually have to add code to the GotFocus event of each control.  You can select all of the controls at once and enter a function call in the GotFocus property of each of the controls

GotFocus: = DisplaySF()

Then create the function which uses the ActiveControl to determine which control has the focus and enable/disable the subform based on that selection.