How do I hide a tab if a checkbox is checked

Angel69

Registered User.
Local time
Today, 16:24
Joined
Jun 11, 2013
Messages
86
My tab name is SPA and it is the third tab in my form. I want to hide it if the AddToSPA checkbox is checked. This is what I have but I'm getting error 'Compile error: Method or data member not found"

Code:
Private Sub Form_Load()
If Me.AddToSPA = True Then
    Me.SPA.Pages(2).Visible = True
Else
    Me.SPA.Pages(2).Visible = False
End If
End Sub
 
You just need to refer directly to the specific tab's control name, you don't need to include the overall tab control:

Code:
Private Sub Form_Load()
If Me.AddToSPA = True Then
    Me.Page2.Visible = True
Else
    Me.Page2.Visible = False
End If
End Sub
[code]
 
I just realized AddToSPA checkbox is on page 1. Do I need to do anything different?
 
If I refer to Page 2 it does not work and gives me an error. The code below works but it hides the tab all the time. I need to hide it only if it is not an SPA.

Private Sub Form_Load()
If Me.AddToSPA = True Then
Me.SPA.Visible = True
Else
Me.SPA.Visible = False
End If
End Sub
 
I can't see anything wrong with the code and you could actually shorten your code to ...


Oh, hang on. By using the Form_Load event the control might not be loaded properly yet. Try using Form_Current instead.

Code:
Private Sub Form_Current()
' You can't hide a tab when it holds the focus
  If Me.SPA = 2 Then Me.SPA = 0
  
  ' Adds a default value for a Null field
  Me.SPA.Pages(2).Visible = Nz(Me.AddToSPA, False)
End Sub

You may also need to add code to handle changing the value of the checkbox.

Code:
Private Sub AddToSPA_Click()
  Me.SPA.Pages(2).Visible = Me.AddToSPA
End Sub

ETA: I have attached an example database in Zip file.
 

Attachments

Last edited:
I'm getting a Compile error: Method or data member not found and it's highlighting the Me.SPA text. Any ideas?


ETA: I'm a dummy! I didn't realize SPA was the name of the tabCtl. I go it. Thank you!!
 
Last edited:
Make sure SPA is a control on the Form you have coded it in. Maybe it is a spelling mistake or you are trying to refer to a control that does not even exist.
 

Users who are viewing this thread

Back
Top Bottom