txtPhone.Value & "") + Len(Me.txtState.Value & "")) <> 0 Then
Once again, we use the Len function to help us make this determination. If the length of
both strings summed together is greater than 0, then at least one of the fields contains data.
If the result is True, then we prompt the user as to whether they want to continue with the
new record and throw out the existing data.
CHAPTER 4 n USERFORMS 146
The Cancel command button has a very simple job: clearing the form and closing it.
Here is the code for the Cancel button:
Private Sub cmdCancel_Click()
ClearForm
Unload UserForm1
End Sub
The last step is to create a procedure in a standard module to launch our Customer Survey
form. In the VBE, add a new standard code module and create a new subroutine named
ShowForm. Add the following line of code to the procedure.
Sub ShowForm()
UserForm1.Show
End Sub
If you renamed your UserForm object, use that name in place of UserForm1. Let??™s run our
form and enter some data. In Excel, run the ShowForm macro, as shown in Figure 4-8.
Figure 4-8. Running the ShowForm macro
The Customer Survey formdisplays. The ID displayed is 103, and as you can see in
Figure 4-9, the last entry in the table is 102. Also note that row 5 is the next row available for
data.
Let??™s check our code. Enter NY in the State text box, but leave the phone number field
blank, and then click the Save button.
Figure 4-10 shows that our cCustSurvey??™s ValidateData method finds that neither piece of
required data is present, and returns a message to our client code in the UserForm.
Pages:
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167