Run VBA code whenever a tabbed form is reselected in Access
Asked Answered
M

2

5

EDIT: For clarification, I'm talking in the below post about tabbed document browsing, not a Tab Control. However, if you're looking for roughly the same problem but regarding a Tab Control, Gord Thompson's answer is correct. Two answers for the price of one!

I've got an Access 2007 database that uses tabbed documents. I need to run some VBA code every time a user selects the form called "Reports", either via opening it or clicking on its tab if it's already open.

I could achieve much the same thing by closing it each time it's used and running the code on an OnLoad event, but ideally I'd like to keep it open so that users can keep the settings of the various drop down boxes, radio boxes etc that they've already set on "Reports".

I was hoping for an event that could run code on tab reselection, but neither of my guesses (OnCurrent and GotFocus) seem to work (OnCurrent works only when the form is opened, like OnLoad would).

Any ideas greatly appreciated - can't find what I'm looking for on Google, though I suspect that's because I don't know exactly what I'm looking for.

Matte answered 22/5, 2013 at 11:33 Comment(1)
+1 The "Current Database" options in Access 2007 and later versions include a setting called "Document Window Options", which offers a choice between "Overlapping Windows" and "Tabbed Documents". This question is about a form with the "Tabbed Documents" setting active.Ked
M
3

Have found the answer I was looking for. It's the OnActivate event.

Matte answered 22/5, 2013 at 11:47 Comment(0)
K
4

The .Value property of a TabControl returns the index (zero-based) of the current page. So, if I have a TabControl named TabCtl14 that contains two pages, FirstPage and SecondPage, then the code...

Private Sub TabCtl14_Click()
If Me.TabCtl14.Value = 1 Then
    MsgBox "SecondPage was clicked."
End If
End Sub

...displays the MsgBox whenever I click the "SecondPage" tab in the TabControl.

Kellum answered 22/5, 2013 at 12:2 Comment(2)
To clarify, I meant tabbed documents (as in the way of viewing forms) as opposed to a TabControl within a form.Matte
@BFDatabaseAdmin Ah, okay, then Form.OnActivate is probably what you'll want to use in that case.Kellum
M
3

Have found the answer I was looking for. It's the OnActivate event.

Matte answered 22/5, 2013 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.