PDA

View Full Version : [SOLVED] Finding selected optionbutton on a userform



m_chauhan77
06-21-2005, 11:22 PM
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

Steiner
06-22-2005, 12:11 AM
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

sheeeng
06-22-2005, 12:45 AM
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

m_chauhan77
06-22-2005, 01:00 AM
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

Bob Phillips
06-22-2005, 01:40 AM
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

m_chauhan77
06-22-2005, 01:54 AM
I want the names of option buttons which have been selected by the user, not the name of all is it possible?

Bob Phillips
06-22-2005, 03:00 AM
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?

m_chauhan77
06-22-2005, 03:22 AM
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

Bob Phillips
06-22-2005, 03:27 AM
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?