- Before declaring or using a variable, first decide what kind of role that variable will play in your program.
- The kind of variable you want to use is referred to as a data type.
- To specify the kind of variable you want to use, you type the As keyword on the right side of the variable's name.
- The formula to declare such a variable is
Dim VariableName As DataType
String
- A string is an empty text, a letter, a word or a group of words considered.
- To declare a string variable, use the String data type. Here is an example:
Private Sub Form_Load()
Dim CountryName As String
End SubPrivate Sub Form_Load()
Dim CountryName As String
CountryName = "Great Britain“
End Sub Boolean
- A Boolean variable is one whose value can be only either True or False.
- To declare such a variable, use the Boolean keyword. Example:
Private Sub Form_Load() Dim IsMarried As BooleanEnd Sub
- Boolean variable can be initializes by assigning it either True or False. Example
Private Sub Form_Load() Dim IsMarried As Boolean
IsMarried = False
End Sub Like any other variable, after initializing the variable, it keeps its value until you change its value again.Numeric data typesByte
- A byte is a small natural positive number that ranges from 0 to 255. A variable of byte type can be used to hold small values such as a person's age, the number of fingers on an animal, etc.
- To declare a variable for a small number, use the Byte keyword. Here is an example:
Private Sub Form_Load() Dim StudentAge As ByteEnd Sub Integer
- An integer is a natural number larger than the Byte. It can hold a value between -32,768 and 32,767. Examples of such ranges are: the number of pages of a book.
Long Integer
- A long integer is a natural number whose value is between –2,147,483,648 and 2,147,483,642.
- Examples are the population of a city, the distance between places of different countries, the number of words of a book.
- To declare a variable that can hold a very large natural number, use the Long keyword.
Decimal data typesSingle
- To declare a variable that can hold small decimal numbers with no concern for precision, use the Single data type.
DoubleTo declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword.In most circumstances, it is preferable to use Double instead of Single when declaring a variable that would hold a decimal number. Although the Double takes more memory spaces (computer memory is not expensive anymore(!)), it provides more precision.Date
- To declare a variable that can hold either date values, time values, or both, use the Date keyword. After the variable has been declared, you will configure it to the appropriate value. Here are two examples:
Dim DateOfBirth As Date Dim KickOffTime As DateVariant
- A Variant can be used to declare any kind of variable. You can use a variant when you can't make up your mind regarding a variable but, as a beginning programmer, you should avoid it.
FUNCTIONSThe InputBox( ) Function:
- An InputBox( ) function will display a message box where the user can enter a value or a message in the form of text. The format is
- myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)
- myMessage is a variant data type but typically it is declared as string, which accept the message input by the users.
- The arguments are explained as follows:
- Prompt - The message displayed normally as a question asked.
- Title - The title of the Input Box.
- default-text - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to key in.
- x-position and y-position - the position or the coordinate of the input box.
Private Sub OK_Click() Dim userMsg As String userMsg = InputBox("What is your message?", "Message Entry Form", "Enter your messge here", 500, 700) If userMsg <> "" Then message.Caption = userMsg Else message.Caption = "No Message" End If End Sub
- When a user click the OK button, the input box as shown in Figure will appear. After user entering the message and click OK, the message will be displayed on the caption, if he click Cancel, "No message" will be displayed.
|