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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Button (3rd Time)

Status
Not open for further replies.

jbento

Technical User
Jul 20, 2001
573
US
Can someone help with this?

I have an option group (radio buttons), with 6 radio buttons. I want the user to have to choose one or more of those buttons before he closes or moves to another record, and if he doesn't do that, he will get a message saying he needs to do this before leaving. I have the following code for it:

Private Sub Form_Close()
Dim No_Buttons_Selected As Boolean
No_Buttons_Selected = False
If Me!RadioButton1.Value = False Then
No_Buttons_Selected = True
End If
If Me!RadioButton2.Value = False Then
No_Buttons_Selected = True
End If
If Me!RadioButton3.Value = False Then
No_Buttons_Selected = True
End If
If Me!RadioButton4.Value = False Then
No_Buttons_Selected = True
End If
If Me!RadioButton5.Value = False Then
No_Buttons_Selected = True
End If
If Me!RadioButton6.Value = False Then
No_Buttons_Selected = True
End If
If No_Buttons_Selected Then
MsgBox "Please select a Radio button"
End If
End Sub

The code runs fine, but if I choose ok from the message (MsgBox) the second time, it allows me to go to the next record or closes the form. I want it to loop with the MsgBox, until the user chooses one of more radio buttons. I tried to do a loop, but I can't get it to work. Does anyone know of more code I can add to the above to get it to do what I want? Please help. Thanks.

Jerome
 
Yeah, you need to loop this. That will tighten up your code and make life easier for you later when you maintain this stuff. First off, this is not an option group. Option groups only allow one choice. This is a group of radio buttons within a frame.

Private Sub Form_Unload(Cancel as integer)
Dim ctl as control
dim blnOK as Boolean

for each ctl in Me.controls
if typeof ctl is optionbutton then
if ctl.value = True then
blnOK = TRUE
exit for
end if
end if
next
if blnoK = False then
msgbox ""Please select a Radio button"
cancel = 1
end if

end sub

This sub routine loops through all of the controls on the form and determines what the value of your radio buttons are. When it comes across a radio button with a value of TRUE then it exits the loop(assuming a selected button has a value of TRUE). When it evaluates blnOK, if the value is FALSE the msgbox is displayed and the form will not unload.

Hope that helps.

BB
:)
 
rbowes,
I tried this code and unfortunately it doesn't work like I want it to. Let me explain what I want. I want this code to work on unload and if the user tries to go to another record. This code only works from the unload standpoint. Also, I want the user to be able to get out of this loop or if statement if he only chooses one radio button. I only want the message to come up if none of the radio buttons are selected. I tried it and if I choose one radio button, it will not let me get out of the code, unless I choose all the radio buttons. Can you help me with this? Thank you very much.

Jerome
 
What you could do is make the loop routine its own Private Function returning a boolean and call it from any event you want, Unload, Save or whatever and then evaluate.

As for the other thing, I am not sure what your radio buttons are used for. If in their unselected state, they have a value of False then a selected radio button will have a value of True. In the loop it tests each radio button until it comes up with a button that has a value of True. It then sets the variable blnOK to true and then exits the loop. in the routine that I put in the Unload event, it generates a msgbox if blnOK is false and then cancels. I checked it out and it works ok. If you put a msgbox on the radio button's click event to find its value it comes up with either -1 or 0 (TRUE or False respectively)

It should work for you.

BB
:cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top