Adding page to Tab Form Dynamically

Barbados

Registered User.
Local time
Today, 19:57
Joined
May 22, 2013
Messages
27
Hi,

I would like to add a page to a tab form when a certain button is clicked. I don't want to use the visible/hide solution. So I already found out that this can only be done in design view. I am using the following code:

Code:
Private Sub cmd_StrategyAdd_Click()
    DoCmd.OpenForm "Form1", acDesign
    Me.tabMain.Pages.Add
    DoCmd.OpenForm "Form1", acNormal
End Sub

I am getting the error: Run-time error 2467. The expression you entered refers to an object that is closed or doesn't exist.

I am sure the name of my tab form is tabMain. Any idea why that is?

Thanks.
 
Try closing the form, with acSaveYes and then opening it again..
Code:
Private Sub cmd_StrategyAdd_Click()
    DoCmd.OpenForm "Form1", acDesign, WindowMode:=acHidden
    Me.tabMain.Pages.Add
    DoCmd.Close acForm, "Form1", acSaveYes
    DoCmd.OpenForm "Form1", acNormal
End Sub
 
Try closing the form, with acSaveYes and then opening it again..
Code:
Private Sub cmd_StrategyAdd_Click()
    DoCmd.OpenForm "Form1", acDesign, WindowMode:=acHidden
    Me.tabMain.Pages.Add
    DoCmd.Close acForm, "Form1", acSaveYes
    DoCmd.OpenForm "Form1", acNormal
End Sub

Thanks for your reply. However, when debuggin, it seems the error is caused by the line: Me.tabMain.Pages.Add.

Am I missing something?
 
Ooo.. I see the problem.. it should not be Me. as Me. will be for current objects.. you should use,
Code:
[B]Forms!Form1[/B].tabMain.Pages.Add
 
Ooo.. I see the problem.. it should not be Me. as Me. will be for current objects.. you should use,
Code:
[B]Forms!Form1[/B].tabMain.Pages.Add

Thanks for your quick reply again. The same error keeps popping up. I tried

Code:
[B]Forms!Form1[/B].tabMain.Pages.Add
and
Code:
[B]Forms!Form_Form1[/B].tabMain.Pages.Add
and
Code:
[B]Form_Form1[/B].tabMain.Pages.Add

but nothing seems to work.

Clueless!
 
Are you running the code from the same form you are trying to add a page to?
 
Try this:

Code:
DoCmd.OpenForm "Form1", acDesign, WindowMode:=acHidden
Forms("Form1")("TabMain").Pages.Add
DoCmd.Close acForm, "Form1", acSaveYes
DoCmd.OpenForm "Form1", acNormal

Linq ;0)>

Late Note: Just saw that you're trying to do this from the same Form; above example works when doing from a second Form.
 
Last edited:
Try this:

Code:
DoCmd.OpenForm "Form1", acDesign, WindowMode:=acHidden
Code:
[B]Forms("Form1")("TabMain").Pages.Add[/B]
[B]DoCmd.Close acForm, "Form1", acSaveYes[/B]
[B]DoCmd.OpenForm "Form1", acNormal[/B]

Linq ;0)>

Late Note: Just saw that you're trying to do this from the same Form; above example works when doing from a second Form.

Thanks for trying to get me in the right direction. Any idea what need to be done if it is done from the same form, or is it impossible?
 
I spent about two hours working on this and I can't find a way. I even tried opening a secondary form, with the code I posted above in its OnLoad event. If you open the secondary form independently it creates the page on the original form, but it's still a no-go if you open the secondary form from the first form!

I'll work on it some more, later today, but I'm not optimistic!

Why, exactly, are you trying to do this? It really is a most unusual task, as opposed to using the visible/invisible route.

Linq ;0)>
 
I noticed this post and did a little research (older research). From Access Developers Handbook 97 (p 386) it says "you can not add or delete pages at run time". It goes on to say "...
you must create as many tabs as you'll ever need in Design view and then hide the extra tabs as you load the form..." and then "How many Pages can a Tab control support anyway?"

I think he has interchanged the word Tab and Pages, but the bottom line appears that you can not add pages to a tab control at run time.

He does offer a function to adjust the number of pages for a Tab control. Here's a version of the code.
from Access Developers Handbook 97
Code:
Public Sub PrepareTab(ctl As TabControl, ByVal IntPages As Integer)
'
'from Access Developers Handbook 97
'Litman, Getz, Gilbert
'
' This subroutine accepts 2 parameters:
' -- a reference to a Tab Control
' --the number of pages you'd like that Tab control to have
' When this code runs it either removes or adds pages to get the Page count you  
' requested
'
' If you open your form in design view, you can adjust the page count for a given tab 
' control, by using this sort of statement ... to have 5 pages in the tab control
'
' Call PrepareTab(Forms!frmTabYourFormName!yourTabControl, 5)
'
'
Dim pge As Page
Dim intl As Integer
Dim pges As Pages
On Error GoTo Error_Handler
Set pges = ctl.Pages
If IntPages < 1 Then
  Exit Sub
Else
  If IntPages < pges.Count Then
       For intl = pges.Count - 1 To IntPages Step -1
           pges.Remove intl
       Next intl
  Else
       For intl = 1 To IntPages - pges.Count
            pges.Add
       Next intl
       ctl.MultiRow = True
  End If
End If

Exit_Here:
  Exit Sub
  
Error_Handler:      'slight name change here
  Select Case Err.number
   Case Else
      MsgBox "Error:  " & Err.Description & _
      "  (" & Err.number & ")"
  End Select
  Resume Exit_Here
End Sub

However, it's possible that the ability to add or remove Pages from a Tab control has changed in recent versions-But I haven't found any such reference. Advice is to do as in the example, create the form withtab control with all pages, then adjust using the visible property at run time.
 
Last edited:
However, it's possible that the ability to add or remove Pages from a Tab control has changed in recent versions-But I haven't found any such reference. Advice is to do as in the example, create the form withtab control with all pages, then adjust using the visible property at run time.

Hi, thanks for your answer. I am using a workaround now. Right now, my button on Form1 will open a second form (Form2). Some code upon opening Form2 will add the page to the Tabbing Form on Form1 during run-time. Seems to work just fine.
 
As I said, my reference was ADH 97 and things could have changed. I didn't find a technique using Google.
You may want to post your work around code just in case someone else needs it.
 
...Right now, my button on Form1 will open a second form (Form2). Some code upon opening Form2 will add the page to the Tabbing Form on Form1 during run-time...
That's the approach I've been looking at, but could not get to work, yet. Could you post your code that's working?

Linq ;0)>
 
That's the approach I've been looking at, but could not get to work, yet. Could you post your code that's working?

Linq ;0)>

Hi, on Form1 I have a button with the following code:

Code:
Private Sub cmd_Button_Click()
    DoCmd.OpenForm "Form2"
End Sub

I have changed my application slightly, so on Form2 the user has to input various data, among which the name of the page to be added. After the user has done all that, he presses OK and the page is added to Form1. A part of the code underlying that button is:

Code:
Private Sub cmd_Form2_OK_Click()
        DoCmd.OpenForm "Form1", acDesign, WindowMode:=acHidden
        Forms("Form1")("tab_Main").Pages.Add
 
        Forms("Form1")("tab_Main").Pages.Item(2).Name = "Some Name" + CStr(nr_Scenarios + 1)
        Forms("Form1")("tab_Main").Pages.Item(2).Caption = "Some Caption"
 
        DoCmd.Close acForm, "Form1", acSaveYes
        DoCmd.OpenForm "Form1", acNormal
 
        DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
 

Users who are viewing this thread

Back
Top Bottom