Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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”
5
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
6
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
7
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
8
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
9
Set to Default
10
No Default
11
No Option Selected
14
InputBox Function Used to request input from the user The InputBox Function consists of a new form that holds a text box The InputBox Function is similar to the MsgBox In an InputBox you can display a message, called a prompt, and allow the user to type input into the text box VariableName = InputBox(“Prompt”, “Title”)
16
The Prompt must be enclosed in quotation marks (and may include newline characters vbCrLf, if you want the prompt to appear on multiple lines) The Title displays in the title bar of the dialog box, and if the title is missing, the project name appears in the title bar VariableName = InputBox(“Prompt”, “Title”, Default, XPos, YPos) Any value displayed in Default appears in the text box when it is displayed, otherwise the text box is empty (If string default, then must be enclosed in quotation marks) XPos and YPos, if present, define the measurement in twips (1/1440 of an inch) for the left edge and top edge of the box
17
Format Function Used to format data for display, either on the printer or on the screen Predefined Formats User-Defined (Custom) Formats To Format means to control the way output will be presented to the user For example, 12 is just a number, but $12.00 conveys more meaning as dollar amounts (the currency format is used to display dollar amounts) Format$(ExpressionToFormat, “FormatDescription”)
18
Format$(ExpressionToFormat, “FormatDescription”) The optional dollar sign in the function name specifies that the formatted value will be a string, without the dollar sign, the value returned is a variant data type In most cases the results will be the same, but the use of the dollar sign is much more efficient, Why? The expression to be formatted maybe numeric or string, a variable, a literal, or a property. In most cases formats are used for numeric data, to control the number of decimal positions Formats are not generally needed for integer values
19
The format description can take one of several forms VB provides several predefined formats for the most common types of output To select one of these formats, include the format name in quotation marks lblTotal.Caption = Format$(cTotalAmount, “Currency”) A feature of the format function, is that it rounds fractional values to the requested number of decimal places The value is rounded for output, but the stored value does not change, and any further calculations, using the value, will be done with the non-rounded (stored)value
20
If the Predefined Formats do not give the results that you want, you can create your own Custom Formats To help in creating these formats, use the Help Topics provided by VB for: User-Defined Formats
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.