Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 09-28-2016, 10:47 AM   #1
nesco88
Newly Registered User
 
Join Date: Sep 2016
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
nesco88 is on a distinguished road
Using a button to switch between tabs in Access

Hello,

How do I create a VBA macro to switch between tabs in a navigation form in Access? I would think the code would be simple, but I haven't been able to find anything clear or definitive online. These are the code samples I've tried so far:

Private Sub Command5_Click()
With Me.TabCtl0 .Value = (.Value + 1) Mod .Pages.Count
End With
End Sub

Private Sub Command1_Click()
Me.NavigationButton2.SetFocus
End Sub

They give me errors, so I wonder if something more complicated is needed. The button and the tabs are separate (button is not on the tab itself), as shown in the attached image. We will eventually want to switch between many tabs, hence why we would like a button.
Attached Images
File Type: png DMfa5.png (5.5 KB, 91 views)

nesco88 is offline   Reply With Quote
Old 09-28-2016, 11:02 AM   #2
static
Newly Registered User
 
Join Date: Nov 2015
Posts: 823
Thanks: 4
Thanked 192 Times in 179 Posts
static will become famous soon enough static will become famous soon enough
Re: Using a button to switch between tabs in Access

TabCtl0.Pages(i).SetFocus
static is offline   Reply With Quote
Old 09-29-2016, 06:47 AM   #3
nesco88
Newly Registered User
 
Join Date: Sep 2016
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
nesco88 is on a distinguished road
Re: Using a button to switch between tabs in Access

Quote:
Originally Posted by static View Post
TabCtl0.Pages(i).SetFocus
Hi static,

I entered that code and declared i as an integer above it but still get:

Runtime error '424'

Object required

Is this because it is a navigation control form and doesn't recognize a tab control? Thank you.


Last edited by nesco88; 09-29-2016 at 10:44 AM.
nesco88 is offline   Reply With Quote
Old 09-29-2016, 07:47 AM   #4
Minty
AWF VIP
 
Minty's Avatar
 
Join Date: Jul 2013
Location: UK - Wiltshire
Posts: 5,709
Thanks: 138
Thanked 1,535 Times in 1,507 Posts
Minty has a spectacular aura about Minty has a spectacular aura about Minty has a spectacular aura about
Re: Using a button to switch between tabs in Access

Not being funny but why would you need a button when the tab is already there and can be made to look like a button?.
Seems very unnecessary to me, and is extra clutter on the form.
__________________
If we have helped please add to our reputation - click the scales symbol on the left, tick 'I approve' and leave a comment.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Minty is offline   Reply With Quote
Old 09-30-2016, 04:34 AM   #5
nesco88
Newly Registered User
 
Join Date: Sep 2016
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
nesco88 is on a distinguished road
Re: Using a button to switch between tabs in Access

Quote:
Originally Posted by Minty View Post
Not being funny but why would you need a button when the tab is already there and can be made to look like a button?.
Seems very unnecessary to me, and is extra clutter on the form.
Hi Minty,

It doesn't necessarily need to be done with a button, but one of the tabs will be populated with a list, and when this list is clicked, I want to move to the next tab.
nesco88 is offline   Reply With Quote
Old 09-30-2016, 04:48 AM   #6
Ranman256
Newly Registered User
 
Join Date: Apr 2015
Location: KY,USA
Posts: 3,139
Thanks: 0
Thanked 683 Times in 668 Posts
Ranman256 will become famous soon enough Ranman256 will become famous soon enough
Re: Using a button to switch between tabs in Access

The tabs are the 'buttons'.
Ranman256 is offline   Reply With Quote
Old 09-30-2016, 05:45 AM   #7
static
Newly Registered User
 
Join Date: Nov 2015
Posts: 823
Thanks: 4
Thanked 192 Times in 179 Posts
static will become famous soon enough static will become famous soon enough
Re: Using a button to switch between tabs in Access

I assumed we were talking about a tab control, but a quick search reveals navigation forms "Applies To: Access 2016 , Access 2013"

I only have access to 2007 ATM. I'll have a look later maybe.

static is offline   Reply With Quote
The Following User Says Thank You to static For This Useful Post:
nesco88 (10-05-2016)
Old 10-05-2016, 10:21 AM   #8
nesco88
Newly Registered User
 
Join Date: Sep 2016
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
nesco88 is on a distinguished road
Re: Using a button to switch between tabs in Access

Hi static,

Thanks, anything you could on tab navigation / navigation forms would be great as well.
nesco88 is offline   Reply With Quote
Old 10-05-2016, 10:26 AM   #9
MarkK
Super Moderator
 
MarkK's Avatar
 
Join Date: Mar 2004
Location: Vancouver BC
Posts: 7,761
Thanks: 10
Thanked 1,287 Times in 1,225 Posts
MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all MarkK is a name known to all
What is the name of your tab control?
__________________
formerly known as lagbolt | Windows 10 | Access 2010 | Visual Studio 2013 | "Institutions have a vested interest in perpetuating the problems to which they are the solution." - Clay Shirky
MarkK is offline   Reply With Quote
Old 10-05-2016, 01:01 PM   #10
static
Newly Registered User
 
Join Date: Nov 2015
Posts: 823
Thanks: 4
Thanked 192 Times in 179 Posts
static will become famous soon enough static will become famous soon enough
Re: Using a button to switch between tabs in Access

Oops sorry. forgot all about this.

I created a navigation form and added 2 forms to it.
This switches the linked form but it doesn't make the 'selected' tab appear selected.

Code:
Dim x

Private Sub Command1_Click()
    
    If x = 1 Then x = 0 Else x = 1
    
    NavigationControl0.Controls(x).SetFocus
    NavigationSubform.SourceObject = NavigationControl0.Controls(x).NavigationTargetName
    
End Sub
But...

You can easily make your own navigation form that does exactly the same thing

Add toggle buttons to a Frame (Option Group control)
Add a subform.

Add code to the frame's onclick event to switch the source object of the subform control.

Code:
Private Sub myFrame_Click()
     Select Case myFrame.Value
     Case 1: mySubForm.SourceObject = "form1"
     Case 2: mySubForm.SourceObject = "copy of form1"
     End Select
End Sub
To automate switching tabs, set the value of the frame.

Code:
Private Sub Command0_Click()
    If myFrame.Value = 1 Then myFrame.Value = 2 Else myFrame.Value = 1
    myFrame_Click
End Sub
static is offline   Reply With Quote
The Following User Says Thank You to static For This Useful Post:
nesco88 (10-06-2016)
Old 10-06-2016, 09:22 AM   #11
nesco88
Newly Registered User
 
Join Date: Sep 2016
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
nesco88 is on a distinguished road
Re: Using a button to switch between tabs in Access

Quote:
Originally Posted by MarkK View Post
What is the name of your tab control?
The name isTabCtl0.
nesco88 is offline   Reply With Quote
Old 10-06-2016, 09:24 AM   #12
nesco88
Newly Registered User
 
Join Date: Sep 2016
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
nesco88 is on a distinguished road
Re: Using a button to switch between tabs in Access

Quote:
Originally Posted by static View Post
Oops sorry. forgot all about this.

I created a navigation form and added 2 forms to it.
This switches the linked form but it doesn't make the 'selected' tab appear selected.

Code:
Dim x

Private Sub Command1_Click()
    
    If x = 1 Then x = 0 Else x = 1
    
    NavigationControl0.Controls(x).SetFocus
    NavigationSubform.SourceObject = NavigationControl0.Controls(x).NavigationTargetName
    
End Sub
But...

You can easily make your own navigation form that does exactly the same thing

Add toggle buttons to a Frame (Option Group control)
Add a subform.

Add code to the frame's onclick event to switch the source object of the subform control.

Code:
Private Sub myFrame_Click()
     Select Case myFrame.Value
     Case 1: mySubForm.SourceObject = "form1"
     Case 2: mySubForm.SourceObject = "copy of form1"
     End Select
End Sub
To automate switching tabs, set the value of the frame.

Code:
Private Sub Command0_Click()
    If myFrame.Value = 1 Then myFrame.Value = 2 Else myFrame.Value = 1
    myFrame_Click
End Sub
Hi static,

Thanks, the first part works! However, is there a way to make the selected tab appear selected without frames? As we would have to restructure the whole database to make that work.
nesco88 is offline   Reply With Quote
Old 10-06-2016, 02:34 PM   #13
static
Newly Registered User
 
Join Date: Nov 2015
Posts: 823
Thanks: 4
Thanked 192 Times in 179 Posts
static will become famous soon enough static will become famous soon enough
Re: Using a button to switch between tabs in Access

Well a quick search shows some have solved it by using docmd.browseTo.
But I can't get it to work and it's getting late.

A dirty method would be to set focus on the button and use sendkeys to hit the spacebar

NavigationControl0.Controls(x).SetFocus
SendKeys " "

Then you don't have to set the sourceobject.

static is offline   Reply With Quote
Reply

Tags
access , button , macro , navigation , tab

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Navigation control and switch between tabs nightmare!!! Gianluca Forms 16 06-17-2015 05:19 AM
RowSource switch using toggle button megamef Modules & VBA 1 12-04-2014 03:59 AM
Switch Control Sources via a button? november Forms 3 12-06-2008 09:24 AM
Switch tabs & go to corresponding record _Nickel_ Modules & VBA 3 12-02-2003 12:48 PM
Makro button to switch pages Royor Macros 0 02-10-2002 09:40 AM




All times are GMT -8. The time now is 01:41 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Sponsored Links

How to advertise

Media Kit


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World