Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Option Button Array Question 1

Status
Not open for further replies.

Elena

Technical User
Oct 20, 2000
112
US
Hi,

I have a control array that is comprised of option buttons in a frame. I want to make sure that one of the options is selected at runtime. How do I find out? Is there something simple or do I have to iterate through the array one at a time?

Thanks,
Elena

 
You can set it in design mode and you could also again set it at run time, Form_Load option(?).value = true. If you set it in design mode, the form script is also setting it at load, so you actually only req one of these.
 
I thought of that, but I wanted a way to double check that the user actually paid enough attention to check the right one instead of accepting what would appear to be the default. Is there another way?
 

Declare a integer variable (form level) and in the click event of the option button array set the integer variable = index. You can then check the value of the integer variable in your validation routine without having to enumerate through the controls.

Good Luck
 
Thanks, that works!

Best regards,
Elena
 
In which case you could set your integer during loading to -1 and be able to see if something has been selected.
 

I also use a boolean like strongm in the Form_Load event, to exit any unwanted events during loading.

However, in this situation, where you want to make sure that the user makes a selection no matter what, you can set the option buttons all to False in the load event.
Setting an Option button to False will not fire the Option button's Click event.

(This example is assumming an Option Button control array is being used. If it is not a control array, then you will need to test each option button with a If/Else statement):

Private Sub Form_Load()

Dim opt As OptionButton
For Each opt In Option1
opt.Value = False
Next opt
End Sub

Now you need a method to check if any option buttons have been selected:

Private Function CheckOptions() As Boolean
Dim opt As OptionButton
For Each opt In Option1
If opt.Value Then
CheckOptions = True
Exit Function
End If
Next opt
End Function

So, before your code continues onto the next step, call this function, and if it returns FALSE, then you know that the user hasn't selected anything:

If CheckOptions() then
MsgBox "Option selection was made
Else
MsgBox "Option selection was NOT made

End If [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top