Tab control click events (1 Viewer)

Atomic Shrimp

Humanoid lifeform
Local time
Today, 04:48
Joined
Jun 16, 2000
Messages
1,954
I've got a tab control which contains an assortment of subforms and controls - I want one of the subforms to requery when the user switches to that tab.

Except it just doesn't work. At least not when the user clicks on the tab. The click event of the tab page doesn't get triggered by clicking on the tab, only the main page area.

How do I get code to run when the tab itself is clicked?
 

Dennisk

AWF VIP
Local time
Today, 04:48
Joined
Jul 22, 2004
Messages
1,649
try the onenter Event (or Gotfocus) for the subform on the tab.

or when the tab is clicked you still get an event for the tab control so you can determine which tab has been clicked.

I have done this somewhere in my 100s of dbs, but I doubt I will be able to find it easily.
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:48
Joined
Sep 12, 2006
Messages
15,652
try the tabcontrols "change" event in conjunction with testing the active tab page (must be some way of doing this)

the tab page itself has a click event also, but this isnt fired just by selecting the page
 

Atomic Shrimp

Humanoid lifeform
Local time
Today, 04:48
Joined
Jun 16, 2000
Messages
1,954
try the tabcontrols "change" event in conjunction with testing the active tab page (must be some way of doing this)
That's what I'm thinking - the tab control (whole thing) has a click event that could also be used, but I can't work out how to determine which page has just become active.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 04:48
Joined
Sep 12, 2006
Messages
15,652
use the change event of the tabctrl itslef

and then the tabctrl.VALUE holds the pagenumber

eg
change click event handler
MsgBox ("Changed to " & MyTabCtl.value)
 

missinglinq

AWF VIP
Local time
Yesterday, 23:48
Joined
Jun 20, 2003
Messages
6,423
As gemma has suggested, the OnClick event for a tabbed control is nearly useless! You have to use the OnChange event. Here's a short tutorial:

The first thing to understand is that tabbed pages are indexed starting with zero, i.e. the first page has a value of 0, second page has a value of 1, third page has a value of 2, etc.

Secondly, you need to understand that the OnClick event of a tabbed page only fires when you click on the page itself, not the tab at the top of the page!

Lastly, you need to understand that the tabbed control change event fires anytime the tabbed pages change. But to base something on the change event, you have to identify the particular page that has focus.

Code:
Private Sub YourTabbedControl_Change()
If Me.YourTabbedControl.Value = 0 Then 'First Page
  'Do code for Page 1
ElseIf Me.YourTabbedControl.Value = 1 Then 'Second page
  'Do code for Page 2
ElseIf Me.YourTabbedControl.Value = 2 Then 'Third page
  'Do code for Page 3
End If
End Sub
 

boblarson

Smeghead
Local time
Yesterday, 20:48
Joined
Jan 12, 2001
Messages
32,059
Code:
Private Sub YourTabbedControl_Change()
If Me.YourTabbedControl.Value = 0 Then 'First Page
  'Do code for Page 1
ElseIf Me.YourTabbedControl.Value = 1 Then 'Second page
  'Do code for Page 2
ElseIf Me.YourTabbedControl.Value = 2 Then 'Third page
  'Do code for Page 3
End If
End Sub

I would suggest a select case statement as it is cleaner and easier to manage (less typing - type Me.YourTabbedControl.Value once :))
 

missinglinq

AWF VIP
Local time
Yesterday, 23:48
Joined
Jun 20, 2003
Messages
6,423
Good point, Bob! I actually do use Select Case for most everything these days. This was an old response I started posting a gazillion years ago. :D

Here's the same hack using Select Case
Code:
Private Sub YourTabbedControl_Change()
  Select Case YourTabbedControl 
    Case 0  'First Page
      'Code for Page 1
    Case 1  'Second page
      'Code for Page 2
   Case 2   'Third page
     'Code for Page 3
  End Select
End Sub
 
Last edited:

Atomic Shrimp

Humanoid lifeform
Local time
Today, 04:48
Joined
Jun 16, 2000
Messages
1,954
Thanks for the replies - that's great.

I am going to have to think about how to write it so that it will still work if I at some point insert new tabs or change the order of existing one, or change their captions.

I think the way to do it is going to be with the tag property - put something unique in there, then on the change event, determine which tab is active, then use that number to look up the tag of the page, then decide on the appropriate action...

Phew!
 

JIMR

JIMR
Local time
Yesterday, 20:48
Joined
Sep 26, 2008
Messages
63
Hi Mike Gurman,

You probably have this solved but I was looking to do the same thing when switching tabs.

Using the information in this thead I was able to requery a form by using "me.yourqryname.requery" in the on change event. no other code was needed.
 

Users who are viewing this thread

Top Bottom