Visual Basic Cheat Sheet

Link to Phillip's Home Page

Category Item Type
Displaying and entering information Messagebox Function/Statement
Input Box Function/Statement
Conditional test
Select Case Statement
For Next LoopStatement
If then else/elseif Statement
Object handling
Load  Method
Unload Method
Show Method
Hide Method
Menus  Property
Top, Left, Width, Height Property
Visible  Property
Program control
End Statement
Shell Function/Statement
Type Statement


Displays a message box on the screen with a choice of buttons.

Dim Response as integer
Response = MsgBox (“Prompt”,Buttons,”Title”)

Notes:  Buttons = VBOk, VbYesNo, Vbquestion
Eg to get yes no button with exclamation icon: Buttons = VbYesNo + Vbexclamation


Displays a prompt in a dialog box, waits for the user to input text or choose a button, and returns the contents of the text box.

Dim response as string
Response = InputBox (“prompt”, “title”, “default”)

Select Case

Evaluates an expression and executes the options specified

Select Case Response 
Case VbYes
MsgBox “You pressed the Yes Button”
Case VbNo
MsgBox “You pressed the No button”
End Select

For Next Loop

Repeats a group of statements a number of times

Dim counter as integer
For counter = 1 to 10 step 2

Msgbox “The counter number is “ + Str$( counter)

next counter

Notes:  Str$() function converts numbers to strings (or text)

If Then Else/ElseIf

Conditionally executes a group of statements based on the evaluation of an expression.

If YourName = “Peter” then
Msgbox “Your name is Peter”

Msgbox “Your name is not Peter”
End if


If YourName = “Peter” then
Msgbox “Your name is Peter”
Elseif YourName = “Fred”

Msgbox “Your name is Fred”

Msgbox “Your name is neither Peter or Fred”
End if


This keyword terminates a running program.



This keyword creates an object at run time

Load Object


Arrays are objects that share the same name but have unique index numbers to specifically identify each element.

Object(index range)

Dim BoyNames (1 to 10) as string

BoyNames(1) = “Kym”

BoyNames(2) = “Shane”

BoyNames(3) = “Scott”


Menus have the following properties:

Caption:            eg   &File

Name:              eg    MenuFile

Note: the ampersand prior to a letter indicates the shortcut key for that menu item.  In the case above, pressing the Alt key and F together will pop up the file menu.

A single Hyphen “-“ creates a menu separator bar.

Indenting the menu items will create sub menus.


Showing, Hiding, Loading and Unloading

These methods are used to show, hide, load and unload forms.  -  This code displays the form named Frmabout.  If the form has not been loaded into memory, it is first loaded then made visible.

Frmabout.hide  -  This code makes the form named Frmabout invisible but does not unload it from memory.

Load Frmabout  -This code loads the form named Frmabout into memory but doesn’t make it visible

Unload FrmAbout  - This code unloads the form from memory thus making it invisible.

Top, Left Width & Height

These properties of an object identify its physical location on the form (Top, Left) and Physical size (Width & Height)

The measurement units are normally TWIPs  (1440 TWIPS to the inch)

These properties can be read or set in code.

To set the left and top properties of a object called CommandButton:

CommandButton.left = 100 =100

To find out the top property of an object called CommandButton:what the

Dim TopPosition as integer

TopPosition =


This object property determines whether the object is visible or not.

To make the object visible:

CommandButton.visible = true

To make the object invisible and therefore unusable

CommandButton.visible = false


This Function allows the running of any executable program and passing of any parameters to that application.

To start Notepad and open a file called fred.txt:

Dim dummy as integer

Dummy = Shell (“C:\windows\notepad.exe A:\fred.txt” ,1)

Note: the first part of the string is the Pathname to the program.  The second part is the pathname to the file that you want to open   The ,1 forces the application to take the focus.  Thus it will become the active window.

Custom Variable Types

Declaration required at Module Level :

Public Type MyType

    MyName As String   ' String variable stores a name.

    MyBirthDate As Date             ' Date variable stores a birthdate.

    MySex As Integer                ' Integer variable stores sex (0 for female, 1 for male).

End Type

Declaration required at code level:

Dim Employee(1 To 100) As MyType  ‘Creates an object array with properties the same as MyType

Employee(1).MyName = "Peter"

Employee(1).MyBirthDate = "25/2/1995"

Select Case Index

    Case 1

    MsgBox Employee(1).MyName

    Case 2

    MsgBox Employee(1).MyBirthDate

End Select