Results 1 to 8 of 8

Thread: Can you change a button group lock options to True/False with 1 command?

  1. #1
    Board Regular TAPS_MikeDion's Avatar
    Join Date
    Aug 2009
    Location
    Massachusetts
    Posts
    489
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Can you change a button group lock options to True/False with 1 command?

    Just wondering if I can shorten my code a tad by changing grouped buttons with 1 command instead of two.

    For example: If you have a group of two radio buttons (for this example I'll call the group "NewEmp") and you want to lock or unlock them, do you have to use two commands (i.e. Me.NewEmpYes.Locked = False, Me.NewEmpNo.Locked = False) or is there a way to lock/unlock them both with one command since they're grouped together?

    Thanks.
    Mike Dion

  2. #2
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,908
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    There is not a single command for that.
    You have to go through the controls, ask if it is an option button and if it is within the group, then change the status, for example:

    Code:
      Dim Cntrl As Control
      For Each Cntrl In Controls
        If TypeOf Cntrl Is MSForms.OptionButton Then
          If Cntrl.GroupName = "NewEmp" Then
            Cntrl.Locked = False
          End If
        End If
      Next
    Last edited by DanteAmor; Oct 10th, 2019 at 01:52 PM.
    Regards Dante Amor

  3. #3
    Board Regular TAPS_MikeDion's Avatar
    Join Date
    Aug 2009
    Location
    Massachusetts
    Posts
    489
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    Thanks Dante! I'm going to try this.
    Mike Dion

  4. #4
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,908
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    Youre welcome, thanks for the feedback.
    Regards Dante Amor

  5. #5
    Board Regular TAPS_MikeDion's Avatar
    Join Date
    Aug 2009
    Location
    Massachusetts
    Posts
    489
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    Thanks Dante. It worked for one set of buttons.

    What in your opinion would be the best way to approach it so it works with multiple groups?
    By that, I mean there are several groups of buttons that need to be either False or True at the moment that code is called upon. Right now, your code works with one set of buttons. If I were to add ElseIf, then it will bypass the other button groups.

    Thanks again!
    Mike Dion

  6. #6
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,908
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    It may be so:

    Code:
     Dim Cntrl As Control
      For Each Cntrl In Controls
        If TypeOf Cntrl Is MSForms.OptionButton Then
          Select Case Cntrl.GroupName
            Case "NewEmp": Cntrl.Locked = False
            Case "Group2": Cntrl.Locked = True
            Case "Group3": Cntrl.Locked = False
          End If
        End If
      Next
    Regards Dante Amor

  7. #7
    Board Regular TAPS_MikeDion's Avatar
    Join Date
    Aug 2009
    Location
    Massachusetts
    Posts
    489
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    Awesome! This worked. Thanks Dante. Funny, I was thinking about using Select Case, but didn't know if that would work for this.

    Had to edit the code a little. The first "End If" should be "End Select" for those who may come across this thread down the road.
    Mike Dion

  8. #8
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,908
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Can you change a button group lock options to True/False with 1 command?

    Quote Originally Posted by TAPS_MikeDion View Post
    Awesome! This worked. Thanks Dante. Funny, I was thinking about using Select Case, but didn't know if that would work for this.

    Had to edit the code a little. The first "End If" should be "End Select" This is correct


    for those who may come across this thread down the road.

    Must be:
    Code:
     Dim Cntrl As Control
      For Each Cntrl In Controls
        If TypeOf Cntrl Is MSForms.OptionButton Then
          Select Case Cntrl.GroupName
            Case "NewEmp": Cntrl.Locked = False
            Case "Group2": Cntrl.Locked = True
            Case "Group3": Cntrl.Locked = False
          End Select
        End If
      Next
    Regards Dante Amor

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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