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