Results 1 to 7 of 7

Thread: RESOLVED - Call an Option Button by String

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    3

    Resolved RESOLVED - Call an Option Button by String

    Hi! (This is my first post )

    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
    Last edited by taken; Oct 4th, 2006 at 09:53 PM.

  2. #2

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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.

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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:
    1. CallByName Command1, "BackColor", VbLet, vbRed
    and

    VB Code:
    1. Dim ctl As Control
    2.  
    3. For Each ctl In Controls
    4.  
    5.     If TypeOf ctl Is CommandButton Then
    6.         ctl.BackColor = vbGreen
    7.     End If
    8.  
    9. Next

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2006
    Posts
    3

    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:
    1. Private Sub Command1_Click()
    2. Dim Controls(1 To 3) As Control
    3. Controls(1) = Option1
    4. Controls(2) = Option2
    5. Controls(3) = Option3
    6.  
    7. Dim ctl As Control
    8.  
    9. For Each ctl In Controls
    10.  
    11.     If TypeOf ctl Is OptionButton Then
    12.         ctl.BackColor = vbGreen
    13.     End If
    14.  
    15. Next
    16. End Sub


    Thanks

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Dim N As Long
    2. For N = 1 To 3
    3.     Me.Controls("Option" & N).BackColor = vbRed
    4. Next N

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Call an Option Button by String

    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:
    1. Private Sub Command1_Click()
    2. Dim ctl As Control
    3.  
    4. For Each ctl In Controls
    5.  
    6.     If TypeOf ctl Is OptionButton Then
    7.         ctl.BackColor = vbGreen
    8.     End If
    9.  
    10. Next
    11. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width