2
votes

I have a form in MS Access with a Tab Control (called TabCtrl). This control has several pages, each with a subform.

On Form_Open, I want to query the subforms for the total number of records and put that number in the tab's name. For example, the tab named MyTab should become MyTab (2):

Private Sub SetTabName_MyTab()
    Dim i As Integer
    i = CurrentDb.OpenRecordset("Select count(*) from [MyQry];").Fields(0).Value
    TabCtrl.Pages("MyTab").Name = "MyTab (" & i & ")"
End Sub

However, when I run this, the last line returns Run-time error 2136 "To set this property, open the form or report in Design view". Does this mean I can't do this in code? Should I use another Event?

2

2 Answers

3
votes

You are trying to change the internal name of the control, rather than caption on the tab page control.

Try:

ATabcontrol.Pages("MyTab").Caption = "MyTab(" & i & ")"

Note: I have done this before and found issues with the text not changing till the tabcontrol was manipulated.

0
votes

Try:

Dim s as string

'To Get current Tab Caption
s = Me.TabCtl.Pages(Me.TabCtl.Value).Caption

'To Set current Tab Caption
s = "TabName"

Me.TabCtl.Pages(Me.TabCtl.Value).Caption = s