MJ98
MJ98

Reputation: 87

Deselecting option buttons when selected together

on my UserForm, I have created option buttons adjacent to the topic I want my users to choose i.e. if they choose "mechanical engineering" there is a "yes" option button or a "no" option button to the right of it. However, users cannot select classes "mechanical engineering" and "chemical engineering" together. I have entered the first code below which works. However, it only works if option button "Me.optFMME" and then "Me.optFMCE" in that order. If you choose them in the other order i.e."Me.optFMCE" and then "Me.optFMME", it allows it. So, I entered code (the second one undernearth it) to counteract this but it doesn't do anything.

Please if anyone has any guidance, it would be much appreciated.

Private Sub optFMME_Click()

    If Me.optFMME And Me.optFMCE Then
        MsgBox "Mech Eng and Chem Eng cannot be selected together due to similar     material. Please select another combination."
        Me.optFMME = False
        Me.optFMCE = False
    End If
End Sub

`

Private Sub optFMCE_Click()

    If Me.optFMCE And Me.optFMME Then
        MsgBox "Mech Eng and Chem Eng cannot be selected together due to similar     material. Please select another combination."
        Me.optFMCE = False
        Me.optFMME = False
    End If
End Sub

Upvotes: 2

Views: 428

Answers (1)

PeterT
PeterT

Reputation: 8557

Your logic for checking the option buttons is identical, so it should be isolated into a separate function. In this way, you remain consistent in your logic and if something needs to be adjusted, it's all in one place.

Private Sub optFMME_Click()
    LogicCheck
End Sub

Private Sub optFMCE_Click()
    LogicCheck
End Sub

Private Sub LogicCheck()
    If Me.optFMCE And Me.optFMME Then
        MsgBox "Mech Eng and Chem Eng cannot be selected together " & _
               "due to similar material. Please select another combination."
        Me.optFMCE = False
        Me.optFMME = False
    End If
End Sub

Upvotes: 1

Related Questions