This tutorial is marked as an intermediate tutorial. Please make sure you know and understand the following things: * Variables (using and declaring) * If, IfElse and Else statements * Message Boxes * With Blocks We will start by making the following form: Project name: Validate Form Name: frmValidate
Alright, now that we have our form set up, lets get the easy coding out of the way. Double click the "Exit" button and insert the following code block for the click event procedure: end Close out of the code window. Now, double click the "Execute" button to open the code window. Make sure you still on the click event procedure. I’m going to put the entire code right now, and then break it down in the next few steps. Insert the following code (don't copy and paste, you do not learn that way)
for the click event procedure of
cmdExecute Dim strname As String Dim sngage As Single If txtname.Text = "" Then MsgBox "Enter your name", , "Missing Data" txtname.SetFocus Exit Sub ElseIf IsNumeric(txtname.Text) Then MsgBox "Numerals are not allowed in the text box, enter your name.", , "Invalid Data" With txtname .SelStart = 0 .SelLength = Len(txtname.Text) .SetFocus End With Exit Sub Else strname = txtname.Text If txtage.Text = "" Then MsgBox "Enter a number greater than zero", , "Missing Number" txtage.SetFocus Exit Sub ElseIf Not IsNumeric(txtage.Text) Then MsgBox "Enter numerals and no other characters", , "Invalid Data" With txtage .SelStart = 0 .SelLength = Len(txtage.Text) .SetFocus End With Exit Sub ElseIf IsNumeric(txtage.Text) And Val(txtage.Text) <= 0 Then MsgBox "Age must be greater than zero", , "Age Out of Accepted Range" With txtage .SelStart = 0 .SelLength = Len(txtage.Text) .SetFocus End With Exit Sub Else sngage = txtage.Text End If End If lbloutput.Caption = "Hello " & strname & ". You are " & sngage & " years old."
Press play and you will see the following types of errors: * If you don’t enter your name or age, you receive an error. * If your name contains numbers, you will receive an error * If your age is less than zero, you will receive an error
Alright, now for the synopsis. I will break down the above code, block by block. The first thing we do is declare the variables we will be using. Most times I will declare my variables in General Declarations (See Variable tutorial), but this time, since they are only local variables, I will declare them in the event procedure for cmdExecute. Dim strname As String Dim sngage As Single We have told the program we will be using one "string" variable and one "single" variable.
Next we begin validating the information the user puts in. The first if block is checking whether or not txtName is empty or not. If it is, an alert pops up (see message box tutorial) telling the user they forgot to fill out a field. The user clicks okay, and the cursor is automatically placed in the Name field. With the "elseif" statement, we are saying: if the above is true, then check this. We are checking to see if txtName holds any numerical data. We do this by utilizing the isnumerical() function. If this turns out true, the same thing happens as before, except this time we have used selstart and sellength to highlight the data in the textbox.
The next thing we are doing is saying that if the data entered into the txtName text box is not numeric and not invalid, then set strName equal to the text in txtName. Now we begin validating the information in the age text box. The first step is the same as before, check to see if the text box is empty. It is done the same way as was done with the txtName field. Now we check to see if txtAge is not a numerical value. This is accomplished by placing a “not” before the isnumerical() function: elseif not isnumerical(txtage.text) then If the data entered in txtAge is not a number, then an alert box notifies the user they must enter a number, greater than zero, into the text box.
Now that we have determined whether or not the info in txtAge is a number, we must determine whether or not this number is negative. You can’t be -13 years old, can you? We need to use another elseif. We tell the program that if txtage is a number and if that number is less than or equal to zero, display another alert box notifying the user they need to enter a number greater than zero. If all of the above criteria are met for txtAge, then we finally tell the program to set sngAge = txtAge.text. We must now close our If statements. Remember, we have opened two if statements, which means we need to close two if statements. If you don’t, it will result in errors. Now the last thing to do is set the caption of lblOutPut to display: “Hello {name}. You are {age} years old.”
Good work, I hope this helped you better understand how to validate data with VB.
Comments
Post new comment