inkineu
asked on
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?
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
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
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.S etFocus
Me!SubFormName.Visible = False
End sub
PRivate Sub Subform_Exit(Cancel as Integer)
Me!SomecontrolOnMainForm.S
Me!SubFormName.Visible = False
End sub
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
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
ASKER
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.
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>.SetFo cus
Forms!<MainFormName>!txtFa ke.SetFocu s
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.
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>.SetFo
Forms!<MainFormName>!txtFa
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.
ASKER
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.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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"