Making a tab-page invisible on a form

  • Thread starter Thread starter Amit
  • Start date Start date
A

Amit

Access 2K, Win XP
------------------

Hi,

I'm having problem making a tab page invisible on a form
when I move to a new record from an existing record.

My form has a tab which has 2 pages, each having a
different subform. Based on the value selected in a combo-
box on the main form, I make the appropriate page visible
and the other invisible.

It's working fine, except when I move to a new record from
an existing record. In that instance, the tab page from
the previous record is visible. I'd like both pages of the
tab to be invisible when I move to a new record.

Here's the code:
======================================
Private Sub Form_Current()
If Me.NewRecord = True And IsNull(Me.cboPayeeType) Then
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = False
End If

If Me.cboPayeeType = 1 Then
Me.TabCtl10.Pages(0).Visible = True
Me.TabCtl10.Pages(1).Visible = False
End If
If Me.cboPayeeType = 2 Then
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = True
End If
=========================================
Private Sub cboPayeeType_AfterUpdate()
If Me.cboPayeeType = 1 Then
Me.TabCtl10.Pages(0).Visible = True
Me.TabCtl10.Pages(1).Visible = False
End If
If Me.cboPayeeType = 2 Then
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = True
End If
End Sub
=========================================

Any help will be appreciated.

Thanks!

-Amit
 
Hello Amit,

You are enabling one of the tabs again as cboPayeeType will still contain a
value. The code below would correct your problem:

Private Sub Form_Current()

' Check to see if on a new record
If Me.NewRecord Then
' Clear the combobox
Me.cboPayeeType = ""
End If
' Check the value of the combobox
Select Case Me.cboPayeeType
Case 1
' Enable the first page
Me.TabCtl10.Pages(0).Visible = True
Me.TabCtl10.Pages(1).Visible = False
Case 2
' Enable the second page
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = True
Case Else
' No value has been selected yet
' Disable both
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = False
End Select

End Sub

Private Sub cboPayeeType_AfterUpdate()

' Check the value of the combobox
Select Case Me.cboPayeeType
Case 1
' Enable the first page
Me.TabCtl10.Pages(0).Visible = True
Me.TabCtl10.Pages(1).Visible = False
Case 2
' Enable the second page
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = True
Case Else
' No value has been selected yet
' Disable both
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = False
End Select

End Sub

Instead of having two If statements I have used a Select Case statement
instead (nearly the same thing but can check for more values). The last
thing that I would suggest is if you notice, the select case statements in
both the AfterUpdate and Current events are identical (so were your if
statements). It would be easier to move this into a Sub on it's own and then
if it needed to be updated, you would only have to update it once (less
error prone this way). Instead of using the above you would use:

Private Sub Form_Current()

' Check to see if on a new record
If Me.NewRecord Then
' Clear the combobox
Me.cboPayeeType = ""
End If
' Call procedure to update tab control pages
Call UpdateTabCtlPages

End Sub

Private Sub cboPayeeType_AfterUpdate()

' Call procedure to update tab control pages
Call UpdateTabCtlPages

End Sub

Private Sub UpdateTabCtlPages()

' Check the value of the combobox
Select Case Me.cboPayeeType
Case 1
' Enable the first page
Me.TabCtl10.Pages(0).Visible = True
Me.TabCtl10.Pages(1).Visible = False
Case 2
' Enable the second page
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = True
Case Else
' No value has been selected yet
' Disable both
Me.TabCtl10.Pages(0).Visible = False
Me.TabCtl10.Pages(1).Visible = False
End Select

End Sub

As you can see, this way is a lot easier to understand and should do the
trick.

HTH,

Neil.
 
Back
Top