Set Subform Visible property to False upon losing focus

I have setup a calendar form and reference it in other forms as a subform.  I am trying to set the Visible property to 'false' for that subform when the subform loses focus.  I tried the 'On Exit' event for the subform, but it won't allow me to set the Visible property to false as it at this point still has the focus.  What's the best way of handling this, other than setting up 'On Focus' events for all the controls on the main form?    
LVL 1
inkineuAsked:
Who is Participating?
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.

dannywarehamCommented:
You can't make a contol's visible false whilst that control fires the event... I believe that applies to lostfocus too (hence your issue)

Where could you lose focus too? Back to the form?
Have a function in your form module

Function Hide_SubForm()
Me.Subform.Visible = false
End function.

Then, on every control's got focus have "hide_subform"

dannywarehamCommented:
The otehr way is to have a hide/show toggle button.

Private Sub ToggleButton_Click()
If me.toggle = 1 then
   me.subform.visible = false
   me.toggle.caption = "Hide"
   Else
   me.subform.visible = True
   me.toggle.caption = "Show"
end if

kirenievsCommented:
I do not know if it will be an option but you could do like this:

PRivate Sub Subform_Exit(Cancel as Integer)

    Me!SomecontrolOnMainForm.SetFocus
    Me!SubFormName.Visible = False

End sub
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

omgangIT ManagerCommented:
How about a public variable to hold the name of the form that called the calendar

Public strFormName As String

Then, from everywhere you call the calendar subform, assign a value to the variable

strFormName = Me.Name

On the calendar forms OnExit, or Close event call a function, say HideCalendar

Public Function HideCalendar()

      'set focus back to form that called the calendar
   Forms!strFormName.SetFocus
      'hide the calendar form
   Forms!frmCalendar.Visible = False

End Function

This will always return focus back to the form that called the calendar form.
OM Gang
inkineuAuthor Commented:
Is there a way that I can check where the FOCUS has been directed from within the subform ON EXIT Event?  That way I could set the Focus to that control and then set Visible = False.
thenelsonCommented:
This works:

Create a little textbox with length & height = 0.007" (the minimum), transperant background and border in the mainform.  If it is called txtFake then put this in the On Exit event of the subform:
Forms!<MainFormName>.SetFocus
Forms!<MainFormName>!txtFake.SetFocus

In the on enter event of txtFake put:
Me.<SubFormName>.Visible = False

When you click on a control outside of the subform, this is what happens:
1. The on exit event fires, setting focus to txtFake
2. txtFake on enter fires setting subform invisible
3. The focus changes to the control clicked in.
inkineuAuthor Commented:
I don't understand why your step 3. would change the focus back to the control.  In step 1 you change the focus to txtFake.  I would assume this would override the initial click.  Is that right?  I ran a test and the focus remains on txtFake.  
thenelsonCommented:
I guess I was probably double clicking my touch screen when it worked.  I tried it again and the focus remained on txtFake.  But then I tried this several times with several controls and it worked consistantly:

Private Sub <subform Name>_Exit(Cancel As Integer)
Me.TimerInterval = 1
End Sub

Private Sub Form_Timer()
Me.TimerInterval = 0
Me.[Switchboard Items subform].Visible = False
End Sub
thenelsonCommented:
I did find a problem with the above code.  If the focus is in the subform and you click on a control that moves the focus to the subform, you get error # 2165, You can't hide a control that has the focus.  To resolve it:

Private Sub Form_Timer()
Me.TimerInterval = 0
On Error Resume Next
Me.[Switchboard Items subform].Visible = False
If err = 2165 Then err = 0
End Sub

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
inkineuAuthor Commented:
Sorry, I was off for a week.  Thanks for your response.  Seems to work as intended.  Interesting use of the Form_Timer event.  Thank you.
thenelsonCommented:
Glad I could help.  I have used the timer for the on exit procedure before.  It seems that the on exit fires before the actual exit happens.
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.