Reputation: 35
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:
Upvotes: 1
Views: 8212
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
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
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