Bob
Bob

Reputation: 1396

Access: How To See If An Options Radio Button Is Checked

I have 4 "Option" radio buttons that can be checked. If they are checked by a user, then I want to insert a record into a table. Code would be something as follows...

If option1Button is Checked then 
     doInsertQuery
If option2Button is Checked then 
     doInsertQuery
If option3Button is Checked then 
     doInsertQuery
If option4Button is Checked then 
     doInsertQuery

I can't find any documentation on how to do the "is Checked" method. I'm coming from a VB.net background, and VBA definitely doesn't have the toys i'm used to having for this sort of thing.

Upvotes: 3

Views: 8222

Answers (1)

Lee Mac
Lee Mac

Reputation: 16015

Rather than querying the state of each Option Button, you should instead obtain the Value property of the Option Group itself, i.e. the frame surrounding the set of option buttons.

This will yield the Option Value associated with the selected Option Button.

Since only one Option Button may be selected within an Option Group, I would suggest using a VBA Select Case statement when testing the selected value, e.g.:

Select Case YourFrameControl
    Case 1: Debug.Print "Option 1 Selected."
    Case 2: Debug.Print "Option 2 Selected."
    Case 3: Debug.Print "Option 3 Selected."
End Select

Since the Value property is the default value returned by a control, it needn't be explicitly stated.


If the Option Buttons are standalone and are not a member of an Option Group, then you can query the Value property of the Option Button itself, which will yield a True (-1) or False (0) value, or True (-1), False (0), or Null for a Triple-State Option Button.

In this case, I would suggest using checkboxes instead of radio buttons, since this would be more intuitive for an interface permitting selection of multiple options.

Since you state that the user may select "all options that apply", you would likely need to use a sequence of separate if statements to evaluate an expression for each selected option.

If you are only evaluating a single expression, you could write the if statements in line:

If YourOptionControl1 Then Debug.Print "Option 1 selected."
If YourOptionControl2 Then Debug.Print "Option 2 selected."
If YourOptionControl3 Then Debug.Print "Option 3 selected."

Or alternatively, as separate if blocks:

If YourOptionControl1 Then
    Debug.Print "Option 1 selected."
End If
If YourOptionControl2 Then
    Debug.Print "Option 2 selected."
End If
If YourOptionControl3 Then
    Debug.Print "Option 3 selected."
End If

Again, since the Value property is the default value returned by a control, it needn't be explicitly stated; for the above, I've assumed you are not using triple-state option buttons.

Upvotes: 4

Related Questions