20 textboxes and 10 comboboxes on multipage1.

D

Damil4real

I have a multipage userform with about 20 textboxes and 10 comboboxes
on multipage1.

User is to click on "next" to go to multipage2.

Once the user clicks on "next" to move to multipage2, I want to be
able to validate each textbox & combobox on page1 to make sure all
enabled textboxes & comboboxes are not blank. If any is blank, message
box notifying user and then setFocus to that textbox or combo so user
can enter data. If all non-disabled textboxes & comboboxes on
multipage1 are full complete, then move to multipage2.

I'm currently about to start writing IF statements, but I don't want
to have to write 40 IF statements!

I appreciate any assistance.

Thanks!
 
H

Harald Staff

Writing user friendly apps is pretty hard work. Write your 40 statements.
(Or write a macro that writes 40 statements ;-)

But Messageboxes saying "You did not do everything right" is an bad user
experience. We want people to love our programs.
I'd do it this way: Disable the "Next" button, call a single central test
routine on all the controls' Exit event and don't enable "Next" until
entries are ok.

HTH. Best wishes Harald
 
D

Damil4real

Writing user friendly apps is pretty hard work. Write your 40 statements.
(Or write a macro that writes 40 statements ;-)

But Messageboxes saying "You did not do everything right" is an bad user
experience. We want people to love our programs.
I'd do it this way: Disable the "Next" button, call a single central test
routine on all the controls' Exit event and don't enable "Next" until
entries are ok.

HTH. Best wishes Harald













- Show quoted text -

Thanks for your response. And how do I call a single central test
routine on all the controls' Exit event? There are going to be some
textboxes that will be blank & disabled because based they might not
be applicable. I want those type of textboxes to be exempt when the
code is checking whether to enable the "Next" button or not.

Thanks for your help!
 
D

Dave Peterson

I think it would be better to check the status of each control after each
change. Then if everything is ok, you can enable the Next button.

If something is wrong, then the Next button is disabled.

I created a userform with a single multipage with 3 pages (Page 1, Page 2, and
Page 3).

I put 3 textboxes and 1 combobox on Page 1.
I put 3 textboxes and 1 combobox on Page 2.
I put 2 textboxes and 0 combobox on Page 3.

The textboxes were named: TextBox1, ..., TextBox8.
The comboboxes were named: Combobox1 and Combobox2.

I put 3 commandbuttons under the multipage.

This is the code behind the userform.

Option Explicit
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub CommandButton2_Click()
'next/ok button
With Me.MultiPage1
If .Value < .Pages.Count - 1 Then
.Pages(.Value + 1).Visible = True
.Pages(.Value).Visible = False
Call ChkNextCmdBtn(.Pages(.Value))
Else
'real code that does the work here!
MsgBox "everything has been entered!"
End If
End With
End Sub
Private Sub CommandButton3_Click()
'Previous
With Me.MultiPage1
If .Value = 0 Then
'do nothing
Else
.Pages(.Value - 1).Visible = True
.Pages(.Value).Visible = False
Call ChkNextCmdBtn(.Pages(.Value))
End If
End With
End Sub
'controls on Page 1 of the multipage
Private Sub TextBox1_Change()
Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
Private Sub TextBox2_Change()
Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
Private Sub TextBox3_Change()
Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
Private Sub ComboBox1_Change()
Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
'controls on Page 2 of the multipage
Private Sub TextBox4_Change()
Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
Private Sub TextBox5_Change()
Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
Private Sub TextBox6_Change()
Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
Private Sub ComboBox2_Change()
Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
'controls on Page 3 of the multipage
Private Sub TextBox7_Change()
Call ChkNextCmdBtn(Me.MultiPage1(2))
End Sub
Private Sub TextBox8_Change()
Call ChkNextCmdBtn(Me.MultiPage1(2))
End Sub
Private Sub ChkNextCmdBtn(myPage As MSForms.Page)

Dim OkToEnable As Boolean
Dim ctrl As Control

OkToEnable = True
For Each ctrl In myPage.Controls
If TypeOf ctrl Is MSForms.TextBox Then
If ctrl.Value = "" Then
OkToEnable = False
Exit For
End If
ElseIf TypeOf ctrl Is MSForms.ComboBox Then
If ctrl.ListIndex < 0 Then
OkToEnable = False
Exit For
End If
End If
Next ctrl

'Previous button, disabled on the first page, enabled on others
Me.CommandButton3.Enabled = CBool(myPage.Index <> 0)

'Next Button
Me.CommandButton2.Enabled = OkToEnable

'on last page? if yes, then change the caption.
If myPage.Index = Me.MultiPage1.Pages.Count - 1 Then
Me.CommandButton2.Caption = "Ok"
Me.CommandButton2.ControlTipText = "Run the program"
Else
Me.CommandButton2.Caption = "Next"
Me.CommandButton2.ControlTipText = "Advance to the next Step"
End If

End Sub
Private Sub UserForm_Initialize()

Dim iCtr As Long

'just some test data
With Me.ComboBox1
.AddItem "A"
.AddItem "B"
.Style = fmStyleDropDownList
End With

With Me.ComboBox2
.AddItem "w"
.AddItem "x"
.Style = fmStyleDropDownList
End With

With Me.CommandButton1
.Caption = "Cancel"
.Cancel = True
.TakeFocusOnClick = False
.Enabled = True
End With

With Me.CommandButton2
.Tag = "Next"
.Caption = "Next"
.ControlTipText = "Advance to the next Step"
.Enabled = False
End With

With Me.CommandButton3
.Tag = "Previous"
.Caption = "Previous"
.ControlTipText = "Retreat to the previous Step"
.Enabled = False
End With

With Me.MultiPage1
.Value = 0
.Pages(0).Visible = True
For iCtr = 1 To .Pages.Count - 1
.Pages(iCtr).Visible = False
Next iCtr
End With

End Sub
 
H

Harald Staff

Writing user friendly apps is pretty hard work. Write your 40 statements.
(Or write a macro that writes 40 statements ;-)

But Messageboxes saying "You did not do everything right" is an bad user
experience. We want people to love our programs.
I'd do it this way: Disable the "Next" button, call a single central test
routine on all the controls' Exit event and don't enable "Next" until
entries are ok.

HTH. Best wishes Harald













- Show quoted text -
Thanks for your response. And how do I call a single central test
routine on all the controls' Exit event? There are going to be some
textboxes that will be blank & disabled because based they might not
be applicable. I want those type of textboxes to be exempt when the
code is checking whether to enable the "Next" button or not.

Thanks for your help!

Like this

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.BtnNext.Enabled = Satisfied
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Me.BtnNext.Enabled = Satisfied
End Sub

'and so on for all controls

Private Function Satisfied() As Boolean
Satisfied = True
If Me.TextBox1.Enabled = True And _
Me.TextBox1.Text = "" Then Satisfied = False
If Me.TextBox2.Enabled = True And _
Me.TextBox2.Text = "" Then Satisfied = False
'and so on for all controls
End Function

HTH. Best wishes Harald
 
D

Damil4real

I think it would be better to check the status of each control after each
change.  Then if everything is ok, you can enable the Next button.

If something is wrong, then the Next button is disabled.

I created a userform with a single multipage with 3 pages (Page 1, Page 2, and
Page 3).

I put 3 textboxes and 1 combobox on Page 1.
I put 3 textboxes and 1 combobox on Page 2.
I put 2 textboxes and 0 combobox on Page 3.

The textboxes were named:  TextBox1, ..., TextBox8.
The comboboxes were named: Combobox1 and Combobox2.

I put 3 commandbuttons under the multipage.

This is the code behind the userform.

Option Explicit
Private Sub CommandButton1_Click()
    Unload Me
End Sub
Private Sub CommandButton2_Click()
    'next/ok button
    With Me.MultiPage1
        If .Value < .Pages.Count - 1 Then
            .Pages(.Value + 1).Visible = True
            .Pages(.Value).Visible = False
            Call ChkNextCmdBtn(.Pages(.Value))
        Else
            'real code that does the work here!
            MsgBox "everything has been entered!"
        End If
    End With
End Sub
Private Sub CommandButton3_Click()
    'Previous
    With Me.MultiPage1
        If .Value = 0 Then
            'do nothing
        Else
            .Pages(.Value - 1).Visible = True
            .Pages(.Value).Visible = False
            Call ChkNextCmdBtn(.Pages(.Value))
        End If
    End With
End Sub
'controls on Page 1 of the multipage
Private Sub TextBox1_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
Private Sub TextBox2_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
Private Sub TextBox3_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
Private Sub ComboBox1_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(0))
End Sub
'controls on Page 2 of the multipage
Private Sub TextBox4_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
Private Sub TextBox5_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
Private Sub TextBox6_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
Private Sub ComboBox2_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(1))
End Sub
'controls on Page 3 of the multipage
Private Sub TextBox7_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(2))
End Sub
Private Sub TextBox8_Change()
   Call ChkNextCmdBtn(Me.MultiPage1(2))
End Sub
Private Sub ChkNextCmdBtn(myPage As MSForms.Page)

    Dim OkToEnable As Boolean
    Dim ctrl As Control

    OkToEnable = True
    For Each ctrl In myPage.Controls
        If TypeOf ctrl Is MSForms.TextBox Then
            If ctrl.Value = "" Then
                OkToEnable = False
                Exit For
            End If
        ElseIf TypeOf ctrl Is MSForms.ComboBox Then
            If ctrl.ListIndex < 0 Then
                OkToEnable = False
                Exit For
            End If
        End If
    Next ctrl

    'Previous button, disabled on the first page, enabled on others
    Me.CommandButton3.Enabled = CBool(myPage.Index <> 0)

    'Next Button
    Me.CommandButton2.Enabled = OkToEnable

    'on last page?  if yes, then change the caption.
    If myPage.Index = Me.MultiPage1.Pages.Count - 1 Then
        Me.CommandButton2.Caption = "Ok"
        Me.CommandButton2.ControlTipText = "Run the program"
    Else
        Me.CommandButton2.Caption = "Next"
        Me.CommandButton2.ControlTipText = "Advance to the nextStep"
    End If

End Sub
Private Sub UserForm_Initialize()

    Dim iCtr As Long

    'just some test data
    With Me.ComboBox1
        .AddItem "A"
        .AddItem "B"
        .Style = fmStyleDropDownList
    End With

    With Me.ComboBox2
        .AddItem "w"
        .AddItem "x"
        .Style = fmStyleDropDownList
    End With

    With Me.CommandButton1
        .Caption = "Cancel"
        .Cancel = True
        .TakeFocusOnClick = False
        .Enabled = True
    End With

    With Me.CommandButton2
        .Tag = "Next"
        .Caption = "Next"
        .ControlTipText = "Advance to the next Step"
        .Enabled = False
    End With

    With Me.CommandButton3
        .Tag = "Previous"
        .Caption = "Previous"
        .ControlTipText = "Retreat to the previous Step"
        .Enabled = False
    End With

    With Me.MultiPage1
        .Value = 0
        .Pages(0).Visible = True
        For iCtr = 1 To .Pages.Count - 1
            .Pages(iCtr).Visible = False
        Next iCtr
    End With

End Sub











--

Dave Peterson- Hide quoted text -

- Show quoted text -

Thanks a great deal for this code! I'm currently reviewing it. But
before I go deep into this code, this code doesn't seem like to make
room for those blank textboxes that might NOT BE APPLICABLE depending
on the answer the user picks...or does it? There will be some blank
textboxes and I will want those to exempt from been completed if they
don't apply.

Thanks!
 
D

Dave Peterson

Harald showed one way to check the .enabled status.

Or you could just keep track and check by name or tag or whatever you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top