Consulting

Results 1 to 9 of 9

Thread: Finding selected optionbutton on a userform

  1. #1

    Finding selected optionbutton on a userform

    I am trying to create a multiple choice question paper on a userform. My problem is I have to check all the 80 option buttons if they're selected and if the answer is correct. Is there a way possible through which I can simply get all the option buttons that are selected?

    Manu

  2. #2
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    I'm not quite sure what you want to do, but maybe this loop might get you started:

    Dim Ctl As Control
    For Each Ctl In Me.Controls
          If TypeOf Ctl Is OptionButton Then
             Debug.Print Ctl.Name & ": " & Ctl.Value
          End If
       Next Ctl

    It goes through all controls on the current form, checks if the control is an optionbutton and if it is, it prints the name and value to the debug view.

    Daniel

  3. #3
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by Steiner
    I'm not quite sure what you want to do, but maybe this loop might get you started:

    Dim Ctl As Control
    For Each Ctl In Me.Controls
    If TypeOf Ctl Is OptionButton Then
    Debug.Print Ctl.Name & ": " & Ctl.Value
    End If
    Next Ctl

    It goes through all controls on the current form, checks if the control is an optionbutton and if it is, it prints the name and value to the debug view.

    Daniel
    Manu,
    Steiner's code will work according to your needs if you add all selected options to an array or string.

    Sample below is untested. Might have some error.

    Dim Ctl As Control
    For Each Ctl In Me.Controls
    If TypeOf Ctl Is OptionButton Then
    MsgBox Ctl.Name & ": " & Ctl.Value
    DoFunction(Ctl.Name) 'pass val to a function to verify right / wrong
    End If
    Next Ctl

  4. #4
    I', sorry I didn't mention my problem clearly, let me explain it again.
    There are 20 questions, to each question there are 4 options, only 20 options can be right, in other words a user can select only 20 option buttons. I want to be able to send the caption of only the selected option buttons to the excel sheet, is it possible?

    Manu

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    This identifies the name of each selected button

    Dim Ctl As Control
    For Each Ctl In Me.Controls
        If TypeName(Ctl) = "OptionButton" Then
            If Ctl.Value Then
                MsgBox Ctl.Name
            End If
        End If
    Next Ctl

  6. #6
    I want the names of option buttons which have been selected by the user, not the name of all is it possible?

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by m_chauhan77
    I want the names of option buttons which have been selected by the user, not the name of all is it possible?
    Did you try what I gave you?

  8. #8
    Yes I did, though made a little change to customize it for my need, it works great for me. Thanks Dude.
    Here it is
    Dim Ctl As Control
    For Each Ctl In Me.Controls
        If TypeName(Ctl) = "OptionButton" Then
            If Ctl.Value = True Then ' this gives me what I was looking for
                'MsgBox Ctl.Caption
                answer(m) = Ctl.Caption
                m = m + 1
            End If
        End If
    Next Ctl

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by m_chauhan77
    Yes I did, though made a little change to customize it for my need, it works great for me.
    You shouldn't have needed to do that.

    If set, Ctl.Value evaluates to True, so saying

    =IF true = True Then

    is no better than saying

    =If True Then

    Anyway, can you mark it as solved?

Posting Permissions

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