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?
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.
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!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.