Message box to appear if option not selected

joacro

Board Regular
Joined
Jun 24, 2010
Messages
158
Hi there

I wish that a message box to appear if no option is selected:
- if txtCh1 = true and opt 1, opt2 or opt3 is not selected then msgbox (please select age group of child / adult dependant)
- if txtCh2 = true and opt4, opt5 or opt6 is not selected then msgbox (please select age group of child / adult dependant)
- if txtCh3 = true and opt7, opt8 or opt9 is not selected then msgbox (please select age group of child / adult dependant)
- if txtCh4 = true and opt10, opt11 or opt12 is not selected then msgbox (please select age group of child / adult dependant)
- if txtCh5 = true and opt13, opt14 or opt 15is not selected then msgbox (please select age group of child / adult dependant)
- if txtCh6 = true and opt16, opt17 or opt18 is not selected then msgbox (please select age group of child / adult dependant)

Hope someone can help me with this

Thanx in advance

Joacro
 
The rest of the sub will continue after the message box appears. Would it be better to exit the sub when asked to select an age group?
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try adding a command button to the userform. then use this code:

Code:
Private Sub CommandButton1_Click()
Dim i As Long
Dim j As Long
j = 0
    For i = 1 To 6
        If UCase(Me.Controls("txtCh" & i).Value) = "TRUE" And Me.Controls("opt" & j + 1).Value = False And _
                Me.Controls("opt" & j + 2).Value = False And Me.Controls("opt" & j + 3).Value = False Then
            MsgBox "please select age group of child / adult dependant"
        End If
        j = j + 3
    Next i
End Sub

call the userform and test to see if anything happens...
 
Upvote 0
This assumes that the option buttons are grouped,
opt1, opt2, opt3
opt4, opt5, opt6
opt7, opt8, opt9
etc
and that each of those grouped triplets are associated with one textbox.

This approach allows the arbitrary assignment of those correspondences. Instead of depending on a numbering/naming convention, the name of the text box is put in one option button of the associated group.

Code:
Private Sub UserForm_Initialize()
    With Me
        .Opt1.Tag = "txtCh1"
        .Opt4.Tag = "txtCh2"
        .Opt7.Tag = "txtCh3"
        .Opt10.Tag = "txtCh4"
        .Opt13.Tag = "txtCh5"
        .Opt17.Tag = "txtCh6"
    End With
End Sub

Private Sub CommandButton1_Click()
    Dim oneControl As MSForms.Control
    For Each oneControl In Me.Controls
        If TypeName(oneControl) = "OptionButton" Then
            If pollOptGroup(oneControl) Then
                MsgBox "please select age group of child / adult dependant"
                oneControl.SetFocus
                Exit Sub
            End If
        End If
    Next oneControl
End Sub

Function pollOptGroup(anOpt As MSForms.OptionButton) As Boolean
    Dim groupName As String
    Dim oneCont As MSForms.Control
    Dim textName As String
    
    groupName = anOpt.groupName
   
    For Each oneCont In anOpt.Parent.Controls
        With oneCont
            If TypeName(oneCont) = "OptionButton" Then
                If .groupName = groupName Then
                    pollOptGroup = pollOptGroup Or .Value
                    textName = textName & .Tag
                End If
            End If
        End With
    Next oneCont
    
    On Error Resume Next
    pollOptGroup = Not (pollOptGroup) Imp (LCase(Me.Controls(textName).Text) = "true")
    On Error GoTo 0
End Function
 
Upvote 0
This assumes that the option buttons are grouped,
opt1, opt2, opt3
opt4, opt5, opt6
opt7, opt8, opt9
etc
and that each of those grouped triplets are associated with one textbox.

This approach allows the arbitrary assignment of those correspondences. Instead of depending on a numbering/naming convention, the name of the text box is put in one option button of the associated group.

Code:
Private Sub UserForm_Initialize()
    With Me
        .Opt1.Tag = "txtCh1"
        .Opt4.Tag = "txtCh2"
        .Opt7.Tag = "txtCh3"
        .Opt10.Tag = "txtCh4"
        .Opt13.Tag = "txtCh5"
        .Opt17.Tag = "txtCh6"
    End With
End Sub
 
Private Sub CommandButton1_Click()
    Dim oneControl As MSForms.Control
    For Each oneControl In Me.Controls
        If TypeName(oneControl) = "OptionButton" Then
            If pollOptGroup(oneControl) Then
                MsgBox "please select age group of child / adult dependant"
                oneControl.SetFocus
                Exit Sub
            End If
        End If
    Next oneControl
End Sub
 
Function pollOptGroup(anOpt As MSForms.OptionButton) As Boolean
    Dim groupName As String
    Dim oneCont As MSForms.Control
    Dim textName As String
 
    groupName = anOpt.groupName
 
    For Each oneCont In anOpt.Parent.Controls
        With oneCont
            If TypeName(oneCont) = "OptionButton" Then
                If .groupName = groupName Then
                    pollOptGroup = pollOptGroup Or .Value
                    textName = textName & .Tag
                End If
            End If
        End With
    Next oneCont
 
    On Error Resume Next
    pollOptGroup = Not (pollOptGroup) Imp (LCase(Me.Controls(textName).Text) = "true")
    On Error GoTo 0
End Function

I cant seem to get it to work. I already have a form UserForm_Initialize
Is it possible for me to send you my sheet to look at?
 
Upvote 0
Try adding a command button to the userform. then use this code:

Code:
Private Sub CommandButton1_Click()
Dim i As Long
Dim j As Long
j = 0
    For i = 1 To 6
        If UCase(Me.Controls("txtCh" & i).Value) = "TRUE" And Me.Controls("opt" & j + 1).Value = False And _
                Me.Controls("opt" & j + 2).Value = False And Me.Controls("opt" & j + 3).Value = False Then
            MsgBox "please select age group of child / adult dependant"
        End If
        j = j + 3
    Next i
End Sub

call the userform and test to see if anything happens...

Added a command button, and pasted the code. I call the form and nothing happens. Any idee why it doesn't seem to work?
 
Upvote 0
The file you sent me does exactly what is required.
When the new command button is pressed:
If a text box contains "true" and no option button (of that group) is selected, the message box appears.
Other wise the message box does not appear.
This includes the case where a text box contains "James" and no option button is selected. (in which case no message box appears)
 
Upvote 0
Thank you for the reply. Sorry I think I gave you the wrong information.

The three option boxes are linked to one textbox.

I need this:

If txtbox.value = true and opt 1 - 3 is not selected then message box should appear. Not just if the value = "true" but if any value is entered.

Does it make any sense???

I also need to need this to be incopporated into my add button and not the extra command button.

Thanks in advance
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,791
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top