You can create the interface of an application by just dragging controls onto the forms and setting the appropriate properties. But to make the application work you have to put some useful code into it. The code basically consists of procedures that respond to events. A form module is a file consisting of forms and the code within that form. A standard module is a file that holds code not specific to a form.
Any data can broadly be categorised into :
<il>Numeric</il><il>String</il><il>Other (Misc)</il>
For representing such data, Visual Basic supports many different data types. The most common and useful one's are :
- Integer : holds numeric values with no decimal point and a range of -32768 to +32767
- String : consists of 0 to 65400 characters of alphanumeric data. They may contain numbers and special characters.
- Variant : used when the type is unknown. Can hold any type of data.
To declare variables, we have to use the 'Dim' statement. 'Dim' stands for dimension. The syntax for the Dim statements is
Dim <Variable Name> As <Datatype>
If you declare a statement 'Option Explicit' at the top of the form or standard module, you have to declare the variable before you use them. Otherwise you may start using a variable before declaring it. Such a variable will be given the 'Variant' datatype.
Examples :
- Dim A As Integer
- Dim FirstName As String
- Dim Value As Variant
The last statement can also be written as 'Dim Value'. If you don't define the datatype, the Visual Basic IDE will assume that it is a 'Variant' datatype. If you want to limit the size of a string, you can do so with the following statement,
Dim FirstName As String * 10
This will limit the length to 10 characters. If you enter more than 10 characters, Visual Basic will truncate the string and store only 10 characters.
To assign value to a variable, we have to use an assignment statement. This is done by using the '=' operator. The general syntax for this statement is : <Variable Name> = <Expression>. Needless to say that the expression must hold the same data type as the datatype of the variable.
Examples :
A = 35
FirstName = "Rae"
If you have a form with a text box, which is used to input or get the first name of a person, and you want to put the name into a variable, say FirstName, then use the following statement :
FirstName = txtName.Text
The above statement assumes that the name of the text box is defined as 'txtName'.
To concatenate two strings, we have to use '&'. Look at the following example:
strFullName = txtFirst.Text & " " & txtLast.Text
The space is included for ease of understanding, as Visual Basic does not by default include a space. You can also use '+' instead of '&', but the latter is more used to remove any ambiguity.
References :
Teach Yourself Visual Basic 6 in 24 Hours by Perry and Hittihewa
Programming Microsoft Visual Basic 6 by Francesco Balena (Microsoft Press)
Written by: rae (June 8th 2005)
rae is a C/O of OPERATION COMMLINX
This article was originally published by CyberArmy.net in the CyberArmy Library.
|
|