Validating the data

Play around with this example some more, and you'll find that it has a small problem: It doesn't ensure that the user actually enters a name into the text box. The following code is inserted in the OKButton_Click procedure before the text is transferred to the worksheet. It ensures that the user enters a name (well, at least some text) in the TextBox. If the TextBox is empty, a message appears, and the focus is set to the TextBox so that the user can try again. The Exit Sub statement ends the procedure with no further action.

' Make sure a name is entered If TextName.Text = "" Then

MsgBox "You must enter a name." TextName.SetFocus Exit Sub End If

0 0

Post a comment