Check for radiobutton status

I have serveral groups of radiobuttons in a form.

As the form opens up empty I want to check all the groups of radiobuttons for at least one index (radiobutton) clicked before saving.

I tried this

Iterate thru all controls in the window…

 [code] if ctrl IsA RadioButton then 
    
    if RadioButton(ctrl).Value = False then
      
      MsgBox "THERE ARE UNDEFINED ITEMS IN THE FORM"
      return
      
    end
  end[/code]

The problem witht this is that (it seems) that I get a false from all the unclicked radio buttons in the groups that have not been selected.
So I always get this pop up , because there are ALWAY some radiobuttons unclicked…

Ideas ?

How many total radio buttons do you have in the window? Rather than iterating through ALL controls, have you considered converting your radio buttons to control arrays (one array per group)? Then you could quickly loop through JUST the radio buttons, rather than all controls.

The radio button are already control sets (if that’s what you mean)…
Anyway… what difference does it make ?

Why do you check for Value = False if you just want to check if there’s a Value = True? Loop through the Sets and Exit the Loop once it hits a Value = True, if it does not exit before the highest Index is reached, there’s no Value = True in the Set.

An example (not actual working code):

For attempt = 1 To 10 If Value = True Then // Found a checked Button Exit End If Next If attempt > 10 Then // No checked Button found End If

Or add 1 Property for each Set with a default of -1 and change it to the Index, once a RadioButton has been set to True? No Loops needed. :wink:

Speed? :slight_smile:

Even if your current implementation does not show any performance hit, i’d always recommend to look for ways to reduce the workload in your code. Most times, this also reduces the code needed to accomplish goals and makes code more readable. At least that’s how I experience it.

When you move into a new set of radios (you decide how to check for that, affix on name of control, i.e. radio_set_1, radio_set_2?) set a variable inside the loop to false. If you find a radio set to true, change this internal variable to true, when you get to the end of this set of radios or start the next set of radios, check that variable, if its still false then pop up your message. You should then only receive one popup per control group. Its then a small step to keep a variable outside the loop that checks for a single false at the end of each set of radios to just show the message once for all controls when you are done traversing all controls.

Let me know if you need a demo, the IDE crashed while I was making one for you.

Thanks for your replies, your time and your ideas… I’ve finally got around it by creating a dictionary that keeps the RadioButton Name and a boolean value with the state of that button. It’s probably not the most efficient but since the dictionary is quite small (10-20 entries) the lookup is very fast.
Anyway… first I had to do it… then do it properly… It’s the learning process, isnt it ? :slight_smile: