Using a button click to display a tab page

B

Bill

I want to use a click of a command button to display or hide tab's in the tab
control.

For example is a user clicks "New" the tab page for "New Record" would
display but hide the other tab pages. If they click "reports" that page
would display only.

I've used it before on a check box by using a if command. In this case,
there is no true value on the button so how would I get this to work?

this is what I've used before with the check box:

Private Sub chkWorkComp_AfterUpdate()
If Me.chkWorkComp = True Then
Me.WCEmployment.Visible = True
Else
Me.WCEmployment.Visible = False
End If
End Sub

How can I get it to work for a onclick function of a button?? Thanks for
the help!
 
D

dymondjack

How about something like


Private Sub btnToggleTabVisibility()

Me.WCEmployment.Visible = _
Iif(Me.WCEmployment.Visible, False, True)

End Sub

--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill
 
B

Bill

Im getting a compile error on the iif statement.


Private Sub Button_AddNewRecord_Click()
Me.AddNew.Visible = _
IIf(Me.AddNew.Visible, False, True)
End Sub
 
D

dymondjack

Make sure you are referring to the Name property, not the Caption. Check
under the 'Other' tab in the page properties and make sure thats what you're
using in the code.

Or, it could be the name AddNew. I'm not positive but I thinkt his may be a
reserved name. Change it to something like tabAddNew (as the Name, not
Caption).

hth
--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill
 
B

Bill

I really appreciate your help. I changed the name to TabAddNew and updated
the script. I still get the compile error at the iif statement and it
highlights the .TabAddNew area.

Private Sub Button_AddNewRecord_Click()
Me.TabNewAdd.Visible = _
IIf(Me.TabNewAdd.Visible, False, True)
End Sub
 
J

JimBurke via AccessMonster.com

That IIf statement will only set the visible property of that one tab, and is
basically just toggling the value of the tab's visible property to the
opposite of what the current setting is. If that's what you want just use
TabNewAdd.Visible = Not TabNewAdd.Visible. Also, you call it TabAddNew in
your description, but in your code you have TabNewAdd.

But based on your description it sounds like you only want one tab visible at
any one time, and want all others not visible. This IIf statement won't do
that. If I'm interpreting correctly, you can have a sub that does somthing
like this (where 'tabName' is passed to the sub and is taken from the name of
the tab that should be set to visible):

Private Sub SetTabVisible(byval tabName as string)

For i = 0 to tabControl.Pages.count - 1
if tabControl.Pages(i).name = tabName then
tabControl.Pages(i).visible = true
else
tabControl.Pages(i).visible = false
end if
Next i

End Sub

'tabControl' would be the name of the tab control itself, not of any of the
individual tabs.

So for each button that you click, assuming it corresponds to one of the tabs,
in that buttons On Click event proc just put

Call SetTabVisible("tabName")

where 'tabname' is the name of the tab you want to be visible (the name of
the tab, not the caption!)
I really appreciate your help. I changed the name to TabAddNew and updated
the script. I still get the compile error at the iif statement and it
highlights the .TabAddNew area.

Private Sub Button_AddNewRecord_Click()
Me.TabNewAdd.Visible = _
IIf(Me.TabNewAdd.Visible, False, True)
End Sub
Make sure you are referring to the Name property, not the Caption. Check
under the 'Other' tab in the page properties and make sure thats what you're
[quoted text clipped - 12 lines]
 
B

Bill

I think you nailed it. I just wish I understood it more! :)

Ok, so I need to create a sub (not sure how or maybe where) which would read
something like this:

Private Sub SetTabVisible(byval TabCntrl_Main as string)

For i = 0 to tabControl.Pages.count - 1
if tabControl.Pages(i).name = TabCntrl_Main then
tabControl.Pages(i).visible = true
else
tabControl.Pages(i).visible = false
end if
Next i

End Sub

Seems like Im missing something...

JimBurke via AccessMonster.com said:
That IIf statement will only set the visible property of that one tab, and is
basically just toggling the value of the tab's visible property to the
opposite of what the current setting is. If that's what you want just use
TabNewAdd.Visible = Not TabNewAdd.Visible. Also, you call it TabAddNew in
your description, but in your code you have TabNewAdd.

But based on your description it sounds like you only want one tab visible at
any one time, and want all others not visible. This IIf statement won't do
that. If I'm interpreting correctly, you can have a sub that does somthing
like this (where 'tabName' is passed to the sub and is taken from the name of
the tab that should be set to visible):

Private Sub SetTabVisible(byval tabName as string)

For i = 0 to tabControl.Pages.count - 1
if tabControl.Pages(i).name = tabName then
tabControl.Pages(i).visible = true
else
tabControl.Pages(i).visible = false
end if
Next i

End Sub

'tabControl' would be the name of the tab control itself, not of any of the
individual tabs.

So for each button that you click, assuming it corresponds to one of the tabs,
in that buttons On Click event proc just put

Call SetTabVisible("tabName")

where 'tabname' is the name of the tab you want to be visible (the name of
the tab, not the caption!)
I really appreciate your help. I changed the name to TabAddNew and updated
the script. I still get the compile error at the iif statement and it
highlights the .TabAddNew area.

Private Sub Button_AddNewRecord_Click()
Me.TabNewAdd.Visible = _
IIf(Me.TabNewAdd.Visible, False, True)
End Sub
Make sure you are referring to the Name property, not the Caption. Check
under the 'Other' tab in the page properties and make sure thats what you're
[quoted text clipped - 12 lines]
IIf(Me.AddNew.Visible, False, True)
End Sub
 
J

JimBurke via AccessMonster.com

The sub would go in the form module. The subroutine argument would be the
name of the tab (not the caption!) that needs to be visible. I think you said
that for each tab, you want a command button that will make that tab the only
one visible. So for example if one of the tabs (not the main tab control
itself, but one of it's tabs) had a name of 'tabA', then the command button
to make only that tab visible would have an On Click event proc that would
just have

Call SetTabVisible("tabA")

Same would go for "tabB" - there would be a corresponding button with On
Click event proc of

Call SetTabVisible("tabB") and on and on, for each tab on the main tab
control.

Hope that explains it.

I think you nailed it. I just wish I understood it more! :)

Ok, so I need to create a sub (not sure how or maybe where) which would read
something like this:

Private Sub SetTabVisible(byval TabCntrl_Main as string)

For i = 0 to tabControl.Pages.count - 1
if tabControl.Pages(i).name = TabCntrl_Main then
tabControl.Pages(i).visible = true
else
tabControl.Pages(i).visible = false
end if
Next i

End Sub

Seems like Im missing something...
That IIf statement will only set the visible property of that one tab, and is
basically just toggling the value of the tab's visible property to the
[quoted text clipped - 45 lines]
 
B

Bill

Perfect. Thank you for all the help and yes I now know the difference
between the name and caption fields :)

Ok so I created a module which is called "SetTabVisible" and contains:

Option Compare Database
Private Sub SetTabVisible(ByVal TabCntrl_Main As String)
For i = 0 To TabControl.Pages.Count - 1
If TabControl.Pages(i).Name = TabCntrl_Main Then
TabControl.Pages(i).Visible = True
Else
TabControl.Pages(i).Visible = False
End If
Next i
End Sub

The main tab control area is named "TabCntrl_Main". Then under the first
button "AddNewRecord" I placed the following code for OnClick:

Private Sub Button_AddNewRecord_Click()
Call SetTabVisible("TabNewAdd")
End Sub

The tab it should be displaying has a name of "TabNewAdd".

So when I click the AddNewRecord button I get the compile error stating:
"Expected variable or procedure, not module"
and it highlights "Call SetTabVisible"

Thanks for your help

Bill


JimBurke via AccessMonster.com said:
The sub would go in the form module. The subroutine argument would be the
name of the tab (not the caption!) that needs to be visible. I think you said
that for each tab, you want a command button that will make that tab the only
one visible. So for example if one of the tabs (not the main tab control
itself, but one of it's tabs) had a name of 'tabA', then the command button
to make only that tab visible would have an On Click event proc that would
just have

Call SetTabVisible("tabA")

Same would go for "tabB" - there would be a corresponding button with On
Click event proc of

Call SetTabVisible("tabB") and on and on, for each tab on the main tab
control.

Hope that explains it.

I think you nailed it. I just wish I understood it more! :)

Ok, so I need to create a sub (not sure how or maybe where) which would read
something like this:

Private Sub SetTabVisible(byval TabCntrl_Main as string)

For i = 0 to tabControl.Pages.count - 1
if tabControl.Pages(i).name = TabCntrl_Main then
tabControl.Pages(i).visible = true
else
tabControl.Pages(i).visible = false
end if
Next i

End Sub

Seems like Im missing something...
That IIf statement will only set the visible property of that one tab, and is
basically just toggling the value of the tab's visible property to the
[quoted text clipped - 45 lines]
IIf(Me.AddNew.Visible, False, True)
End Sub
 
D

dymondjack

Sorry I didn't get back yesterday, been busy.

You Module and the Function you shouldbe named differently. You should
really look at developing some naming conventions, such as prefixing a
command button with 'btn' or 'cmd' and a module with 'mod', and tab pages
with 'tab' or 'pg' or something. Not only will it make it about a thousand
times easier for you to keep track of what you're doing, more importantly it
keeps access from getting confused.

You can do one of two things to make this work after making sure your module
and function names are unique

1) paste these into the same module as your form, or
2) change the Private to Public on the functions.

As Private, they are only 'visible' by code inside the module they're in,
not from everywhere else. This is why they can be moved to your form's
module as is and will work fine within that form.

hth

Thanks for helping out Jim, I didn't fully understand what he was after.

--
Jack Leach
www.tristatemachine.com

- "Success is the ability to go from one failure to another with no loss of
enthusiasm." - Sir Winston Churchill


Bill said:
Perfect. Thank you for all the help and yes I now know the difference
between the name and caption fields :)

Ok so I created a module which is called "SetTabVisible" and contains:

Option Compare Database
Private Sub SetTabVisible(ByVal TabCntrl_Main As String)
For i = 0 To TabControl.Pages.Count - 1
If TabControl.Pages(i).Name = TabCntrl_Main Then
TabControl.Pages(i).Visible = True
Else
TabControl.Pages(i).Visible = False
End If
Next i
End Sub

The main tab control area is named "TabCntrl_Main". Then under the first
button "AddNewRecord" I placed the following code for OnClick:

Private Sub Button_AddNewRecord_Click()
Call SetTabVisible("TabNewAdd")
End Sub

The tab it should be displaying has a name of "TabNewAdd".

So when I click the AddNewRecord button I get the compile error stating:
"Expected variable or procedure, not module"
and it highlights "Call SetTabVisible"

Thanks for your help

Bill


JimBurke via AccessMonster.com said:
The sub would go in the form module. The subroutine argument would be the
name of the tab (not the caption!) that needs to be visible. I think you said
that for each tab, you want a command button that will make that tab the only
one visible. So for example if one of the tabs (not the main tab control
itself, but one of it's tabs) had a name of 'tabA', then the command button
to make only that tab visible would have an On Click event proc that would
just have

Call SetTabVisible("tabA")

Same would go for "tabB" - there would be a corresponding button with On
Click event proc of

Call SetTabVisible("tabB") and on and on, for each tab on the main tab
control.

Hope that explains it.

I think you nailed it. I just wish I understood it more! :)

Ok, so I need to create a sub (not sure how or maybe where) which would read
something like this:

Private Sub SetTabVisible(byval TabCntrl_Main as string)

For i = 0 to tabControl.Pages.count - 1
if tabControl.Pages(i).name = TabCntrl_Main then
tabControl.Pages(i).visible = true
else
tabControl.Pages(i).visible = false
end if
Next i

End Sub

Seems like Im missing something...

That IIf statement will only set the visible property of that one tab, and is
basically just toggling the value of the tab's visible property to the
[quoted text clipped - 45 lines]
IIf(Me.AddNew.Visible, False, True)
End Sub
 
J

JimBurke via AccessMonster.com

If 'TabCntrl_Main' is the name of the tab control, you can't also use it as
a subroutine argument name. I put tabControl in the code I showed you because
I wasn't sure what the name of the tab control was. In the sub, you need to
replace 'TabControl' with 'TabCntrl_Main'. Like Jack said, it's best to
always use good naming conventions, including using sub argument names that
are descriptive of what the argument is used for. In this case use something
like 'tabName', since the argument is going to be the name of a tab. Whatever
field name you choose for the argument, just change all references for
'TabCntrl_Main' in the sub to that new name.

Here's what you should have in the sub:

Private Sub SetTabVisible(ByVal tabName As String)

For i = 0 To TabCntrl_Main.Pages.Count - 1
If TabCntrl_Main.Pages(i).Name = tabName Then
TabCntrl_Main.Pages(i).Visible = True
Else
TabCntrl_Main.Pages(i).Visible = False
End If
Next i

End Sub

Also, the sub should just go in the form's module (when in the form in design
mode, just choose View, Code form the menu bar, paste the code at the bottom
of the form module after any subs that are already there). If you do create a
module that will contain code, don't also use the module name as the name of
a sub in that module. If the sub is set up only for a particular form's tab,
it makes sense to just put the code in the module of the form that has that
tab. You could create a general 'SetTabVisible' sub that would work for any
tab on any form. If you did that you'd have to also pass the tab control as
an argument and change the code accordingly. But for your purposes, since the
code is set up to refer to the tab from that particular form, put the code in
the form module and then delete the module you had created (if that 's the
only code in the module).


Perfect. Thank you for all the help and yes I now know the difference
between the name and caption fields :)

Ok so I created a module which is called "SetTabVisible" and contains:

Option Compare Database
Private Sub SetTabVisible(ByVal TabCntrl_Main As String)
For i = 0 To TabControl.Pages.Count - 1
If TabControl.Pages(i).Name = TabCntrl_Main Then
TabControl.Pages(i).Visible = True
Else
TabControl.Pages(i).Visible = False
End If
Next i
End Sub

The main tab control area is named "TabCntrl_Main". Then under the first
button "AddNewRecord" I placed the following code for OnClick:

Private Sub Button_AddNewRecord_Click()
Call SetTabVisible("TabNewAdd")
End Sub

The tab it should be displaying has a name of "TabNewAdd".

So when I click the AddNewRecord button I get the compile error stating:
"Expected variable or procedure, not module"
and it highlights "Call SetTabVisible"

Thanks for your help

Bill
The sub would go in the form module. The subroutine argument would be the
name of the tab (not the caption!) that needs to be visible. I think you said
[quoted text clipped - 38 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top