Professional Documents
Culture Documents
In Visual Basic, you use variables to temporarily store values during the execution of an application. Variables have a name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store).
Declaring Variables
To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable: Dim variable name [As type] Dim num as integer Dim str1 as string
Dim statement
Variables declared with the Dim statement within a procedure exist only as long as the procedure is executing. When the procedure finishes, the value of the variable disappears. In addition, the value of a variable in a procedure is local to that procedure that is, you can't access a variable in one procedure from another procedure. These characteristics allow you to use the same variable names in different procedures without worrying about conflicts or accidental changes.
Implicit Declaration
We don't have to declare a variable before using it. For example, we could write a function where you don't need to declare TempVal before using it. Visual Basic automatically creates a variable with that name, which you can use as if you had explicitly declared it. While this is convenient, it can lead to subtle errors in your code if you misspell a variable name.
Explicit Declaration
To avoid the problem of misnaming variables, you can stipulate that Visual Basic always warn you whenever it encounters a name not declared explicitly as a variable.
Data types
Boolean: True or False values Byte: Binary Data type Currency: Decimal fractions such as dollars and cents Date: An 8 character date Double: Double precision floating point numbers with 14 digits of accuracy. Integer: Whole numbers in the range -32768 to 32767. Long: Larger whole numbers. Single: Single precision floating point numbers with 6 digits of accuracy. String: Alphanumeric data, letters digits and other characters. Variant: Converts from one type to other as and when needed.
Boolean
If you have a variable that will contain simple true/false, yes/no, or on/off information, we can declare it to be of type Boolean. The default value of Boolean is False.
Numeric Data data types Integer, Long Types Visual Basic supplies several numeric
(long integer), Single (single-precision floating point), Double (double-precision floating point), and Currency. Using a numeric data type generally uses less storage space than a variant. If you know that a variable will always store whole numbers (such as 12) rather than numbers with a fractional amount (such as 3.57), declare it as an Integer or Long type. Operations are faster with integers, and these types consume less memory than other data types. They are especially useful as the counter variables in For...Next loops. If the variable contains a fraction, declare it as a Single, Double, or Currency variable. The Currency data type supports up to four digits to the right of the decimal separator and fifteen digits to the left; it is an accurate fixed-point data type suitable for monetary calculations. Floating-point (Single and Double) numbers have much larger ranges than Currency, but can be subject to small rounding errors.
Val() function
Returns the numbers contained in a string as a numeric value of appropriate type. Dim myval as integer Myval=val(text1.text)
Arithmetic operations
Exponentiation(^) Multiplication(*) and division(/) Addition(+) and subtraction(-)
Returns an expression formatted as a currency value using the currency symbol defined in the system control panel FormatCurrency(Expression,NumDigitsAfterDecimal ,IncludeLeadingDigit ,UseParensForNegativeNumbers ,GroupDigits ) IncludeLeadingDigit, UseParensForNegativeNumbers, GroupDigits arguments have the following settings: ConstantValueDescription vbTrue 1 vbFalse 0 vbUseDefault 2(Use the setting from the computer's regional
settings.)
FormatCurrency Function
FORMATCURRENCY(-10,5,VBTRUE,VBTRUE) ($10.00000)
NumDigitsAfterDecimal:
Optional. Numeric value indicating how many places to the right of the decimal are displayed. Default value is 1, which indicates that the computer's regional settings are used. IncludeLeadingDigit: Optional. constant that indicates whether or not a leading zero is displayed for fractional values. See Settings section for values. UseParensForNegativeNumbers: Optional. constant that indicates whether or not to place negative values within parentheses. See Settings section for values. GroupDigitsOptional.: constant that indicates whether or not numbers are grouped using the group delimiter LIKE ,
Formatnumber function
Returns an expression formatted as a number.
FormatNumber(Expression,NumDigitsAfterDecimal ,IncludeLeadingDigit ,UseParensForNegativeNumbers ,GroupDigits) Form1.Print (FormatNumber(5000780089, 8, vbTrue, vbTrue, vbTrue))
FormatPercent Function
Returns an expression formatted as a percentage (multipled by 100) with a trailing % character.
FormatPercent(Expression,NumDigitsAfterDecimal ,IncludeLeadingDigit ,UseParensForNegativeNumbers ,GroupDigits) Form1.Print (FormatPercent(0.78, 2, vbFalse, vbTrue, vbTrue)) 78.00%
FormatDateTime Function
Returns an expression formatted as a date or time.
The FormatDateTime function syntax has these parts:
Date:Required. Date expression to be formatted. NamedFormat:Optional. Numeric value that indicates the
If...Then...Else Statement
Conditionally executes a group of statements, depending on the value of an expression. If condition Then [statements] [ElseIf condition-n Then [elseifstatements] ... [Else [elsestatements]] End If
InputBox Function
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box. InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])
Msgbox function
Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked. MsgBox(prompt[, buttons] [, title] [, helpfile, context]) Prompt: String expression displayed as the message in the dialog box. Buttons: Numeric expression that is the sum of values specifying the number and type of buttons to display. Title:. String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.