Full Version: How To Open A Specific Page On A Tab Control Which Exists On Another Form
UtterAccess Forums > Microsoft® Access > Access Forms
AccessToSql
Hello,
I want to use a command button on a form to open the first page on a TabControl named TabControlRoutes which is on another form called frmSearchAirport

i tried the code below but its not working.

CODE
DoCmd.OpenForm [frmSearchAirport]!TabControlRoutes.Pages (0), acNormal


Thank you
Doug Steele
You can't OpenForm in that manner.

What you could do is pass the page number as an OpenArgs value, then have frmSearchAirport check whether an OpenArgs value was passed and act accordingly.
AccessToSql
Thanks, I dont understand OpenArgs. Im new to VBA but ill look into it. Thanks for reply
Doug Steele
OpenArgs is really just a "do whatever you want it to" argument: you pass any literal you want.

Use something like the following for the Load event of frmSearchAirport:

CODE
Private Sub Form_Load()

  If IsNull(Me.OpenArgs) = False Then
    Me.tabExample = Me.OpenArgs
  End If
  
End Sub


Open the form using something like:

CODE
  DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=0
AccessToSql
Thanks very much for providing the code.

I also have other command buttons that will need to open the other pages of the same Tab Control named TabControlRoutes

For example I will need to open pages 0,1,2,3,4 and 5 on the Tab Control using different command buttons to open each page.

Is the code below the correct way to include the different pages in the OpenArgs

CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=0 & =1 & =2 & =3 & =4 & =5


Would the code below have to be altered for the code to support support multiple OpeningArgs.

CODE
Private Sub Form_Load()

  If IsNull(Me.OpenArgs) = False Then
    Me.tabExample = Me.OpenArgs
  End If
  
End Sub
Doug Steele
Recognize that you can only open the form to one page on the tab, not multiple! That means that, while OpenArgs can be used to pass anything, it does not make sense in this case to pass more than one value.

In other words, if you wanted the form to open on the second pages of the tab, you'd use

CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=1


and if you wanted the form to open on the fourth page of the tab, you'd use

CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=3


The code in the Load event of the target form would not need to change.
AccessToSql
Im not trying to open multiple pages at the same time.

Whatever command button is pressed will open one page only.

Because the On Load event applies to the whole form i wanted to know how to write the code so that it would it would only open on whatever page was associated with a particular command button

edit: i re read your post and understand it now. thanks
moke123
technically, all the pages of a Tab Control are "Open" when the form loads. You can access control values in other pages even though that tab is not the visible tab. In reality i think you are only setting the focus to that particular page, not opening it per se.
AccessToSql
This worked but I have encountered a problem

I have two Tab Controls on the same form.

Ive used this code on a command button to open the first page on TabControl1
CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=0


and this code is used for the on Load event of the form

CODE
Private Sub Form_Load()

  If IsNull(Me.OpenArgs) = False Then
    Me.TabControl1 = Me.OpenArgs
  End If
  
End Sub


how do i write the code so that the first page on TabControl2 will open when a different command button is pressed because if I use the same code for the command button the first page on TabControl1 will open. if i use the code below for the On Load event how will Access know which tabControl to open

CODE
Private Sub Form_Load()

  If IsNull(Me.OpenArgs) = False Then
    Me.TabControl1 = Me.OpenArgs
      Me.TabControl2 = Me.OpenArgs
  End If
  
End Sub
moke123
if you want to pass 2 values in your openargs you can concatenate them such as below. note the pipe character seperating the 2 values.
CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=0 & "|" & 0


then in your open event use split() to seperate them and pass them using their index.

CODE
Dim Pg As Variant
If Not IsNull(OpenArgs) Then
Pg = Split(OpenArgs, "|")
Me.TabCtl1 = Pg(0)
Me.TabCtl2 = Pg(1)
End If
End Sub


edit: another option is you could use a "select case me.openargs" in your form and pass a single number in the openargs of you button

CODE
select case me.openargs
case 1
Me.TabCtl1 = 0
Me.TabCtl2 = 1

case 2
Me.TabCtl1 = 1
Me.TabCtl2 = 0

case 3
Me.TabCtl1 = 0
Me.TabCtl2 = 0

end select
AccessToSql
Thanks for posting the codes.

I notice that you have 0 and 0 as the openArgs in this code
CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=0 & "|" & 0


but in the code for the Form load event you have
CODE
Me.TabCtl1 = Pg(0)
Me.TabCtl2 = Pg(1)


why are the page numbers different than the OpeningArgs in the two pieces of code
moke123
check out the split function. this could explain it better than i can. Split()

OpenArgs:=0 & "|" & 0 could also be OpenArgs:=1 & "|" & 0 or any 2 numbers you want to pass. they are separated by a delimiter, in this case the Pipe Character.

pg(0) and pg(1) are the result of the split - pg(0) or the 1st element in the array and pg(1) the 2nd element in the array. It is zero based. (n) shows the position in the array.

sorry pg is the first thing to come to mind for a variant name when writing it, didn't think it may be confusing.
tina t
QUOTE
while OpenArgs can be used to pass anything, it does not make sense in this case to pass more than one value.

i'm with Doug on this one. also, fyi, you might find it easer to use an Option Group control, rather than creating and adding code to multiple command buttons. it's easy to make the option controls inside the option group look like command buttons - just use toggle buttons. set the first option control's OptionValue property to 0, the second to 1, etc, etc. then you'd only need one line of code, in the option group control's Click event, as

DoCmd.OpenForm FormName:=frmSearchAirport, View:= acNormal, OpenArgs:=Me!MyOptionGroupControlName

instead of needing a line of code behind each of several command buttons, individually. replace MyOptionGroupControlName with the correct name of the option group control in your form, of course.

hth
tina
moke123
hi tina,
the op's requirement changed since Doug's post. Originally he had one tabcontrol but now wishes to set focus on 2 seperate tabcontrols on the form. Thus the need to either pass 2 values from the calling form or resolve it in the load of the form with the 2 tabcontrols.
edit: then again if its his intention to always set the focus to the same page in both tabcontrols, he'll only need to pass one value.
AccessToSql
Thanks Tina for the idea of the option group, it might be a lot cleaner and require less code. Ill create another form later and compare them.
Thanks moke123 for the Split function explanation. Ive grasped that now but id like to pass just one OpenArg.

Ideally I want to pass just one OpenArg and then that page on the desired TabControl would open.
I thought I'd pass one OpenArg which would open the same page on both TabControls and then hide the unrequired TabControl by making it invisible.

I wrote this code but i get an error saying the action or method requires a form name argument
CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False


I used this code for the On Load event. Do I need to still include the Split function.
CODE
Private Sub Form_Load()

  If IsNull(Me.OpenArgs) = False Then
    Me.TabControl1 = Me.OpenArgs
      Me.TabControl2 = Me.OpenArgs
  End If
  
End Sub
moke123
try
Forms![frmSearchAirport].Form.TabControl1.Visible = False

if your only passing the same value in openargs for both tabcontrols you should be good as written
AccessToSql
I tried that Moke but i got the same error.

When I hit Debug this part was in yellow
CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:=acNormal, OpenArgs:=0


I changed the code by deleting this Forms![frmSearchAirport].Form.TabControl1.Visible = False and wrapping the form name in Quotes. The code below opens the form on the correct page
QUOTE
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0


however when i add the next piece of code i now get an Application Defined or Object Defined error
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].Form.TabControl1.Visible = False
Doug Steele
Try removing the .Form.

The first error you got was because it though frmSearchAirport was a variable in the statement

CODE
DoCmd.OpenForm FormName:=frmSearchAirport, View:=acNormal, OpenArgs:=0


The fact that you didn't get a compile error indicates that you have not told Access to ensure that all variables are declared. To do that, select Tools | Options from the menu in the VB Editor, and ensure Require Variable Declaration is checked on the Editor tab. That will ensure that the line Option Explicit is automatically inserted at the beginning of each module. Unfortunately, you'll have to go into each existing module and add that line yourself. Note that you may have many, many errors that need to be corrected once you do this, but the one-time pain is worth the long-term gain.
AccessToSql
I already have Require Variable Declaration checked in the Editor Tab.

i removed .Form and am still getting the error. When i click Debug this part is in yellow
CODE
Forms![frmSearchAirport].TabControl1.Visible = False


This is the code that i used
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False


The form is actually opening on the correct page but the other TabControl is still visible and the pop up error message appears saying Application Defined or Object Defined error
moke123
check the names of your objects. the default name of a tabcontrol will be like TabCtl2. i noticed you first named TabControlRoutes and then TabControl1 & 2. if they dont match or are spelled wrong it will throw that error.
AccessToSql
Thanks Moke, it works perfect now.

I was using TabControl1 and TabControl2 in the later posts for simplicity so that people following the thread could distinguish them.

When i used the code in my database i forgot to switch the names back and was using
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControl1.Visible = False


instead of this
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControlFares.Visible = False

many thanks to all for all your help. im new to VBA and while its amazing what you can do with it, i find its very time consuming correcting pieces of code
AccessToSql
Ive encountered another problem

When some command buttons are pressed i get an error message saying you cant hide a control that has the focus

so I added a text box named txtBoxFocus to the form and made it invisible. i want to set the focus to this text box before making the unrequired TabControl invisible

I wrote the code below but i get an error saying Microsoft cant move the focus to the control txtBoxFocus

CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport]!.txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlFares.Visible = False


ive also tried writing it this way to but i get an error saying Microsoft cant find the referenced form

CODE
Forms![frmSearchAirport]!.txtBoxFocus.SetFocus

DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport].TabControlFares.Visible = False
Doug Steele
You cannot set focus to a control that isn't visible. Since the purpose of setting focus is to draw attention to the control, that makes sense. (You also cannot set focus to a control that has its Enabled property set to False)

Your second code sample appears to be trying to set focus to the text box before the form has been opened. That's not possible: the form has to be open in order to set focus to a control on it. (In fact, the form's Load event has to have completed before you can set focus to a control).

While SetFocus Method is from Access 2003, I couldn't find a comparable entry for a more recent version. However, things have not changed.

Why not simply set focus to a command button on the form (or just make sure your tab control isn't the first control to receive focus when the form opens)?
AccessToSql
I changed the text Box to visible and changed its height and width to zero. I also have the text box as the first tab stop on the form

This code worked for opening a page on TabControlRoutes
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport]!.txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlFares.Visible = False


but when i need to open a page on TabControlFares this code doesnt work. Instead the form opens but is completely blank. there are no error messages with this code
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms![frmSearchAirport]!.txtBoxFocus.SetFocus
Forms![frmSearchAirport].]TabControlRoutes.Visible = False
Doug Steele
Are you sure you've copied-and-pasted that from your code? There are several syntactical errors that wouldn't let you even run the code!
moke123
typos?
Forms![frmSearchAirport].]TabControlRoutes.Visible = False
Forms![frmSearchAirport]!.txtBoxFocus.SetFocus
AccessToSql
sorry, i made the typos when i tried to make parts of the code appear in bold and then deleted using the backspace key

this code works for TabControlRoutes
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms!frmSearchAirport!txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlFares.Visible = False


this code doesnt work for TabControlFares
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms!frmSearchAirport!txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlRoutes.Visible = False


i think whats happening is that after opening a page on TabControlRoutes and then closing the form the TabControlFares remains invisible when the form is opened again by trying to open a page on TabControlFares and becuase the command button sets TabControlRoutes to invisible the whole form appears blank
Doug Steele
Any chance of zipping up a small version of the database so we can take a look at it? Remove any personal data, but make sure the form still opens (and that the problem still occurs!) Don't forget to give instructions of what we need to do to see the problem.
moke123
you dont by chance have an acSaveYes in your form closing code, do you?
AccessToSql
QUOTE
Any chance of zipping up a small version of the database so we can take a look at it? Remove any personal data, but make sure the form still opens (and that the problem still occurs!) Don't forget to give instructions of what we need to do to see the problem.


Ill do this later if I get the time

QUOTE
you dont by chance have an acSaveYes in your form closing code, do you?


if you mean do i have acSaveYes in the Forms On Close Event , no i dont have.
I do have command buttons on each page which close Queries that are run from the pages and they contain acSavePrompt. I closed the form using the X button on the Form. One code is below.
CODE
Private Sub cmdCloseRouteIDQuery_Click()
DoCmd.Close acQuery, "qryRouteID", acSavePrompt
End Sub


I noticed when I opened the Form directly it only displays TabControlRoutes. I think whats happening is that after making TabControlFares invisible, it is staying invisible when the form is closed even when this code is run to open a page on it
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms!frmSearchAirport!txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlRoutes.Visible = False


This code then makes TabControlRoutes invisible and so the Form appears completely blank when opened.

Im going to try adding code to the On Close Event making both TabControls visible when the form is closed. that might solve it. Ill post back later when ive done that.
AccessToSql
I added the code below to the On Close Event of the Form and I got the error The expression you entered refers to an Object that is closed or doesnt exist
CODE
Private Sub Form_Close()
Me.TabControlFares.Visible = True
Me.TabControlRoutes.Visible = True
End Sub


Im also getting an error message from the code for the On Load Event when I press one of the command buttons. It says The setting you enteresd isnt valid for this property. This piece of code is in yellow when i Debgug the On Load code below
CODE
Me.TabControlRoutes = Me.OpenArgs


CODE
Private Sub Form_Load()

If IsNull(Me.OpenArgs) = False Then
    
  Me.TabControlFares = Me.OpenArgs
  Me.TabControlRoutes = Me.OpenArgs
  
  End If
  
End Sub
AccessToSql
Ive solved it.

When TabControlFares was made Invisible by the code below its Visible Property had been reset to No on the Property Sheet.
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms!frmSearchAirport!txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlFares.Visible = False


I changed the Visible property back to Yes and now this code works correctly
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=0
Forms!frmSearchAirport!txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlRoutes.Visible = False


QUOTE
Im also getting an error message from the code for the On Load Event when I press one of the command buttons. It says The setting you enteresd isnt valid for this property. This piece of code is in yellow when i Debgug the On Load code below
CODE
Me.TabControlRoutes = Me.OpenArgs

The reason I got this error message is because the two TabControls did not have the same number of pages.
TabControlFares has five pages while TabControlRoutes has only four pages.Therefore when 4 was selected for the OpenArg in order to open page 5 of TabControlFares it could not open page 5 of TabControlRoutes as it did not exist.
CODE
DoCmd.OpenForm FormName:="frmSearchAirport", View:=acNormal, OpenArgs:=4
Forms!frmSearchAirport!txtBoxFocus.SetFocus
Forms![frmSearchAirport].TabControlRoutes.Visible = False


I added a Blank page to TabControlRoutes so that it now has five pages and the code now works.

If anyone knows a better way of solving this than adding a blank page let me know. I dont like the idea of having a blank page in the TabControl but its not a major problem.

Hopefully the codes keep working and I wont have to bother you again. Many thanks for your help with this.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.