Sending a control to a sub

bbrother

New Member
Joined
Nov 23, 2015
Messages
41
Oh Great Vicars of VBA and your Excellences of Excel,

I have a toggle button on a user form within a mutipage that I want to send to a sub. I know I am supposed to quote code - but I am not sure what it means for a control on a userform.

About the control:
Type: ToggleButton
Name: TBOC

It resides in the (container?) multipage1
Page
Name: ComputeType
Index: 5

When I execute the code I take it right to the point where it calls the sub and I take a look at what TBOC is. In the watch, TBOC is described as type "Object/ToggleButton" and the context is Wizard2016x.SimulationCapacity

Now for the actual code:

Code:
TurnOnC (TBOC) 

Sub TurnOnC(Ctrl As Control)
    Ctrl.Value = True
    Ctrl.BackColor = vbGreen
End Sub

Now I've tried sticking in "ByRef" without really knowing what it does, and it doesn't seem to help. I am thinking that the problem has something to do with the context since the control is sitting in a container. I want to make the sub independent of context - that is, for any togglebutton on any page I want it to show up as vbGreen when depressed and vbButtonFace when off. (Of course, there's a TurnOffC that mirrors this one).
I actually had this working fine for one page by simply passing the control name (string) to a sub, but when I passed a control with a different context it no longer worked - so I am looking for a context independent solution.

My sometimes working sub looks like this, but I thought simply passing the control would be a far more elegant and robust solution.
Code:
Sub TurnOn(name As String)    Me.MultiPage1.Pages(Me.MultiPage1.Value).Controls(name).Value = True
    Me.MultiPage1.Pages(Me.MultiPage1.Value).Controls(name).BackColor = vbGreen
End Sub

Best regards,
Excel Neophyte
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I forgot to mention, that the above code fails to run. The error is runtime error 424: Object required

It sure looks like an object to me...

As is traditional, I then want to use the computer to replicate errors faster than humanly possible and pass a list of controls to turn off to my sub:

Code:
' TBOP TBOPEN TBOPAN are all toggle buttons on a userform
For Each Ctrl In Array(TBOP, TBOPEN, TBOPAN)
     TurnOffC (Ctrl)
Next

Is it even legal to make an array of controls? I don't want to loop through all of them on the form, but I want to loop through a list.

Best regards,
Excel Neophyte
 
Last edited:
Upvote 0
I am making a little progress- I added context, and now the single call is working:

Code:
With Me.MultiPage1.Pages("ComputeType")
        TurnOnC (.TBOC)

However, when I try to use this in a for each loop, it will crash.
Code:
For Each Ctrl In Array(.TBOP, .TBOPEN, .TBOPAN)     TurnOnC (Ctrl)
Next
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top