The VBA Support Code

As stated earlier, you'll be scaling up the code required for simple dialog boxes to handle the additional complexity of your Wizard. While all Wizards vary, we'll focus our attention on the code to handle the following standard issues:

Rather than discuss each of these issues in a vacuum, it's easier to see the concepts in action by looking at real code. The WizDemo.xls workbook (included on the CD) contains code that addresses all of these issues and more.

Your VBA support code will be divided between two or more modules. One module will be a Standard Module and another will be the Object Module behind your UserForm. The Standard Module will call a UserForm function (in our example, bWizardRun) which will start the Wizard. The UserForm contains all the code for initializing, validating, navigating, and any other routines needed while your Wizard is displayed. Here's the code fragment contained in the mEntry module (a Standard Module) for starting the Wizard.

'''    Call Object Module routine to display the W924959039izard924959039SFSF
    ''' Wizard was NOT cancelled.
    If frmWizardDialog.bWizardRun Then 
        ''' more processing here if needed...
        ''' After your procedure runs remove the
        ''' userform from memory
    Else        ''' User cancelled the WizardSFSF
            '''  Code for any needed cleanup due to cancel
    End If

The frmWizardDialog.bWizardRun is a Public routine in the frmWizardDialog Object Module that displays the dialog and returns True if the user completes the Wizard and False if the user cancels. The complete function is listed below:

'''    Function:    bWizardRun
'''    Returns:        True if user completes the Wizard
'''    Comments:    Shows the Wizard and Unloads it if the
'''                user cancels
Public Function bWizardRun() As Boolean
'''    initialize the Wizard assuming the user will cancel
    mbUserCancelled = True
    frmWizardDialog.Show
    bWizardRun = Not mbUserCancelled
End Function

Calling the Show method of a dialog box automatically fires the Initialize event. In other words, the routine above actually runs all the initialization code, displays the dialog box, validates input, and then runs the code to handle the Wizard results. This occurs because all the code for the Wizard is stored in the UserForm Object Module. When it's finished, it returns a True or False based on your user completing or canceling the Wizard.