Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data as a result of ‘user error’ Checking to verify that appropriate values have been entered for a text box is called validation The validation may include checking the type of data, checking for specific values, or checking a range of values
Checking the Data Type: IsNumeric Function Used to make sure that the data entered is truly numeric The IsNumeric Function returns ‘True’ or ‘False’ to indicate the result of the value checked IsNumeric(Expression) The function tests whether the value is numeric and therefore can be used in a calculation, therefore helping to avoid problems in procedures that contain calculations
If the data cannot be converted to a number, the calculation cannot be performed and a Run-Time Error will occur, which can be prevented by checking the contents of a field after the data has been entered If IsNumeric(txtQuantity.Text) Then iQuantity = Val(txtQuantity.Text) lblDue.Caption = cPrice * iQuantity End If Checking for a Range of Values: Data Validation may also include checking the reasonableness of a value If Val(txtHours.Text) > 10 Then
Message Boxes If the value entered by a user is incorrect, then the user can be notified by displaying a Message Box A special type of VB Window in which a message can be displayed to a user Validating input data is an appropriate time to use a message box. If data is rejected, the user needs to be informed as to why the desired action was not achieved Msgbox “Message String”, [Buttons/Icons], “Caption of the Titlebar”
The Message String is the message that will appear in the message box The Buttons portion is optional; it determines the command buttons that will be displayed and any icons that will appear If The Caption of Title Bar is omitted, then the project name will appear in the message box title bar Message Boxes can be used as a statement, which displays an ‘OK’ button only, or it can be used a s a function The Message Box Function returns a value indicating which button was pressed
Display a message box in the Else clause of an If statement when checking the validity of data If IsNumeric(txtQuantity.Text) Then iQuantity = Val(txtQuantity.Text) lblDue.Caption = cPrice * iQuantity Else MsgBox “Please enter a numeric value”, vbOKonly, “Error” End If
Message Box (Function) Msgbox “Message String”, [Buttons/Icons], “Caption of the Titlebar” Function Return Values The MsgBox Function returns a value that can be tested in a condition using a value (numbers 1 through 7) or an associated intrinsic constant Button Specification and/or Icons to Display The user can specify which buttons to display by using numbers or the VB intrinsic constants, to choose the buttons and an icon, use a plus sign to add the 2 values together
The Message Box Function (Example) Dim iResponse As Integer iResponse = MsgBox(“Do you wish to continue?”, vbYesNo + vbQuestion, “Continue Processing …..”) If iResponse = vbYes Then Text1.Text = InputBox(“Please enter a value”, “Enter a Value”) Else Text1.Text = “ ” End If
Set to Default
No Default
No Option Selected