Visual Basic/VB6 Command Reference

This page aims to be a comprehensive command reference to the MS Visual Basic 6 programming language.

Asc
Returns the number corresponding to the ASCII code for the first character of string

Usage

string = string containing character as first letter whose ASCII code is to be found.

Example

Here  will get value 65

Chr
Returns a string character that corresponds to the ASCII code in the range 0-255. Reverse of the  function.

Usage

code = ASCII code.

Example

Here  gets value "a".

Len
Returns the amount of characters in a given string or 0 for Empty.

Usage

expression = a string or Empty

Example

 mystring = InputBox("Enter a string to test") length = Len(mystring) MsgBox "Length of the string is " & length

e.g. where mystring is "Hello", length will be 5.

Left
Returns a given number of characters from the left hand side of a string

Usage

string = string to use x = number of characters

Example

 mystring = InputBox("Enter a string") mystring = Left(mystring, 4) MsgBox "First four characters of your input are " + mystring 

e.g. where the input mystring is "Hello", the output mystring will be "Hell"

Right
Returns a given number of characters from the right hand side of a string

Usage

string = string to use x = number of characters

Example

 mystring = InputBox("Enter a string") mystring = Right(mystring, 4) MsgBox "Last four characters of your input are " + mystring

e.g. where the input mystring is "Hello", the output mystring will be "ello"

Mid (Function)
Returns a given number of characters from the middle of a string

Usage

string = string to use start = character to start at (1 is the first character) length = number of characters

Example

 mystring = InputBox("Enter a string") mystring = Mid(mystring, 2, 3) MsgBox "The second, third, and fourth characters of your input are " & mystring

e.g. where the input mystring is "Hello", the output mystring will be "ell"

Mid (Statement)
Sets a given number of characters in the middle of a string equal to the same number of characters from the beginning of another string

Usage

mystring = the string to take characters from start = character to start at (1 is the first character) length = number of characters

Example

 mystring = InputBox("Enter a string") Mid(mystring, 2, 3) = "abcd" MsgBox "Your string with abc as the second, third, and fourth characters of your input are " + mystring

e.g. where the input mystring is "Hello", the output mystring will be "Habco"

Trim
Removes leading and trailing spaces from a string

Usage

string = string to use

Example

 mystring = Trim(mystring)

e.g. where the original value of mystring was " Hello  ", the new value of mystring will be "Hello".

LCase
Converts a string to lowercase

Usage

string = string to use

Example

 mystring = LCase(mystring)

e.g. where the original value of mystring was "HELLO", the new value of mystring will be "hello".

UCase
Converts a string to uppercase

Usage

string = string to use OR character

Example

 mystring = UCase(mystring)

e.g. where the original value of mystring was "Hello", the new value of mystring will be "HELLO".

String
Creates a string with the specified length of the specified character

Usage

length = length of string character = character to fill string with

Example

<tt> mystring = String(5,"a")</tt>

e.g. the new value of mystring will be "aaaaa".

Space
Creates a string with the specified length of space

Usage

length = length of string

Example

<tt> mystring = Space(5)</tt>

e.g. the new value of mystring will be "    ".

StrConv
Returns a converted string as specified.

Usage

string = string to use conversion = case to convert the string to (lowercase: vbLowerCase, uppercase: vbUpperCase, proper case (first letter in caps): vbProperCase) LCID = optional. The LocaleID, if different than the system LocaleID.

Example

<tt> mystring = StrConv(mystring, vbProperCase)</tt>

e.g. where the original value of mystring was "HELLO", the new value of mystring will be "Hello".

Abs
Returns the absolute value of a number

Usage

Abs(number)

Example

msgbox "The absolute value of -4 is " & abs(-4)

Important note!

The Abs function only accepts numbers. Non-numeric values generate an error.

Cos
Returns the cosine of a number

Usage

Cos(integer)

Example

msgbox "The cosine of 4 is " & cos(4)

Important note!

The input angle for all VB trigometric functions should be in radians. To convert degrees to radians, multiply degrees by pi / 180.

Sin
Returns the sine of a number

Usage

Sin(integer)

Example

msgbox "The sine of 4 is " & sin(4)

Important note!

The input angle for all VB trigometric functions should be in radians. To convert degrees to radians, multiply degrees by pi / 180.

Tan
Returns the tangent of an angle

Usage

Tan(integer)

Example

msgbox "The tangent of 4 is " & tan(4)

Important note!

The input angle for all VB trigometric functions should be in radians. To convert degrees to radians, multiply degrees by pi / 180.

Val
Returns the numeric value of a string

Uses "." as decimal separator. ( It does not depend on the Regional Settings )

Usage

Val(string)

Example

The return value will be 5

The return value will be 10.6

The return value will be 0 (zero)

The text box txtOutput will contain the product of the numbers in the txtNumber1 and txtNumber2 text boxes. If either of the text boxes contains a value that cannot be evaluated to a number then the output will be 0 (zero).

Rnd
Returns a floating point number less than 1 but greater than or equal to 0.

Usage

You can change the seed the random number generator uses by providing a value as a parameter e.g. Rnd(500) will use the number 500 as the seed in generating random numbers. <ul><li>If you provide a value less than zero then the same number will be returned by the Rnd function</li> <li>A value of zero will return the most recently generated random number.</li> <li>Providing no value will generate the next random number in the sequence. <li>Providing a positive number will generate the next random number in the sequence using number as a seed.</li></ul>

Example

myvar will become a random number between 0 and 10.

myvar will become a random number between 25 and 40.

myvar will become a random number between -25 and 25 and 12345 will be used as the seed to generate this number.

Int
Returns the integer portion of a number.

Usage

Int(n)

Where n is the number to return

Example

Returns 10

Returns 5

Returns 5

NOTE: No rounding will be performed, the decimal portion of the number will simply be removed. If you want to round a number to the nearest whole integer then use CInt. One interesting point to remember about CInt is that if the decimal portion of the number is exactly .5 then it will be rounded down if the number is odd and rounded up if it is even. For example CInt(1.5) and CInt(2.5) will both return 2.

MsgBox
The MsgBox function displays a message in a dialog box, waits for the user to click a button, and returns an Integer value indicating which button the user clicked.

Syntax

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The MsgBox function syntax has these parts:

Usage

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

or

Response=MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The integer value of the button selected will be placed into the Response variable.

Button Combinations

vbOKOnly

vbOKCancel

vbAbortRetryIgnore

vbYesNoCancel

vbYesNo

vbRetryCancel

Icon Display

vbCritical

vbQuestion

vbExclamation

vbInformation

Default Button

vbDefaultButton1

vbDefaultButton2

vbDefaultButton3

vbDefaultButton4

Examples

MsgBox "An error has occurred!",vbExclamation,"Error"

Note that since the message is fixed, quotation marks have been used.

Response=MsgBox("Yes or no?",vbYesNo + vbQuestion,"Choose one")

Note the parenthesis and how the vbYesNo and vbQuestion constants have been combined

MsgBox "An error has occurred on line " & lineno, vbExclamation, "Error"

InputBox
Displays a simple input box for user to enter data

Usage

Variable=InputBox(Prompt,Title)

Variable = variable to which the input value will be stored Prompt = text displayed to the user in the input box Title = title of the input box

Examples

myint=InputBox("Enter a number","Enter a number")

Date
Returns or sets the current system date

Example

MsgBox "Today 's date is " & Date & "."

Time
Returns or sets the current system time

Example

MsgBox "The time now is " & Time & "."

''Sidenote: be wary of changing date and time in your program. Many other programs will depend upon the system's date and time being set correctly, so altering it could have a knock on effect. Use date and time changes sparingly, and notify the user if this is being done.''

Timer
Returns the number of seconds since midnight

Example

NumHrs=Int(Timer / 3600) MsgBox "Currently in hour " & NumHrs

WeekdayName
Returns the name of a day of the week

Usage

WeekdayName(x,FirstDayOfWeek)

x = integer between 1 and 7 FirstDayOfWeek (OPTIONAL) = the day counted as the first in the week. The default is Sunday.

Example

DayName=WeekdayName(i,vbMonday)

IsNull
Returns true if an expression is null and false otherwise.

Example: If IsNull(vntMyVariantVar) Then Debug.Print "vntMyVariantVar is null" Else Debug.Print "vntMyVariantVar is NOT null" End If

Open
Open "" & namefile& ".dat" For Binary As #ff

Selection
'''If... Then... Else...''' Performs selection based upon criteria. A cornerstone of any VB program.

'Usage'

Single Line Selection

If condition Then action

Note that only one line can be used with this style of statement

Multi Line Selection

If condition Then

[code to execute if condition is true]

EndIf

Using Else

If condition Then

[code to execute if condition is true]

Else

[code to execute if condition is false]

EndIf

Condition is a condition which must be met for the code within the statement to be executed. It can make use of the modifiers AND, NOT, OR and XOR (e.g. x=4 AND y > 6). See also the Examples section.

Examples
Simple one-line selection: If x < 10 then MsgBox "The number is below 10"

Slightly more complex one-line selection, using the AND modifier to check two conditions at one time. If x > 10 AND x < 20 then MsgBox "The number is between 10 and 20"

''Simple multi-line selection. The code between the If... and EndIf statements will be executed only if the condition is met.'' If Date = 25/12/2006 Then MsgBox "The date is Christmas Day" Call ChristmasDay EndIf

''Using the Else statement. If the variable i is over 4, the program will exit the subroutine. Otherwise, it will go to the label SaveFile.'' If i > 4 Then Exit Sub Else Goto SaveFile EndIf

''Two If... Endif statements are used, one nested within the other. If i is 6 or 7, the code between the inside If...Endif statements is executed. Then if i=7 the first message box will be shown; otherwise (i.e. if i=6) the other will be displayed. For both cases, the final message box will be shown. When nesting selection statements, it's often a good idea to indent them as shown here.'' If i = 6 OR i = 7 Then If i = 7 Then MsgBox "Today is Sunday" Else MsgBox "Today is Saturday" EndIf MsgBox "Today is the Weekend" EndIf