Page 314 - Excel Progamming Weekend Crash Course
P. 314

r540629 ch22.qxd  9/2/03  9:35 AM  Page 289




                  Session 22 — A User Form Example                                       289

                      ValidateData = False
                      Exit Function
                  End If
                  If txtAddress.Value = “” Then
                      MsgBox “You must enter an address.”
                      ValidateData = False
                      Exit Function
                  End If
                  If txtCity.Value = “” Then
                      MsgBox “You must enter a city.”
                      ValidateData = False
                      Exit Function
                  End If
                  If cmbStates.Value = “” Then
                      MsgBox “You must select a state.”
                      ValidateData = False
                      Exit Function
                  End If
                  If txtZip.TextLength <> 5 Then
                      MsgBox “You must enter a 5 digit zip code.”
                      ValidateData = False
                      Exit Function
                  End If

                  ValidateData = True

                  End Function



               Part 6: Completing the Project
               To be complete, this project needs only the Click event procedures for the three
               CommandButton controls. To reiterate, this is what the command buttons should do:

                   The Next button validates the data. If validation succeeds, the data is entered in
                   the worksheet, and the form is cleared for entry of the next address. If validation
                   fails, the form retains its data so the user can correct it as needed.
                   The Done button performs the same tasks as the Next button with one exception:
                   If validation succeeds, the form closes after the data is entered in the worksheet.
                   The Cancel button discards any data currently entered in the form and then closes
                   the form.
                  You may have noticed that the Done and Next buttons share a task, which is entering
               the validated data in the worksheet. Whenever a task needs to be performed in more than
               one situation, a programmer recognizes this as an opportunity for putting the required code
               in a procedure. If you create a procedure that transfers the data from the form to the work-
               sheet, this procedure can be called by both the Done and the Next button’s Click event
               procedure.
   309   310   311   312   313   314   315   316   317   318   319