Question

I've been struggling with this annoyingly peculiar about setting the option button being selected when clicked.

Here's the my code below.

Private Sub frame_membershiptype_Click()
On Error GoTo Err_frame_membershiptype_Click

Select Case frame_membershiptype
    Case option_standard.Value
        option_standard.SetFocus
        Me.MembershipType = "Standard"

    Case option_concession.Value
        option_concession.SetFocus
        Me.MembershipType = "Concession"


    Case option_concession.Value
        option_lifetime.SetFocus
        Me.MembershipType = "Lifetime"

End Select

Exit_frame_membershiptype_Click:
    Exit Sub

Err_frame_membershiptype_Click:
    MsgBox Err.Description
    Resume Exit_frame_membershiptype_Click

End Sub

I have 3 option buttons in one option group. I'd say that if one of these options buttons is selected (by referring to its Value property), I will set the focus on that specific option button and update the underlying table's record behind it.

But the error came out saying you have no value assigned to these option buttons. Which is confusing me because according to the MSDN documentation having optionbutton.value property will tell me if option button is selected. And I found various online tutorials told me it works for me them this way... But it doesn't work for me!

Why? I'm very puzzled by this. Surely it can't be this hard to implement a simple button checkbox check.

Was it helpful?

Solution

The normal method for handling Option Button controls in an Access form is to make them child controls of an Option Group (a.k.a. Frame) control. You retrieve the selected option from the .Value property of the Option Group control like this:

Option Compare Database
Option Explicit

Private Sub btnSave_Click()
    Dim msg As String
    If IsNull(Me.frame_membershiptype.Value) Then
        msg = "No membership type has been selected."
    Else
        msg = "Membership type selected: "
        Select Case Me.frame_membershiptype.Value
            Case 1
                msg = msg & "Standard"
            Case 2
                msg = msg & "Concession"
            Case 3
                msg = msg & "Lifetime"
            Case Else
                msg = msg & "Unknown"
        End Select
    End If
    MsgBox msg
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top