Nazrul
Nazrul

Reputation: 5

How to get values from controls on tabstrip in VBA?

In different tabs of a tabstrip I have input values which are different in each tab. I need to write a code which takes all these values and do some work like sums up the values of each tab on button click.

Can anyone help me do this? In my code when I input value at a textbox of one tab it also changes the value of all other tabs and hence cannot receive different values of each tab. Any idea, please?

Please click here to check what I am trying to do and what suppressing me to do this

Upvotes: 0

Views: 4331

Answers (1)

Mistella
Mistella

Reputation: 1738

Multipages vs Tabstrips


A multipage is an object with "pages". Each page can hold it's own collection of controls which can then be referenced either directly or through the containing page object.

A tab strip is an object with "tabs". Unlike a "page" object, a tab does not have it's own controls. Instead, there are only the original controls, visible for all "tabs".

Programmatic differences

Since a multipage has a different set of controls for each page, there is very little housekeeping required. The selection of a page affects which controls are visible, and the controls automatically retain values assigned to them (as expected).

For a tabstrip, since there is only the initial set of controls, there is a lot of housekeeping required in the code. The selection of a tabstrip does not have any automatic effect on the values in the controls. Instead the controls act as would be expected if they were not in the tab strip at all.


Tabstrip Solution


Set up a variable (array, collection, dictionary) that can be used to hold the various values of the controls. Then, in the TabStrip_Change() event, store the previous value, and reset the control for the new tab (or fill in the value the new tab last held).

I recommend adding a userform level variable Dim old_tab as Long which you can set to the current page at the end of the TabStrip_Change() event. (This is useful for retrieving previously filled values for the correct tab).

For my sample code, I will be using an array. However, since arrays are not very flexible with changing lengths, you can also look into using either a dictionary or collection, if desired.

For the userform pictured below, the following code causes the single textbox to act as though there is a different textbox per tab. It also saves the values whenever the tabstrip changes. (Note: if you then use the saved values for the calculation, remember to update the value for the current tabstrip first.)

Option Explicit
Dim old_tab As Long
Dim textValues As Variant

Private Sub TabStrip1_Change()
    textValues(old_tab) = TextBox1.Value 'Saves the old value
    TextBox1.Text = textValues(TabStrip1.Value) 'Updates value to reflect tab change
    old_tab = TabStrip1.Value 'updates tab # variable
End Sub

Private Sub UserForm_Initialize()
    ReDim textValues(0 To TabStrip1.Tabs.Count - 1) 'tabs are zero-based, so count is always one more than the maximum tab value
    old_tab = TabStrip1.Value 'Ensures that the first value will be saved to correct location at the tab change
End Sub

Sample Userform with tabstrip with two tabs and one textbox

Upvotes: 1

Related Questions