Trying to hide tab controls based on check box

T

Tracey

I have a main form with 5 tab controls each containing a subform. On my main
form I have a checkbox called HDHP. If checked, only one of the 5 tab
controls should display. If unchecked, the remaining 4 tab controls should
display and the 5th tab control should be invisible. My code results in the
error "can't hide control that has the focus". I have the code on "On
Current" for the form and on the "AfterUpdate" for the checkbox. Using Access
2003.

Private Sub Form_Current()
TogglePage
End Sub

Private Sub TogglePage()
If Me.HDHP_Check = True Then
Me.TabCtl56.Pages(0).Visible = False
Me.TabCtl56.Pages(1).Visible = False
Me.TabCtl56.Pages(2).Visible = False
Me.TabCtl56.Pages(3).Visible = False
Me.TabCtl56.Pages(4).Visible = True
Else
Me.TabCtl56.Pages(0).Visible = True
Me.TabCtl56.Pages(1).Visible = True
Me.TabCtl56.Pages(2).Visible = True
Me.TabCtl56.Pages(3).Visible = True
Me.TabCtl56.Pages(4).Visible = False

End If
End Sub

How do I correct this error? Thanks.
 
D

Douglas J Steele

You have to ensure that focus isn't set to the control that's going to be
made invisible.

Try:

If Me.HDHP_Check = True Then
Me.TabCtl56.Pages(4).SetFocus
Me.TabCtl56.Pages(0).Visible = False
Me.TabCtl56.Pages(1).Visible = False
Me.TabCtl56.Pages(2).Visible = False
Me.TabCtl56.Pages(3).Visible = False
Me.TabCtl56.Pages(4).Visible = True
Else
Me.TabCtl56.Pages(0).SetFocus
Me.TabCtl56.Pages(0).Visible = True
Me.TabCtl56.Pages(1).Visible = True
Me.TabCtl56.Pages(2).Visible = True
Me.TabCtl56.Pages(3).Visible = True
Me.TabCtl56.Pages(4).Visible = False
End If

(Actually, I'm not 100% certain you can set focus to a tab page. You might
have to pick a control such as a text box or command button and set focus
there)
 
B

Brendan Reynolds

You need to set focus to a control that will remain visible. Just before you
change the Visible property ...

Me!SomeControl.SetFocus

.... where "SomeControl" is the name of the control to which you want to set
focus.
 
T

Tracey

That worked perfectly- thank you!

Douglas J Steele said:
You have to ensure that focus isn't set to the control that's going to be
made invisible.

Try:

If Me.HDHP_Check = True Then
Me.TabCtl56.Pages(4).SetFocus
Me.TabCtl56.Pages(0).Visible = False
Me.TabCtl56.Pages(1).Visible = False
Me.TabCtl56.Pages(2).Visible = False
Me.TabCtl56.Pages(3).Visible = False
Me.TabCtl56.Pages(4).Visible = True
Else
Me.TabCtl56.Pages(0).SetFocus
Me.TabCtl56.Pages(0).Visible = True
Me.TabCtl56.Pages(1).Visible = True
Me.TabCtl56.Pages(2).Visible = True
Me.TabCtl56.Pages(3).Visible = True
Me.TabCtl56.Pages(4).Visible = False
End If

(Actually, I'm not 100% certain you can set focus to a tab page. You might
have to pick a control such as a text box or command button and set focus
there)
 
T

Tracey

Thank you for the help. It works now!

Brendan Reynolds said:
You need to set focus to a control that will remain visible. Just before you
change the Visible property ...

Me!SomeControl.SetFocus

.... where "SomeControl" is the name of the control to which you want to set
focus.
 
T

Tracey

I spoke too soon- everything is working great except when I change the combo
box selection that loads this form. The form below is triggered to display
data by a combo box on the main form called PlanName. When I select an
alternate plan to display, the error message "can't hide control that has the
focus" reappears if the record I select has a different checkbox selection.
For example, if the check box on Plan 1 was checked, and then I want to
select Plan 2 which would have the checkbox unchecked, I get the error. I
presume I must put the same code on another event but I don't know which
event and on which form (the form where I select the plan or the form
outlined below). Can you help me out again? Thank you very much.
 
D

Douglas J. Steele

Any place you're setting the visible property of controls, you need to
ensure that you don't set a control to not visible if it has focus.

Do a find on .Visible in your code to determine every place you may need to
change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top