Reputation: 61
I'm setting up a form to help users determine the data to input (option group 1) and the method of input (option group 2).
Once they have made those choices, I need to enable a button which will open the appropriate form to use for the data input.
I have tried to use WithEvents in a class module to detect when the option buttons have been clicked, assign and store the value of 1 when an option is chosen. My plan was to update a hidden label's caption on the form with the number 2 once two option buttons have been clicked. However, I'm failing to access and update the label under this plan.
Upvotes: 1
Views: 207
Reputation: 6829
Standard approach is to use a for each
loop to ensure that option buttons are clicked before moving on in the subroutine for the commandbutton_click.
For each ctrl in userform1.controls
if typename(ctrl) = "optionbutton" then
'do something
end if
next ctrl
You could also assign a global variable once an option button is checked to ensure that you have both items both items marked, such as:
public o1 as string, o2 as string
private sub optionbutton1_click()
o1 = "x"
end sub
private sub optionbutton2_click()
o2 = "x"
end sub
private sub commandbutton1_click()
if o1 = "" and o2 = "" then exit sub
o1 = ""
o2 = ""
'Put in rest of your code
end sub
Edit1:
Example of the for each loop... this isn't a class module, but is a few lines of code to determine which frame (f1 and f2 are frames 1 and 2) is filled in.
Private Sub CommandButton1_Click()
Dim ctrl As Control, f1 As Long, f2 As Long
For Each ctrl In Me.Controls
If TypeName(ctrl) = "OptionButton" Then
If ctrl.Value = True Then
If Right(ctrl.Caption, 1) = 1 Or Right(ctrl.Caption, 1) = 2 Then f1 = 1
If Right(ctrl.Caption, 1) = 3 Or Right(ctrl.Caption, 1) = 4 Then f2 = 1
End If
End If
Next ctrl
If Not f1 + f2 = 2 Then Exit Sub
'do something
Unload Me
End Sub
Upvotes: 2
Reputation: 12167
If you want to use class modules take this approach. I made to classes optBtn
and optBtns
This is the class optBtn
Option Explicit
Private WithEvents mOptBtn As MSForms.OptionButton
Dim mSelected As Boolean
Dim mParent As optBtns
Private Sub mOptBtn_Change()
mSelected = True
End Sub
Property Get selected() As Boolean
selected = mSelected
End Property
Property Set btn(nBtn As MSForms.OptionButton)
Set mOptBtn = nBtn
End Property
Property Set parent(nParent As optBtns)
Set mParent = nParent
End Property
Private Sub mOptBtn_Click()
mParent.Raise
End Sub
This is the class optBtns
Option Explicit
Dim mOpt As Collection
Public Event Clicked()
Public Sub add(ByRef opt As optBtn)
mOpt.add opt
End Sub
Public Sub Raise()
RaiseEvent Clicked
End Sub
Public Function selected() As Boolean
Dim i As Long
For i = 1 To mOpt.Count
If mOpt(i).selected Then
selected = True
Exit For
End If
Next i
End Function
Private Sub Class_Initialize()
Set mOpt = New Collection
End Sub
In the userform I have
Option Explicit
Private WithEvents optGrp1 As optBtns
Private WithEvents optGrp2 As optBtns
Private Sub OptGrp1_Clicked()
MyAction
End Sub
Private Sub OptGrp2_Clicked()
MyAction
End Sub
Private Sub MyAction()
Debug.Print "Event raied"
If optGrp1.selected And optGrp2.selected Then
CommandButton1.Enabled = True
Else
CommandButton1.Enabled = False
End If
End Sub
Private Sub UserForm_Initialize()
Dim sngOptBtn As optBtn
Dim i As Long
Set optGrp1 = New optBtns
Set optGrp2 = New optBtns
For i = 1 To 3
Set sngOptBtn = New optBtn
Set sngOptBtn.btn = Controls("Optionbutton" & i)
Set sngOptBtn.parent = optGrp1
optGrp1.add sngOptBtn
Next i
For i = 4 To 5
Set sngOptBtn = New optBtn
Set sngOptBtn.btn = Controls("Optionbutton" & i)
Set sngOptBtn.parent = optGrp2
optGrp2.add sngOptBtn
Next i
End Sub
The userform looks like that
Name of the option buttons are just Optionbutton1 through to Optionbutton5
Feel free to enhance the code.
Upvotes: 2
Reputation: 2184
I am not sure if i clearly understood your request, but here is my solution assuming option buttons naming scheme is OptionButton#X#_group#Y#
Option Explicit
Dim Opt_group_1 As Long
Dim Opt_group_2 As Long
Private Sub OptionsUpdated()
CommandButton1.Enabled = (Opt_group_1 <> 0 And Opt_group_2 <> 0)
End Sub
Private Sub OptionButton1_group1_Click()
Opt_group_1 = 1
Call OptionsUpdated
End Sub
Private Sub OptionButton1_group2_Click()
Opt_group_2 = 1
Call OptionsUpdated
End Sub
Private Sub OptionButton2_group1_Click()
Opt_group_1 = 2
Call OptionsUpdated
End Sub
Private Sub OptionButton2_group2_Click()
Opt_group_2 = 2
Call OptionsUpdated
End Sub
Private Sub OptionButton3_group1_Click()
Opt_group_1 = 3
Call OptionsUpdated
End Sub
Upvotes: 2