nesco88
nesco88

Reputation: 35

Using a button to switch between tabs in Access

How do I create a VBA macro to switch between pages in a tab control on a 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()
TabControl.Value = (TabCtl0.Value + 1) Mod TabControl.Pages.Count
End Sub

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

But 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 image:

Form Navigation Screenshot

Upvotes: 1

Views: 8212

Answers (6)

Sal
Sal

Reputation: 1

This works. On the On click event of a button

Me.tabname.SetFocus

Upvotes: 0

Ray Cernis
Ray Cernis

Reputation: 103

This is quite an old question, but for the benefit of anyone else that stumbles upon it:

The first example you give in the ones you've tried should work, but has a bug in it. The code refers to both TabControl and TabCtl0, and I think it should be just one or the other.

Assuming your button is called Command9, as in the image, and your tab control is called TabControl, the following should work:

Private Sub Command9_Click()
  TabControl.Value = (TabControl.Value + 1) Mod TabControl.Pages.Count
End Sub

Upvotes: 1

Joey
Joey

Reputation: 11

If you are using the macro builder, you can also use "go to control"

Upvotes: 1

user7075507
user7075507

Reputation:

How about this?

Me.TabControlName.Pages.Item("PageName").SetFocus

Upvotes: 0

ASH
ASH

Reputation: 20342

Why not simply switch between the tabs?

https://www.youtube.com/watch?v=OtN2DSg-cAU

That should get you going in the right direction.

Upvotes: 0

ThunderFrame
ThunderFrame

Reputation: 9461

It seems that you've inadvertently referred to TabControl when you mean to refer to TabCtl0.

You can make the references briefer by using a With..End With statement

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

Upvotes: 0

Related Questions