Using a button to switch between tabs in Access (1 Viewer)

nesco88

New member
Joined
Sep 28, 2016
Messages
8
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.
 

Attachments

nesco88

New member
Joined
Sep 28, 2016
Messages
8
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:

Minty

AWF VIP
Joined
Jul 26, 2013
Messages
6,543
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.
 

nesco88

New member
Joined
Sep 28, 2016
Messages
8
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.
 

static

Registered User
Joined
Nov 2, 2015
Messages
823
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.
 

nesco88

New member
Joined
Sep 28, 2016
Messages
8
Hi static,

Thanks, anything you could on tab navigation / navigation forms would be great as well.
 

static

Registered User
Joined
Nov 2, 2015
Messages
823
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
 

nesco88

New member
Joined
Sep 28, 2016
Messages
8
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.
 

static

Registered User
Joined
Nov 2, 2015
Messages
823
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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top Bottom