Tab Control and Events - Total confusion (1 Viewer)

Minty

AWF VIP
Local time
Today, 15:27
Joined
Jul 26, 2013
Messages
10,354
Hi All, I've been battling with a conundrum for a while on and off, and am going to admit defeat, at least for now.

I have a Main form that has two subforms on it. Form one on the left side is a list based continuous form, that drives the data available on SubForm 2. All very straight forward.
The edit form has a number of tab pages on it. These tabbed pages are not always all visible, depending on certain data in the form. Again this all works tickety boo, and is driven by the current event on the sub form.

And now the issue. When switching between the items on the list form I would like Form 2 to remain on the tab page selected previously.

It looks very roughly like below; (I really am rubbish at mock ups, and can't post the actual db very easily it's all tied up with client data and a remote backend.)
1591284729567.png


What I have tried - A unbound control on the main holding form that is set to the tabcontrol page value on the tab change event. This works almost, except that changing tabs fires the current event and the change event of the tab control as well and messes the whole thing up. When debugging this is what I see in the immediate window, the number shown is Me.tabOtherData.Value

Debug.Print Event & page index
Clicking on the Tab for page index 3
Tab Change 3
Tab Change 0
Tab Change 3
Current 3

Clicking on the Tab for page index 4
Tab Change 4
Tab Change 0
Tab Change 4
Current 4

Now the really weird one
Clicking on the Tab for page index 0
Tab Change 0
Tab Change 3
Tab Change 0
Tab Change 3
Current 3

It ends up back on tab page index 3...


So why is the tabchange event firing multiple times, and why is the current event firing at all?
I suspect it is to do with hiding some of the pages, but the event that drives that shouldn't be being triggered.

I am going to try and knock up a more meaningful actual demo in the meantime but have been too busy messing around with it.

Edit :
The On current event is firing twice once on initial change and once again after the tab hiding goes on so something weird is going on.
 
Last edited:

Minty

AWF VIP
Local time
Today, 15:27
Joined
Jul 26, 2013
Messages
10,354
I'm going to half answer my own question which is very bad form I know.

It appears making a tab page visible or not fires the tab change event, which is annoying. Enabling or disabling the tabs doesn't have the same effect so I've gone down that route for the moment, but it's not as elegant as you ca still click on them.

More thought required.
 

cheekybuddha

AWF VIP
Local time
Today, 15:27
Joined
Jul 21, 2014
Messages
2,237
>> I'm going to half answer my own question which is very bad form I know. <<
I disagree!

>> It appears making a tab page visible or not fires the tab change event, which is annoying <<
How about keeping a variable with the number of visible tabs that is updated in tab change event.

In the change event check the number of visible tabs against the variable - if it's different don't do anything (except update variable), otherwise do your thing (and update variable)

Something like:
Code:
Option Explicit
Option Compare Database

Private m_iVisibleTabs As Integer

Private Function VisibleTabs() As Integer
  Dim iRet As Integer, i As Integer
  With Me.tabCtl
    For i = 0 To .Pages.Count - 1
      iRet = iRet = IIf(.Pages(i).Visible, 1, 0)
    Next i
  End With
  VisibleTabs = iRet
End Function

Private Sub Form_Load()
  m_iVisibleTabs = VisibleTabs
' ... other code
End Sub

Private Sub tabCtl_Change()
  If m_iVisibleTabs = VisibleTabs Then
    ' Do stuff
  Else
    m_iVisibleTabs = VisibleTabs
  End If
End Sub
(NB Untested aircode)

hth,

d
 

Minty

AWF VIP
Local time
Today, 15:27
Joined
Jul 26, 2013
Messages
10,354
That's a really good idea, but (and I haven't checked this) I have 5 tabs and might only have 3 visible, let's say 0, 2, 3 and then on the change event the visible ones might become 0, 2, 4 which is still 3 visible ones. In this case, I wouldn't expect or need to try and stay on the same tab, but I think it would still break.

What I could do is possibly check the combination of pages and set flags if any of the page flags change then tough move the tab page, but if they don't then status quo...

Good thinking David, maybe put me on the correct page (terrible pun sorry) path.
 

Micron

AWF VIP
Local time
Today, 11:27
Joined
Oct 20, 2018
Messages
3,476
on the change event the visible ones might become 0, 2, 4 which is still 3 visible ones.
But the sum of their ordinal positions in the collection would change. 0+2+3 <> 0+2+4 or probably more correctly 0+1+2 <> 0+2+3
Just make sure whatever you sum can only have unique values. Could even be tag property?
 

cheekybuddha

AWF VIP
Local time
Today, 15:27
Joined
Jul 21, 2014
Messages
2,237
I didn't test, but I meant to loop through the pages collection and test whether each one is visible or not.

This is different from page index which will change when a tab is hidden/shown.
 

cheekybuddha

AWF VIP
Local time
Today, 15:27
Joined
Jul 21, 2014
Messages
2,237
Ignore my previous post - I didn't read your's and Micron's posts properly
 

Micron

AWF VIP
Local time
Today, 11:27
Joined
Oct 20, 2018
Messages
3,476
This is different from page index which will change when a tab is hidden/shown.

I think hiding a page does not remove it from the collection and does not alter the position of the pages...
 

Minty

AWF VIP
Local time
Today, 15:27
Joined
Jul 26, 2013
Messages
10,354
I think hiding a page does not remove it from the collection and does not alter the position of the pages...

No, I don't think it does.
What is annoying is that it appears that making the change to the pages visible property seems to fire the current event.
 

cheekybuddha

AWF VIP
Local time
Today, 15:27
Joined
Jul 21, 2014
Messages
2,237
Use this as the VisibleTabs function:
Code:
Function VisibleTabs() As Integer

  Dim iRet As Integer, i As Integer
  
  With Me.tabCtl
    For i = 0 To .Pages.Count - 1
      If .Pages(i).Visible Then
        iRet = iRet + (2 ^ i)
      End If
    Next i
  End With
  VisibleTabs = iRet

End Function
 

cheekybuddha

AWF VIP
Local time
Today, 15:27
Joined
Jul 21, 2014
Messages
2,237
You will get a different total for each combination of tabs

(I think I've got the maths right! 😬 )
 

Users who are viewing this thread

Top Bottom