VB Quick Reference

Programming Usable Interfaces (05-630/05-430)

Spring 2004


[Includes material from http://www.garybeene.com/vb/tutor.htm]




         Dim - Used to define a variable as a certain type. DIM is that simplest way to declare a variable.

Dim varName as VarType


Possible types include Integer, String, Double (floating point), Boolean


VB will let you leave off the VarType declaration, or let you use Object (the equivalent was called Variant in VB6) as the type (can store any type). Both of these options are bad ideas: then YOU have to keep track of the type of the variable and make sure you do the right thing. Uuse types so that VB can help you find your bugs.


In VB.Net, you can declare the variable and initialize it in one step:

Dim myInt as Integer = 42


VB 6 only

Option Explicit - Instructs VB to force an explicit declaration of all variables


Make sure you use this, so VB can catch your typos!

Use Tools->Options->Require Variable Declaration to turn on Option Explicit for all your projects.


         Const - Creates a variable whose value is fixed

Const kgToPound = 2.205

         Call - Transfers control to a Sub or Function (is optional)

Call Procedure 1

Since the use of CALL is optional, you can ignore this.


         Sub - Defines a method which can execute a block of code

Sub methodName (var1 as VarType, var2 as VarType, var3 as VarType)

Be sure to check out HELP for how to handle Sub arguments.


         Function - Declares a procedure which can return a value

Function functionName (var1 as VarType, var2 as VarType) as VarType


This is the most versatile of the Sub/Function procedure types. It can do anything a Sub can do as well as returning a value for use in an expression.


To return a value:

VB 6 functionName = value

VB.Net return value


VB 6 only

         Array A function that returns an array that has been filled with data from a list. It allows you to put the actual data values in the code to avoid having the user input it or to avoid having to read it from a file


ArrayName = Array (10, 20, 30)



/ - Normal division

\ - Integer division (truncates the answer)

^ - Exponentiation operator

* - Multiply

+ - Plus

- - Minus

= - Equal

> - Greater Than

< - Less Than

<> - Not Equal

>= - Greater than or equal

<= - Less than or equal

AND - Defines a boolean value that is the AND of two values

result = expression1 AND expression2

OR - Defines a boolean value that is the OR of two values

result = expression1 OR expression2

IS - Determines if 2 variables reference the same object

result = object1 IS object2

LIKE - Determines if one string matches a pattern

result = string LIKE pattern

More about operators (MSDN).


Math.Round - Rounds a number to a selectable number of decimal places

result = Math.Round ( tempvariable,2 )

         Called Round in VB6
result = Math.Round ( tempvariable,2 )


Val - Returns the numerical content of a string

result = Val ("123.4")

Int - Returns an integer by truncating

i = int ( tempvariable )

Math.Sqrt - Returns the square root of a number

tempvariable1 = Math.Sqrt ( tempvariable2 )

         Called Sqr in VB6
tempvariable1 = Math.Sqrt ( tempvariable2 )

Math summary (MSDN).



Type Conversions

CBool, CByte, CCur, CDate, CDbl, CDec, CInt, CLng, CSng, CStr, Cvar

See documentation (MSDN).


VB 6:

Str Returns a string representation of a number.

VB.Net value.ToString() returns the string representation of value


Left - Returns the left n characters of a string

temp = Left ( teststring, 4 )

Right - Returns the right n characters of a string

temp = Right ( teststring, 4 )

Trim - Removes leading and trailing spaces of a string

temp = Trim ( teststring )

LTrim - Removes only the leading spaces of a string

temp = LTrim ( teststring )

RTrim - Removes only the trailing spaces of a string

temp = Rtrim ( teststring )

UCase - Makes all characters upper case

temp = Ucase ( teststring )

LCase - Makes all characters lower case

temp = Lcase ( teststring )

Mid - Returns n characters from a string, starting a any position

temp = Mid ( teststring, 1, 4 )

Len - Returns the length of a string (how many characters it has)

temp = Len ( teststring )

VB.Net: Can also use temp = teststring.Length


LSet - Positions a string inside another, flush to the left

temp = lset ( teststring )

RSet - Positions a string inside another, flush to the right

temp = rset ( teststring )

Format - Returns a string formatted according to a user-defined format

temp = format ( teststring, "####.0" )

String -

temp = left ( teststring, 4 )

Chr - Returns the string representation of a number

temp = str ( 32 )

Asc - Returns the ASCII code of a single character

temp = asc ( "A" )

Space - Returns n spaces

temp = space ( 15 )

Instr - Determines if one string is found within a second string

i = Instr (starthere, string1, string2)

InStrRev - Determine if one string is found in a second, starting at the end

i = InStrRev (string1, string2, start)

StrComp - Compares two strings

result = StrComp (string1, string2)

StrConv - Converts the case of a string's characters

StrConv (string, vbuppercase)

StrReverse - Reverses character order in a string

StrReverse (string1)

Replace - Replaces each occurrence of a string

Replace (bigstring, searchstring, replacementstring)

String function examples (MSDN)


Boolean Expressions (MSDN)




MsgBox - A built-in dialog box that gives a message and allows a user input

x = msgbox "Read this!", vbokonly, "Test Message"

The result of which button the user clicked is saved in x.

InputBox - A built-in dialog box that allows entry of a text string

inputbox "Input a value!", 5

VB 6:

Load - Load an object

load form1

Unload - Unload an object

Unload form1

RGB - Returns a color value by inputting the red, green, and blue parts

form1.backcolor = RGB (12,128,256)

Me - Refers to the current object, usually the active form

VB 6 print Me.caption

VB.Net Debug.WriteLine(Me.Text)


Loops and Conditional Decisions

If..Then..Else - Performs code based on the results of a test

If A>5 Then Print "A is a bit number!"

For...Next - Loops a specified number of times

For i = 1 to 5: print #1, i: next i

For Each ... Next - Walks through a collection

For Each X in Form1.controls: Next X

While...Wend - Loops until an event is false

while i < 5: i = i +1: wend

Select Case - Takes an action based on a value of a parameter

select case i

case 1 : print "it was a 1"

case 2 : print "it was a 2"

end select

Do...Loop - Loops until conditions are met

do while i < 5 : i = i + 1 : loop

Choose - Selects and returns a value from a list of arguments

Choose (index, "answer1", "answer2", "answer3")

With - Executes a series of statements on a single object

With textbox1

.Height = 100

.Width = 500

End With

End - Immediately stops execution of a program


Stop - Pauses execution of a program (can restart without loss of data)



VB6 Tips


Use Debug.Print to print out data values. Results are shown in the Immediate window while the program is running

Debug.Print varName1, varName2, , varNameN


Make VB save your program when you run it

Tools->Options->Environment tab

In the "When a program starts", chose either "Save Changes" or "Prompt to Save Changes"



VB.Net tips

Use Debug.WriteLine to print out data values. Results are shown in the Output window while the program is running. You must pass WriteLine a string, so compose the string by concatenating other strings using the + operator.

Example: Debug.WriteLine("Value of x: " + x.ToString)