RESOLVED - Call an Option Button by String
Hi! (This is my first post :wave: )
I want to call an option by using its string name.
I saw someone talk about the CallByName method and how it can be used to
call a form or procdure by name, but I can't figure out how to use it to call an option. Can it? Or is their another method that will work?
Confused???? Here's what I actually want to do:
I have many option buttons in my form (ex Option1, Option2 and Option3...)
I want to be able to change all of their backColors, and so it would be really
great if I could use a loop and a counter to change all of their backColors in
a couple lines of code.
Thanks for your time :)
Re: Call an Option Button by String
http://www.vbforums.com/attachment.p...id=47243&stc=1
If you could make a control array out of your command buttons then it would be easy.
Re: Call an Option Button by String
Quote:
Originally Posted by taken
...I have many option buttons in my form (ex Option1, Option2 and Option3...)
You may want to "switch" to control array instead - group of controls of the same type, sharing the same name but having unique indexes. Managing control array is a breaze.
We have plenty of tutorials and samples arround here.
Re: Call an Option Button by String
I should have said in the previous post that it would be easy to do them all at once. BTW the Style of the command buttons must be Graphical. There are a couple of ways to do it if it's not a control array. One is callbyname
VB Code:
CallByName Command1, "BackColor", VbLet, vbRed
and
VB Code:
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is CommandButton Then
ctl.BackColor = vbGreen
End If
Next
Re: Call an Option Button by String
Hi! Thanks for your help. I know that it will work, but I just can't figure out what I'm doing wrong. Maybe you could help:
VB Code:
Private Sub Command1_Click()
Dim Controls(1 To 3) As Control
Controls(1) = Option1
Controls(2) = Option2
Controls(3) = Option3
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is OptionButton Then
ctl.BackColor = vbGreen
End If
Next
End Sub
Thanks
Re: Call an Option Button by String
as mentioned, you should probably use a control array - but you can use the Controls collection in the following way too:
VB Code:
Dim N As Long
For N = 1 To 3
Me.Controls("Option" & N).BackColor = vbRed
Next N
Re: Call an Option Button by String
Quote:
Dim Controls(1 To 3) As Control
Controls(1) = Option1
Controls(2) = Option2
Controls(3) = Option3
The above lines are not needed in your procedure
VB Code:
Private Sub Command1_Click()
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is OptionButton Then
ctl.BackColor = vbGreen
End If
Next
End Sub