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.

Shadi SalehAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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?
Dale FyeOwner, Developing Solutions LLCCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shadi SalehAuthor Commented:
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, Developing Solutions LLCCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.