# Introduction

## How to use this book

The first page of the book is an annotated contents list, click the headings with the mouse to go to the page you want to read.

• You will often see Visual Basic abbreviated to VB, and for the purpose of conciseness, this guide will use VB from this point onwards.
• VB code is written in US English. There are some key differences you should note, as misspelling code can cause hours of problems. Below is a list of the differences- when writing in VB, you should use the right-hand column's spellings:

Many of these words you will not need until you progress into more complex VB

Commonwealth English US English (VB)
Analyse Analyze
Centimetres Centimeters
Centre Center
Colour Color
Co-ordinates Coordinates
Grey(ed) Gray(ed)
Millimetres Millimeters

Words which can be spelled with an 'ise' and 'ize' suffix should be spelled with 'ize' in VB.

This book will guide you step-by-step through Visual Basic. Some chapters of this book contain exercises. The exercises are an integral part of the text and at least some should be tried or you will not get the full benefit of the book. There are no answers to the exercises because most do not have a single correct answer. Physical exercises are done to build muscle and stamina, intellectual exercises are the same thing for the brain. In almost every case you should be able to think of at least two 'solutions'. Some exercises require you to write code, please write it and execute it, others expect you to think about or describe something, please write it down even if only on a piece of scrap paper, it helps fix it in your mind.

If you try an exercise and fail to find a solution please ask for help by adding a comment to the relevant discussion page. It is of course entirely possible that the exercise is faulty so reports of problems will help the authors too.

## Terminology

Please refer to the Glossary for definitions of any unfamiliar words or any words that appear to be used in an unusual sense.

## VB Integrated Development Environment

Visual Basic has several key components you will need to get acquainted with. The first is the tool box, usually located on the left of the screen. This is used for creating objects on your form. A form is the basic window a user will see upon running your program. What is on the form is what the user sees. Make some picture boxes and command buttons on your screen by clicking the button, then clicking and dragging where you want the object to be placed. The next key feature is the properties of objects. Captions, names, and other properties of objects may be edited here. Now, click an object, and a white window will appear. This is where your code goes. Toward the top of your screen you should see a "play" button, but in Visual Basic it is referred to as the "Run" button. This will run any programs you make. Next to it are the pause, and stop buttons, which are pretty self explanatory.

# History

Visual Basic is Microsoft's high-level object-oriented rapid application development environment for the Windows platform. The first versions of Visual Basic were intended to target Windows 3.0 (a version for DOS existed as well), however it was not until version 3.0 for Windows 3.1 that this programming language gained large-scale acceptance in the shareware and corporate programming community.

Using drawing tools that resemble those found in hardcopy page layout programs or PhotoShop, VB programmers make user interfaces by drawing controls and other UI components onto forms. The programmer then adds code to respond to user interactions with the controls (for example, clicks, drag and drop, etc) known as events. The code can trigger events in other controls (for example, by displaying text or an image), execute procedures (run some algorithm based on the values entered in some control, output data, do business logic, etc), or almost anything else one might do in code.

Visual Basic can be considered to be an interpreted language like its Basic ancestor, with appropriate modifications to accommodate object-oriented programming, and has implicit type conversion. That is, the VB development environment goes to great lengths to format (and aid the user in formatting) programming code so that it conforms to executable syntax. For example, VB will appropriately change the case of newly typed variable names to match those that have been declared previously (if they have been declared at all!). Traditionally, VB is known for compiling programs into pseudo-code (p-code, similar to Java's byte code) which is interpreted at runtime, requiring the use of dynamically-linked libraries (for example, VBRUN300.DLL for version 3 of Visual Basic, circa 1992) but newer versions can compile code into something more closely resembling the efficient machine code generated by C-like compilers. VB6 can be compile either into p-code or into native code; in fact VB6 uses the Microsoft C++ compiler to generate the executable.

For new Windows programmers, VB offers the advantage of being able to access much of the Windows UI functionality without knowing much about how it works by hiding the technical details. Although accessing low-level Windows UI functionality is possible, doing so in VB is as, or more difficult compared to such access using Visual C++ or other lower level programming languages. Recently VB.NET has gone a long way to fixing some of the limitations.

Using custom controls provided by Microsoft or third parties, almost any functionality that is possible in Windows can be added to a VB program by drawing a custom control onto a form in the project.

Visual Basic traditionally comes in at least entry level and professional versions, with various designations depending on Microsoft's contemporary marketing strategy. The different versions are generally differentiated by the number of custom controls included, and the capabilities of the compiler. Higher priced packages include more functionality.

## Evolution of Visual Basic

VB 1.0 was introduced in 1991. The approach for connecting the programming language to the graphical user interface is derived from a system called Tripod (sometimes also known as Ruby), originally developed by Alan Cooper, which was further developed by Cooper and his associates under contract to Microsoft.

## Timeline of Visual Basic

• Visual Basic 1.0 (May 1991) was released for Windows.
• Visual Basic 1.0 for DOS was released in September 1992. The language itself was not quite compatible with Visual Basic for Windows, as it was actually the next version of Microsoft's DOS-based BASIC compilers, Microsoft QuickBASIC compiler QuickBASIC and BASIC Professional Development System. The interface was barely graphical, using extended ASCII characters to simulate the appearance of a GUI.
• Visual Basic 2.0 was released in November 1992. The programming environment was easier to use, and its speed was improved.
• Visual Basic 3.0 was released in the summer of 1993 and came in Standard and Professional versions. VB3 included a database engine that could read and write Access databases.
• Visual Basic 4.0 (August 1995) was the first version that could create 32-bit as well as 16-bit Windows programs. It also introduced the ability to write classes in Visual Basic.
• With version 5.0 (February 1997), Microsoft released Visual Basic exclusively for 32-bit versions of Windows. Programmers who preferred to write 16-bit programs were able to import programs written in Visual Basic 4.0 to Visual Basic 5.0, and Visual Basic 5.0 programs can easily be converted with Visual Basic 4.0. Visual Basic 5.0 also introduced the ability to create custom user controls, as well as the ability to compile to native Windows executable code, speeding up runtime code execution.
• Visual Basic 6.0 (Mid 1998) improved in a number of areas, including the ability to create web-based applications using Internet Explorer. Visual Basic 6 is no longer supported.

# Getting Started

## Hello World

The very simplest program in VB is this:

  Public Sub Main()
MsgBox("Hello World!")
End Sub


To create this do as follows:

• Start VB
• A New Project window will appear. Select Standard.exe and click on Open.
• Copy or type the Main subroutine shown above into the new code module.
• Go to VB's Project menu again and click the Project1 Properties item (it should be the last item in the menu).
• In the Project Properties dialog box you should find a dropdown list labeled Startup Object, click there and choose Sub Main
• Go to the Run menu and click Start (or press F5) to run the program. VB might ask if you want to save, just say no this time.

The running program will show the words Hello World! in a small message window.

## The Visual Basic Integrated Development Environment

The Visual Basic Integrated Development Environment (IDE) is a program from Microsoft used to create, test, and deploy Visual Basic (VB) programs. VB programs can be written using other text editors, which must then be converted to executable code (compiled and linked) by the programmer. The VB IDE allows a programmer to write code and test it immediately, converting the source program to executable code as needed (on the fly). As the name implies, creation, editing, compiling, and linking are all integrated in this development environment.

The top of the IDE window shows VB's menu bar (see screenshot). Common menu categories (File, Edit, Help, etc.) as well as items specific to creating code (Project, Debug, Run) provide the text interface for the IDE. Commonly used menu items are provided as toolbar buttons directly below the menu bar.

A screen shot of Microsoft Visual Basic 6 Integrated Development Environment

Two parts of creating a program; designing the user interface using forms and controls, and writing the source code; take up the majority of space in the IDE. As initially configured, four dockable windows also appear in the IDE's application space; the Project Explorer, Properties Window, Form Layout Window, and Toolbox. These and other supplied windows may be floated above the IDE, or docked to the sides, top, or bottom of the IDE application space as may be convenient for the programmer. Clicking on the title bar of a dockable window and dragging it around will dock, or undock, the window at will.

### Availability

As of April 8, 2008, Microsoft no longer provides support to the Visual Basic 6.0 IDE.[1] Nonetheless, Visual Basic 6 is still available via MSDN subscription, under the name of "Visual Studio 6".[2]

### Visual Editing

VB programs are divided into two views, the visual design of the user interface, and the code editors. The visual interface is created by adding controls from the Toolbox to the Form Layout Window. To add a control to a form; click on the desired control in the Toolbox and then click and drag its bounding rectangle on the representation of the Form in the Form Layout Window. When you release the mouse button after the click and drag operation, the selected control will appear on the form.

Clicking on a control, or the form, in the Form Layout Window highlights the control (or form) with sizing handles. You can use the sizing handles to change the Left, Top, Height, and Width properties of a control or the Height and Width properties of a form. A highlighted control can be dragged to other positions on the form simply by clicking on the control and dragging it into place. A highlighted control can also be copy and pasted to reproduce additional identical controls on the form. Clicking and dragging around multiple controls (a lasso operation) will highlight them all as a group, which can then be moved as a group, or copy and pasted as a group, as may be needed.

### Code Editing

Code editing is done using the code module windows. The title text of a code module window shows the project and module name. Two dropdown lists show the objects accessible to that module (General) and the subroutines (Declarations) associated with the selected object in the General list. In a form's code module, the General list will list all of the controls that have been placed on the form. Each control has its own set of events and methods, which will populate the Declarations list upon selection of any control. These two windows combined help the programmer locate specific code for events and methods in the program. In small programs, the editors can be scrolled up and down to find a desired routine to work on. In large programs finding the right spot can be much more difficult, where these two lists bring the programmer to the desired location using a few clicks of the mouse.

The code editors will show the program as one long text document, or it can show the program a single routine at a time. The two buttons in the lower left of the code module window allow the programmer to set his/her view preference.

The code editors can be split into two views by moving the small bar in the upper right of the editing area. The two views show two different views of the same code module. Again, in larger programs, it may be helpful to see one section of the code in the module while working on another area of the code in the module. The split view mode allows the programmer to keep one section of the code in view while creating a new method or function.

### VB Project Files

Code modules and forms are saved individually as text documents in the project folder. The filename extension (.xxx) is used to identify which type of module the file contains. The project file (.vbp) describes the project as a whole, naming the individual files needed to create the program. With VB installed correctly, double clicking on a project file will bring the entire project into a new instance of the VB IDE, while double clicking on a form (.frm) or module (.bas) or class (.cls) or control (.ctl) file will load that individual file into a new instance of the IDE. Typically, loading a single file from a multi file project (other than the project file) will not have all the information required to allow the program to run. However, in many cases, short test code, code written to test out some small idea, is best kept in its own module (and file) where it can be loaded and discarded at will. Such code can be added, removed, or edited, until it is perfected (debugged) and copied into the real program modules.

### Immediate Window

While testing a program for correctness (usually called debugging) it is often advantageous to see interim results in a calculation, or to check on values being passed to a subroutine. Setting breakpoints to stop the program at specific lines can help, as can sending printed values to a second window. VB provides a dockable window for text called the Immediate Window. It is available under the View menu or you can press CTRL+G to bring it into view. It is called the Immediate Window because it can be used to execute BASIC commands immediately. For example:

Print Atn(1) * 4


When you type that line into the Immediate Window and press Enter, VB interprets that line as a VB command and executes the command. In this case it prints the number 3.14159265358979 on the line following the command. (For expediency, the question mark can be substituted for the Print command in most circumstances.)

You can send text to that same window from a running program using the Print method of the Debug object:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Debug.Print Chr(KeyAscii), KeyAscii
End Sub


In this case, with a Textbox called Text1 on some form, every letter a user enters into the textbox will be echoed to the Immediate Window, as well as its numerical ASCII value.

Because text is sent to the Immediate Window using the Debug object, many people refer to the Immediate Window as the Debug Window. You may find it listed either way in this literature.

IDE NOTE:

It should be noted that running a program using the VB IDE is not identical to running a stand alone executable program. The program running inside the IDE is running as a sub-process of the IDE. There are a few small connection and timing issues associated with running as a sub-process. On the whole, however, a program run in the IDE will look and feel like the finished program, but to fully test a program as the user will see it, it is advisable to create the program executable (.exe) and test that for correctness and usability.

## Parts of a Visual Basic Program

### Forms

A form is a window where controls are placed for use by the user of the program. The .Caption property changes the text on the title bar of the window, and the .MinButton and .MaxButton properties show or hide the minimize and maximize buttons. Different window styles such as Dialog boxes, ToolWindows, and standard Forms; as well as some allowed user actions such as resizing and minimizing; are controled by the form's .BorderStyle property. It is a common practice to name a form with frm<FormName> (ex: frmMain, or frmAlert).

### Components

A component is an executable module stored either as a .VBX file (Visual Basic eXtension for the 16-bit versions of VB), .OCX (OLE Control eXtension for 32-bit versions) file or as a .DLL (Dynamic Link Library) file. Components are pre-compiled code modules used by other program writers with/without knowledge and understanding of the details of its inner workings. Pre-compiled components provide reuseable code that has already been written and debugged. Components can be code only (.DLL) or have a visual component that can be placed on a form (.VBX and .OCX). VB supplies many commonly used components (Button, Textbox, Listbox, etc.) as controls in the Toolbox.

### Events

An event is an activity that occurs during a program's execution usually in response to the user's actions, such as a mouse click or a keypress. An event causes a procedure to execute. You the programmer can decide what code (if any) you want to place within the different event procedures.

## Controls

### Control Properties

To display the Control Properties window, select View\Properties Window or press the F4 key. The Properties window initially appears on the right edge of the main window and contains all the names of the editable properties as well as their current values. Some control properties are only editable while the program is running, some are only editable while in design mode.

### Buttons

A button will be your best friend in Visual Basic. Each button should contain code, which is added by you, the programmer. Upon clicking the button, the user will be instructing the program to execute that portion of code. For example, you could set it so when pressed, the program will make a message box that says "HELLO!". Good programming styles generally use cmd<ButtonName> when naming a button.

### Text boxes

Text boxes allows the users to add text areas to their programs. This text does not have to be typed in directly by the programmer, but could come from other sources such as database fields, text files or data the user will type in while the program is running. Although the default value for this is the name of the control, it can be set to anything including "" (or nothing). Text box names are prefixed with txt, eg; txt<BoxName>.

### Labels

Labels are one of the most used Visual Basic objects. They are often used to label other controls (textboxes, images, etc.) or provide feedback to the user. They are usually named like lbl<LabelName>.

### Timers

Timers are interesting and easy to learn. If you want the program to perform a certain task after a certain amount of time, the Timer is there to help you out. Their only event procedure is _timer, which will be executed every time after a certain amount of time is passed. The most common steps to use Timers is as simple as follows:
1. Add a timer to the form and give it a name.
2. Set the time interval in the Properties window to some value above 0.
3. Double click the timer and add the code you want executed at the set intervals.
Timers have very few properties too.
This is a possible use of timer: (To see it in action add a Command button, Shape control, and Timer to a new form. Then set the Timer.Interval property using the Properties window. Copy the code below into the form's code module and press F5 to run the program.)

  Private Sub Command1_Click()
Timer1.Enabled = Not Timer1.Enabled
End Sub

Private Sub Timer1_Timer()
Shape1.Visible = Not Shape1.Visible
End Sub


This would make the command button stop and start the flashing (repeatedly disappear and reappear) of the Shape control. The flash interval is determined by the Timer's Interval property. Timer.Interval is measured in milliseconds.

Here is another timer example of a counter:

  '***************************************************************** *
'APRON TUTORIAL PRESENTED BY MORROWLAND *
'***************************************************************** *
'Project Name : Timer * * Project Description : Using Timer and
'Counter * * Project Type : Visual Basic * * Author : Ronny André
'Reierstad * * Web Page : www.morrowland.com * * E-Mail :
'apron@morrowland.com * * Version : English (UK) * * Date :
'27.06.2002 *
'*****************************************************************
'Timers are the backbone in any good application, you will be able
'to decide when things will happen in millisecounds by using timers
'and counters you gain control

'declare counter as integer
Dim counter As Integer

Timer1.Enabled = False 'disable timer at startup
End Sub

Private Sub Command1_Click()
Timer1.Enabled = True   'starts the timer by enabling it
End Sub

Private Sub Command2_Click()
Timer1.Enabled = False  'stops the timer by disabling it
End Sub

Private Sub Command3_Click()
counter = 0             'reset the counter
End Sub

'The timer procedure
'the timer procedure will loop in the interval of the timer
'I have set the timer interval in the "properties" menu to 1000 ms (1 sec)
Private Sub Timer1_Timer()

counter = counter + 1   'we set the counter to count here

Text1.Text = counter    'write the counter value out as text

End Sub


VB Timers are given low priority on the processing totempole. If any other code needs to execute when the timed interval has elapsed, that other code is allowed to execute before the timer's procedure is called. Other code can yield some of their processing time to Windows by using the DoEvents command. That is the only VB command that will allow code you write to yield time to any pending timer events in the program. Without the use of DoEvents, each subroutine or function is executed start to finish, in serial fashion. In other words, only one command or statement can be executed at a time. Each subroutine or function must list the proper commands to execute, in their proper order.

### Picture boxes

Although called picture boxes, these objects are not just a heavyweight version of image boxes: picture boxes almost have the same properties and function as Form objects. It can do far more than just displaying pictures. Probably the best way to describe picture boxes is that they are containers that can group other objects together, kind of similar to frame objects. E.g. several command buttons can be drawn "inside" of it.

### General properties

VB extends some common properties to all controls placed on a form. Name, Top, Left, and Tag, are a few of the extended property names. When you lasso and highlight several controls on a form, you can change the general property values of all the highlighted controls using the Properties window. Making a change there will change the property of the same name for all of the highlighted controls.

### References

Reference is a kind of link that is used by Visual Basic. The word reference is used in two distinct ways:

Component reference
Are typically external libraries such as DLLs, OCXs, or type library. Component references are added to the project, so that all parts of the program may interact with the outside component.
Object reference
Is typically a pointer to an internal object. Object references differentiate, or identify, specific objects within the program. The term object is a very general term used to indicate just about anything that has properties to set, or methods to call. For example, if your form has 5 textboxes on it and you want to put text in just one of them, you need to use the proper 'object reference' to address that specific textbox.

These two uses are quite distinct and generally do not cause any problems. Usually when object reference is meant it will be written out in full whereas just references usually means references to an external library. In both cases, accessing the properties (or methods) of a component or object reference requires a special syntax that includes a period to separate the object and property. The syntax is: <object>.<property> or, by example: Form1.Caption = "This", or Text1.Text = "That", etc. where Form1 or Text1 are the specific objects you want to alter, and Caption or Text are the specfic properties being changed.

You can create components in VB to be used by other programs (not just those written in VB).

## Reserved Words

Visual Basic contains several reserved words. These words are "reserved" because they are specific functions and commands in Visual Basic. For example, a variable may not be named "Print" because it is a feature in VB to print. This can be avoided however, by naming your variables "prnt" or "print1". As long as it is not the exact word, it should work. A list of frequently used reserved words/keywords:

 And
As
Beep
Call
Close
Command
Date
Do
End
Error
Event
Exit
False
For
Function
Get
GoTo
If
Input
Kill
Let
Loop
Me
Name
Next
Not
Nothing
On
Option
Or
Print
Private
Public
Put
Reset
Resume
Set
Step
Stop
Sub
Then
Time
True
Until
While
With


## REMs

While programming you may find it necessary to leave yourself notes. This can be used to easily identify areas of code, or as a reminder of sections of code with logic errors that need to be fixed. REMs are very simple. Merely place an apostrophe, " ' ", or the word, "REM", before the line and that line will be ignored by the interpreter, whether it be a message to yourself or a section of code. For example:

 ' I leave notes to myself
REM or I could do it like this
'If I REM a line of code it will not execute!
REM x=5


An apostrophe is most often used due to the comparative reduction in space and time as REM has been included so as to be backward compatible with earlier versions of BASIC. The REM statement is a basic command and must be preceded by a colon if following other commands on the same line. An apostrophe needs no colon:

 x = 5 'This comment will work
y = 3: REM and this comment will work.
z = 1 REM This line would cause a syntax error.


## Error Checking

These are several common types of errors that one might get from a VB program:

run-time errors
This type of error are errors that are raised when the program is running. Examples: dividing anything by zero or assigning a string to a numeric variable.
compile errors
Compile errors are errors that the IDE spots at design-time and upon compiling the program just before it runs the program. These errors include syntax errors -- the error raised when the computer does not understand your code, and also errors like undeclared variables etc.
logic errors
Logic errors are errors that the computer cannot spot. These are errors that, even though the program runs, the result is not what you intended.

The first two of these errors are generally easy to spot, and the debugging tool can be used to high-light the line of text at which the error occurred. For example, if you wanted to make a program to convert Celsius to Fahrenheit, and in the code you used a multiplication symbol instead of division, the program would run fine, but the program would not convert the temperatures correctly, as you desired. Although sometimes these logic errors can be easy to spot, some are quite difficult. Logic errors become concealed in the lines of code when making complex programs, such as a game. Just remember, the computer does not know what the program is supposed to do, it only knows the code. Look through the code step-by-step and think of how the computer would interpret the code.

VB won't let you just ignore syntax/compile errors. You have to fix them before you can run your program. But run-time errors, which are syntactically correct, but may cause an error if an attempt is made to execute it, can be handled to possibly prevent your program from crashing. The following example shows a very good way of handling a possible error:

  Private Sub Form_Load()
On Error GoTo ErrorHappened
i = 1 / 0 'This line will cause an error to be raised as anything divided by zero = infinity
'...
'Some code
Exit Sub 'Here the subroutine is exited if no errors occur
ErrorHappened:
'Informs the user an error happened.
MsgBox "Error Number " & Err.Number & ":" & Err.Description
End Sub


The output of this example is a message box that says "Error Number 11: Division by zero". The statement On Error Goto ErrorHappened will skip everything from i = 1 / 0 to Exit Sub if any run-time error occurs within this procedure. When an error is detected the program will continue to run from right after ErrorHappened:, where the error will be displayed so that the programmer can find out what it is and fix it. This also prevents the program from "crashing".

ErrorHappened is just a line label, you can name it anything you wish to identify that section of the procedure you want to execute when an error happens. On Error Goto can only reference line labels that are within the current procedure. You cannot (easily) define one error handler for all procedures. Exit Sub means to end the Form_Load event immediately.

So if no error occurs, a message box will NOT be called because Exit Sub will already have ended or exited our subroutine. And if an error does occur, the message box will pop up, displaying the Error Number and the Error Description.

The above example is the safest way of detecting and handling any error that takes place in the subroutine. However you can also choose to ignore errors by using "On Error Resume Next" which means to ignore all errors. Ignoring errors is not a good idea in most scenarios, but in some cases the proper way to handle an error is to do nothing, so they might as well be ignored. Even using Resume Next, you can test the Err object (Err.Number) to see if an error happened on preceding lines.

See Errors for more detail about error handling.

## Declaring Variables (Dimensioning)

If you don't already know, a variable is, by dictionary definition: a symbol (like x or y) that is used in mathematical or logical expressions to represent a variable quantity. In mathematics, common variables are: x, y, z, etc., and they can "hold" values like x=1, y=3, etc. In VB, instead of x, y and z, a variable can have whatever name you want. It is often good practice, and sometimes necessary, to dimension variables. Often it is called 'dimming'. This process gives the variable its name and the type of value it will be able to hold (which will be discussed later). To dimension a variable, the code is:

 Dim variablename [As Type]


Of course, the variable name could be whatever you want. The type however, is different. You have a choice of single, integer, or string. This tells the computer what type of information the variable holds. "Single" variables can hold numbers with decimal. "Integers" variables can hold whole numbers, while "String" variables holds text or a set of characters. If you don't dim a variable, the type would automatically be "Variant", which can hold almost all kinds of information. For example:

  Option Explicit
Dim intNumber As Integer

intNumber = 31             ' This is ok
intNumber = "I didn't"     ' Error: type mismatch (intNumber is an integer while "I didn't" is a string)


Dimming is especially important for arrays and matrices. For an array, next to the variable name, you enter the range of the array. For example:

 Dim x(1 to 10) As Integer


Arrays will be covered more in depth later. Matrices are dimensioned almost exactly like arrays are, however, instead of the having only one dimension (1 to 20), matrices may have two: (1 to 20,1 to 5), or even three. Dimensioning can also be used to tell the computer that variables are public. This will be discussed later in the Scope section.

Note: If you don't dimension your variables, you might end up with many unexpected errors. It could be avoided by using the Option Explicit statement, which requires every variable to be defined; if not every variable used in the program is defined, VB raises an error: "Variable is not defined". To enable this, you just have to type Option Explicit at the very top of ALL your code in the current module. It's a very good practice to do so.

## Simple output

The interaction between the user and the computer consists of both the input and output of data. The computer will not receive your commands if you don't have a mouse or keyboard which are used to input commands. And conversely, you wouldn't know what the computer is doing at all if there is no monitor or speaker which are used to output data. Therefore output is important.

### Message boxes

One of the easiest form of output is message box. I'm sure you've seen a lot of message boxes in Windows. This is what the code of a normal message box should look like.

 MsgBox("Hello world!")


Try it. Are you tired of the boring "Hello world!"? Let's make a fancier one:

 MsgBox("Fatal error: Your computer will be shut down in five seconds.", vbCritical, "System")


That will creep out quite a lot of people.

### Printing

Note: The word "printing" here means using the Print statement, it's not about using the printer or printing files. Printing is a fairly simple part of Visual Basic, but also essential. Printing is used to output information to the user. It proves to be a valuable troubleshooting tool. Whenever printing, you need an object to print on, followed by of course, something to print. Printing may be used with various objects, however, the most common in the picture box. For the sake of simplicity, we are assuming you renamed the picture box as "pic". In this wikibook though, print is done mainly on picture boxes and forms:

 pic.Print "Hello world!!"   'Prints message on picture box
Print "Hello world!!!"      'Prints message on current form


### Spacing

There are various ways to alter how text is spaced when printing. The most common is the comma. A comma will go to the next print zone. Print zones are 15 characters long. You can think of it like pressing the tab key when typing something out. Remember that print zones are fixed, so if you've typed 1 letter, and then used a comma, then it will be a big space. If you type 13 characters and use a comma, it will not be a large space. For example:

 Private Sub Form_Click()
Me.Print "Hello", "Next Zone"
End Sub


Several new concepts are introduced in this example. The "Form_Click" contains a block of code and it is called to run when the user clicks on the current Form(Form1). 'Me' is the same as the current form (Form1). Don't be afraid to experiment. No matter what you do in VB, its always reversible. Now, the comma isn't all that versatile. Another feature is tab. Tab will move so many spaces from the BEGINNING of the line. Followed by tab in parentheses is the amount of characters spaces. For example:

 Form1.Print "Hello"; Tab(10); "Yay"


This will NOT print "yay" 10 spaces after the O of "Hello". Rather it will print 10 spaces from the beginning of the line. You may use as many tabs as you want in the same print command. Although tab is useful, sometimes it is better to space things in relation to what has already been printed. This is where the space function comes in. The syntax of space is identical to that of tab. Space will move the next printed text so many spaces over from its CURRENT location. For example:

 Pic.print "Hello"; Space(10); "Yay"


This will print the first Y of "Yay" 10 spaces to the right of the O in "Hello". It is important to note, if you write:

 Pic.Print "Hello"
Pic.Print "Hello"


They will appear on separate lines as:

 Hello
Hello


This can be easily dealt with in the need of having separate print statements print on the same line. You merely have to change the code to: (note the semicolon)

 Pic.Print "Hello";
Pic.Print "Hello"


This will appear as:

 HelloHello


If you want to make a blank line in between the two "hello"'s, then you may simply have a blank print statement WITHOUT a semicolon. For example:

 Pic.Print "Hello"
Pic.Print
Pic.Print "Hello"


This will print as:

 Hello

Hello


It is important to remember that if the first print has a semicolon at the end, often referred to as a trailing semicolon, the empty print will only reverse it, and print the second Hello on the next line, and no blank line will appear.

# Simple Arithmetic

Visual Basic has all of the common arithmetical functions. It does not have complex numbers nor does it allow you to overload operators so manipulating complex numbers or matrices must be done by explicit function and subroutine calls.

## Arithmetic Operators

The operators are infix and take two arguments: arg1 operator arg2 except for unary plus and minus

### Numeric Operators

+ Adds two numbers. Also concatenates strings but avoid this use because Visual Basic will try to convert the string to a number first and the results might not be what you expect.
- Subtract the second number from the first.
- unary negate the operand.
* Multiply two numbers. The result will be promoted to whatever data type is needed to represent the size of the number if one of the numbers is already that type (see note below about odd behavior).
/ Normal division. Treats both operands as real numbers and returns a real result.
\ Integer division. Be careful with this because the arguments are converted to integers before the division is performed so use this with integer operands unless you comment your code carefully.

Also note that "/" and "*" are evaluated before "\", so (1000 \ 13 * 3) is not equal to ( (1000 \ 13) * 3)

Mod Produces the remainder after integer division. Be careful with this as the interpretation of the modulus operator in mathematics is ambiguous. a Mod b gives the same answer as this expression: a - (a \ b) * b
^ Raises the first operand to the power of the second. In Visual Basic either or both operands may be negative. If all you want is a square or cube it is faster to explcitly multiply the number by itself the appropriate number of times.

For example:

Text2 = Text1 * 5


Will display the value in Text1, multiplied by 5, in Text2. E.g. if Text1 = 4 then Text2 will be equal to 20.

### Order of operations

• Exponentiation (^)
• Multiplication and normal division (* and /)
• Integer division (\)
• Mod (Mod)

General hint : If an expression uses more than (+,-,*) use all possible brackets to force the expression to be evaluated the way you are thinking it.

### VB odd behavior note

VB considers "explicitly stated" integral numbers to be of type Integer (which must be between -32768 and 32767) if they are within (-32768, +32767) and gives an error if the result of arithmetic with them is more than 32768. This can be seen by trying

Debug.Print (17000 + 17000)


or

Debug.Print (17000 * 3)


which both cause an error. This can be solved (in a direct but ugly way) by enclosing numbers in CLng() (Convert to Long) so that

Debug.Print (CLng(17000) + CLng(17000))


or by using the type-declaration character & which specifies a Long constant:

Debug.Print (17000& + 17000&)


neither of which cause an error. To avoid having to think about this, avoid using explicit numbers in code and instead use "Long" variables and constants such as :

  Const TEST_NUM As Long = 17000&
Debug.Print (TEST_NUM + TEST_NUM)

Dim TestNumVar As Long
TestNumVar = 17000
Debug.Print (TestNumVar + TestNumVar)


### Boolean Arithmetic

Boolean operators use Boolean variables or integer variables where each individual bit is treated as a Boolean. There are six operators:

Operator: Meaning:
Not Negation
And Conjuncton
Xor Exclusive Or
Eqv Equivalence
Imp Implication

When you construct logical expressions with these operators you get the following results:

A B A And B A Or B A Xor B A Eqv B A Imp B
T T T T F T T
T F F T T F F
F T F T T F T
F F F F F T T

## Comparison Operators

These operators, composed of <, > and =, are use to decide whether one value is smaller than, larger than, or equal to another.

For example:

  Dim i
i = 50
If i < 0 Then
MsgBox "i is less than 0"
ElseIf i <= 100 And i >= 0 Then
MsgBox "i is less than or equal to one hundred and greater than or equal to 0"
ElseIf i > 100 And i < 200 Then
MsgBox "i is greater than one hundred less than 200"
Else
MsgBox "i is greater than or equal to 200"
End if


Caution! Due to the internal structure of floating-point numbers (Single and Double), do not use = or <> to compare them. Instead, use a small value (usually called Epsilon) as a "maximum difference". For example:

  ' This returns False :
Debug.Print (Sqr(1.234) * Sqr(1.234)) = 1.234
' This returns True :
E = 0.000001
Debug.Print Abs((Sqr(1.234) * Sqr(1.234)) - 1.234) < E

Operator Meaning
= Equality
<> Inequality
< Less than
> Greater than
>= Greater than or equal to. Or put another way: not less than
<= Less than or equal to. Or put another way: not greater than

## Built in Arithmetic Functions

There are not many native mathematical functions in Visual basic but this doesn't mean that you can't do significant calculations with it.

Abs(x)
returns the absolute value of x, that is, it removes a minus sign if there is one. Examples: Abs(3)=3 ; Abs(-3)=3
Exp(x)
returns the value ex. e is Euler's constant, the base of natural logarithms.
Log(x)
the Neperian ('Natural', e base) logarithm of x.
Randomize(x)
not really a mathematical function because it is actually a subroutine. This initializes the random number generator.
Rnd(x)
produces the next random number in the series. Please read that sentence again! the random numbers aren't really random, they are instead pseudo-random. If you initialize the random number generator with the same number each time you start a program then you will get the same series of values from Rnd()
Round(x,n)
returns a real number rounded to n decimal places (uses Banker's rounding).
Sgn(x)
returns plus one if x is positive, minus one if it is negative, zero if x is identically zero. Sgn(-5)=-1 ; Sgn(5)=1 ; Sgn(0)=0
Sqr(x)
square root of x. Example: Sqr(25)=5. x must be non-negative. Thus Sqr(-25) will generate an error

### Derived Functions

If you want logarithms to some other base you can use this expression:

Log(x, base) = Log(x) / Log(base)


And to calculate the nth root of a number (cube root, ...)

RootN(x, n) = x ^ (1.0 / n)


## Trigonometrical Functions

Visual Basic has the usual simple trigonometric functions, sin, cos, tan, but if you want some of the more unusual ones or inverses you will need to write some simple functions.

Remember that the angles must be supplied as radians

  radians = degrees * pi / 180

ArcSin(x) = Atn(x / Sqr(-x * x + 1))

ArcCos(x) = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)


Notice that the range of applicability of these expressions is limited to the range -1<=x<=1.

Here are some more:

 Secant Sec(x) = 1 / Cos(x) Cosecant Cosec(x) = 1 / Sin(x) Cotangent Cotan(x) = 1 / Tan(x) Inverse Sine Arcsin(x) = Atn(x / Sqr(-x * x + 1)) Inverse Cosine Arccos(x) = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1) Inverse Secant Arcsec(x) = Atn(x / Sqr(x * x - 1)) + Sgn((x) - 1) * (2 * Atn(1)) Inverse Cosecant Arccosec(x) = Atn(x / Sqr(x * x - 1)) + (Sgn(x) - 1) * (2 * Atn(1)) Inverse Cotangent Arccotan(x) = -Atn(x) + 2 * Atn(1) Hyperbolic Sine HSin(x) = (Exp(x) - Exp(-x)) / 2 Hyperbolic Cosine HCos(x) = (Exp(x) + Exp(-x)) / 2 Hyperbolic Tangent HTan(x) = (Exp(x) - Exp(-x)) / (Exp(x) + Exp(-x)) Hyperbolic Secant HSec(x) = 2 / (Exp(x) + Exp(-x)) Hyperbolic Cosecant HCosec(x) = 2 / (Exp(x) - Exp(-x)) Hyperbolic Cotangent HCotan(x) = (Exp(x) + Exp(-x)) / (Exp(x) - Exp(-x)) Inverse Hyperbolic Sine HArcsin(x) = Log(x + Sqr(x * x + 1)) Inverse Hyperbolic Cosine HArccos(x) = Log(x + Sqr(x * x - 1)) Inverse Hyperbolic Tangent HArctan(x) = Log((1 + x) / (1 - x)) / 2 Inverse Hyperbolic Secant HArcsec(x) = Log((Sqr(-x * x + 1) + 1) / x) Inverse Hyperbolic Cosecant HArccosec(x) = Log((Sgn(x) * Sqr(x * x + 1) + 1) / x) Inverse Hyperbolic Cotangent HArccotan(x) = Log((x + 1) / (x - 1)) / 2

The very useful atan2 function (calculate the angle in all four quadrants of a vector) can be simulated like this:

 Public Const Pi As Double = 3.14159265358979

Public Function Atan2(ByVal y As Double, ByVal x As Double) As Double

If y > 0 Then
If x >= y Then
Atan2 = Atn(y / x)
ElseIf x <= -y Then
Atan2 = Atn(y / x) + Pi
Else
Atan2 = Pi / 2 - Atn(x / y)
End If
Else
If x >= -y Then
Atan2 = Atn(y / x)
ElseIf x <= y Then
Atan2 = Atn(y / x) - Pi
Else
Atan2 = -Atn(x / y) - Pi / 2
End If
End If

End Function


Other functions:

  ASin(x) = Atan2(x, Sqr(1 - x * x))
ACos(x) = Atan2(Sqr(1 - x * x), x)


# Branching

A branch is a point at which your program must make a choice. With these data structures, it is now possible to make programs that can have multiple outcomes. You may be familiar with the first type from Algebra, an If-Then statement, or If P then Q. And they work pretty much the same. Another type is the Select Case, this may prove to be easier at times.

Another name for this concept is conditional clauses. Conditional clauses are blocks of code that will only execute if a particular expression (the condition) is true.

## If...Then Statement

If...Then statements are some of the most basic statements in all of programming. Every language has them, in some form or another. In Visual Basic, the syntax for an If...Then statement is as follows:

  If (condition) Then
(reaction)
End If

• condition - a set of test(s) that the program executes.
• reaction - the instructions that the program follows when the condition returns true. The condition returns true if it passes the test and returns false if it fails the test.

The condition can be anything from

  If X = 1 Then
MsgBox "X = 1"
End If


to

  If InStr(1, sName, " ") > 0 Then
sName = """" & sName & """"
End If


If there is only one reaction to the condition, the statement can also be expressed without the End If:

If Y + 3 = 7 Then MsgBox "Y + 3 DOES = 7"


There are also other parts to these statements to make them more complex. Two other terms that can be used are Else, and ElseIf.

Else will, if the condition is false, do whatever comes between the Else statement and the End If statement.

ElseIf will, if the condition directly proceeding it is false, check for another condition and go from there.

## If..Then..Else Statement

The If..Then..Else statement is the simplest of the conditional statements. They are also called branches, as when the program arrives at an "If" statement during its execution, control will "branch" off into one of two or more "directions". An If-Else statement is generally in the following form:

  If condition Then
statement
Else
other statement
End If


If the original condition is met, then all the code within the first statement is executed. The optional Else section specifies an alternative statement that will be executed if the condition is false. The If-Else statement can be extended to the following form:

  If condition Then
statement
ElseIf condition Then
other statement
ElseIf condition Then
other statement
...
Else
another statement
End If


Only one statement in the entire block will be executed. This statement will be the first one with a condition which evaluates to be true. The concept of an If-Else-If structure is easier to understand with the aid of an example:

  Dim Temperature As Double
...
If Temperature >= 40.0 Then
Debug.Print "It's extremely hot"
ElseIf 30.0 <= Temperature And Temperature<=39.0 Then
Debug.Print "It's hot"
ElseIf 20.0 <= Temperature And Temperature <= 29.0 Then
Debug.Print "It's warm"
ElseIf 10.0 <= Temperature And temperature <= 19.0 Then
Debug.Print "It's cool"
ElseIf 0.0 <= Temperature And Temperature <= 9.0 Then
Debug.Print "It's cold"
Else
Debug.Print "It's freezing"
End If


### Optimizing hints

When this program executes, the computer will check all conditions in order until one of them matches its concept of truth. As soon as this occurs, the program will execute the statement immediately following the condition and continue on, without checking any other condition for truth. For this reason, when you are trying to optimize a program, it is a good idea to sort your If..Then..Else conditions in order of descending likelihood. This will ensure that in the most common scenarios, the computer has to do less work, as it will most likely only have to check one or two branches before it finds the statement which it should execute. However, when writing programs for the first time, try not to think about this too much lest you find yourself undertaking premature optimization.

In Visual Basic Classic conditional statements with more than one conditional do not use short-circuit evaluation. In order to mimic C/C++'s short-circuit evaluation, use ElseIf as described in the example above. In fact for complicated expressions explicit If..Then..ElseIf statements are clearer and easier to read than the equivalent short circuit expression.

## Select Case

Often it is necessary to compare one specific variable against several constant expressions. For this kind of conditional expression the Select Case is used. The above example is such a case and could also be written like this:

  Select Case Temperature
Case Is >= 40#
Debug.Print "It's extremely hot"
Case 30# To 39#
Debug.Print "It's hot"
Case 20# To 29#
Debug.Print "It's warm"
Case 10# To 19#
Debug.Print "It's cool"
Case 0# To 9#
Debug.Print "It's cold"
Case Else
Debug.Print "It's freezing"
End Select


## Unconditionals

Unconditionals let you change the flow of your program without a condition. You should be careful when using unconditionals. Often they make programs difficult to understand. Read Isn't goto evil? below for more information.

### Exit

End a function, subroutine or property and return to the calling procedure or function. Note that in Visual Basic returning from a function and assigning a return value requires two separate statements.

For procedures:

Exit Sub


For functions:

Exit function


For properties:

Exit Property


### End

This simple command ends the execution of the program

For example:

  Private Sub cmd1_Click()
End
End Sub


In this example, when the button cmd1 is clicked, the program will terminate. The End command is provided for backward compatibility and is rarely (if ever) needed to end a VB program. The proper way to end a VB program is to release all object references, stop any running timers, and exit out of any executing procedures. In a small program this could mean to simply unload all the forms. When there are no active object references and no code running, VB will terminate the program in a graceful manner. Using End to end the program will terminate the program in an ungraceful manner (some things may not get shut down properly).

### Goto

Transfer control to the statement after the label (or line number).

      Goto Label
Dont_Do_Something
Label:
...


In Visual Basic the target of a Goto statement must be in the same procedure; this prevents abuse of the feature by making it impossible to jump into the middle of another subroutine.

#### Isn't Goto Evil?

One often hears that Goto is evil and one should avoid using goto. But it is often overlooked that any exit statement which is not the last statement inside a procedure or function is also an unconditional statement - a goto in disguise.

Therefore if you have functions and procedures with more than one exit statement you can just as well use goto. When it comes down to readability the following two samples are almost the same:

  Sub Use_Exit
Do_Something
If Test Then
Exit Sub
End If
Do_Something_Else
End Sub

Sub Use_Goto is
Do_Something
If Test Then
Goto Exit_Use_Goto
End If
Do_Something_Else
Exit_Use_Goto:
End Sub


In the pure structured approach, neither goto nor multiple exit statements are needed:

  Sub Use_If is
Do_Something
If Not Test Then
Do_Something_Else
End If
End Sub


A couple of notes:

• Return is a reserved keyword and can only be used with a matching Gosub and that the Gosub must reside in the same function, sub or property. It cannot be used to return from a function, sub or property - maybe it should read Exit Sub.
• Error handling in Visual Basic does need the use of Goto, but with a different syntax.
• Visual Basic, believe it or not, supports line numbering, so for example Goto 20 is perfectly acceptable if line 20 exists!
• Using Exit instead of Goto as shown above has some side effects. Calling Exit Sub clears a current error state, i.e. it is the equivalent of an Err.Clear.

#### Compatibility with VB.Net

If you are concerned that you might want to port your code to VB.Net without recasting it later you should probably avoid goto altogether.

One alternative is the pseudo loop. Its purpose is to fake a goto target that occurs later in the routine:

  Sub Use_PseudoLoop

Do 'Start the pseudo loop.  Exists only so that you can use Exit Do
Try_Something
If Test Then
Exit Do
End If
Try_Again
If Test Then
Exit Do
End If
Try_Try_Again
Loop While False ' will never loop
Do_One_Last_Thing

End Sub


Of course this example can be recast using If..Then..Else..End If without the need for any Exit statements at all so it is a little artificial:

  Sub Use_IfThen

Try_Something
If Not Test Then
Try_Again
If Not Test Then
Try_Try_Again
End If
End If
Do_One_Last_Thing

End Sub


# Loops

Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met.

Visual Basic has three main types of loops: for..next loops, do loops and while loops.

Note: 'Debug' may be a reserved word in Visual Basic, and this may cause the code samples shown here to fail for some versions of Visual Basic.

## For..Next Loops

The syntax of a For..Next loop has three components: a counter, a range, and a step. A basic for..next loop appears as follows:

For X = 1 To 100 Step 2
Debug.Print X
Next X


In this example, X is the counter, "1 to 100" is the range, and "2" is the step.

The variable reference in the Next part of the statement is optional and it is common practice to leave this out. There is no ambiguity in doing this if code is correctly indented.

When a For..Next loop is initialized, the counter is set to the first number in the range; in this case, X is set to 1. The program then executes any code between the for and next statements normally. Upon reaching the next statement, the program returns to the for statement and increases the value of the counter by the step. In this instance, X will be increased to 3 on the second iteration, 5 on the third, etc.

To change the amount by which the counter variable increases on each iteration, simply change the value of the step. For example, if you use a step 3, X will increase from 1 to 4, then to 7, 10, 13, and so on. When the step is not explicitly stated, 1 is used by default. (Note that the step can be a negative value. For instance, for X = 100 to 1 step -1 would decrease the value of X from 100 to 99 to 98, etc.)

When X reaches the end of the range in the range (100 in the example above), the loop will cease to execute, and the program will continue to the code beyond the next statement.

It is possible to edit the value of the counter variable within a for..next loop, although this is generally considered bad programming practice:

For X = 1 To 100 Step 1
Debug.Print X
X = 7
Next


While you may on rare occasions find good reasons to edit the counter in this manner, the example above illustrates one potential pitfall:

Because X is set to 7 at the end of every iteration, this code creates an infinite loop. To avoid this and other unexpected behavior, use extreme caution when editing the counter variable!

It is not required by the compiler that you specify the name of the loop variable in the Next statement but it will be checked by the compiler if you do, so it is a small help in writing correct programs.

### For loop on list

Another very common situation is the need for a loop which enumerates every element of a list. The following sample code shows you how to do this:

Dim v As Variant

For Each v In list
Debug.Print v
Next


The list is commonly a Collection or Array, but can be any other object that implements an enumerator. Note that the iterating variable has to be either a Variant, Object or class that matches the type of elements in the list.

## Do Loops

Do loops are a bit more flexible than For loops, but should generally only be used when necessary. Do loops come in the following formats:

• Do while
• Do until
• Loop while
• Loop until

While loops (both do while and loop while) will continue to execute as long as a certain conditional is true. An Until loop will loop as long as a certain condition is false, on the other hand. The only difference between putting either While or Until in the Do section or the Loop section, is that Do checks when the loop starts, and Loop checks when the loop ends. An example of a basic loop is as follows:

Do
Debug.Print "hello"
x = x + 1
Loop Until x = 10


This loop will print hello several times, depending on the initial value of x. As you may have noticed, Do loops have no built in counters. However, they may be made manually as shown above. In this case, I chose x as my counter variable, and every time the loop execute, x increase itself by one. When X reaches 10, the loop will cease to execute. The advantage of Do loops is that you may exit at any time whenever any certain conditional is met. You may have it loop as long as a certain variable is false, or true, or as long as a variable remains in a certain range.

### Endless loop: Do..Loop

The endless loop is a loop which never ends and the statements inside are repeated forever. Never is meant as a relative term here - if the computer is switched off then even endless loops will end very abruptly.

Do
Do_Something
Loop


In Visual Basic you cannot label the loop but you can of course place a label just before it, inside it or just after it if you wish.

### Loop with condition at the beginning: Do While..Loop

This loop has a condition at the beginning. The statements are repeated as long as the condition is met. If the condition is not met at the very beginning then the statements inside the loop are never executed.

Do While X <= 5
X = Calculate_Something
Loop


### Loop with condition at the end: Do..Loop Until

This loop has a condition at the end and the statements are repeated until the condition is met. Since the check is at the end the statements are at least executed once.

Do
X = Calculate_Something
Loop Until X > 5


### Loop with condition in the middle:Do..Exit Do..Loop

Sometimes you need to first make a calculation and exit the loop when a certain criterion is met. However when the criterion is not met there is something else to be done. Hence you need a loop where the exit condition is in the middle.

Do
X = Calculate_Something
If X > 10 then
Exit Do
End If
Do_Something (X)
Loop


In Visual Basic you can also have more than one exit statement. You cannot exit named outer loops using Exit Do because Visual Basic does not provide named loops; you can of course use Goto instead to jump to a label that follows the outer loop.

## While Loops

While loops are similar to Do loops except that the tested condition always appears at the top of the loop. If on the first entry into the loop block the condition is false, the contents of the loop are never executed. The condition is retested before every loop iteration.

An example of a While loop is as follows:

    price = 2

While price < 64
Debug.Print "Price = " & price
price = price ^ 2
Wend

Debug.Print "Price = " & price & ":  Too much for the market to bear!"


The While loop will run until the condition tests false - or until an "Exit While" statement is encountered.

## Nested Loops

A nested loop is any type of loop inside an already existing loop. They can involve any type of loop. For this, we will use For loops. It is important to remember that the inner loop will execute its normal amount multiplied by how many times the outer loop runs. For example:

For i = 1 To 10
For j = 1 To 2
Debug.Print "hi"
Next
Next


This will print the word 'hi' twenty times. Upon the first pass of the i loop, it will run the j loop twice. Then on the second pass of the i loop, it will run the j loop another two times, and so on.

# Strings

Visual Basic has a traditional set of built in string operations. Unlike many languages Visual Basic strings are always Unicode so they can hold any character. They are also always dynamically allocated and are of almost unlimited length (theoretically up to about 2^31 characters, about 2000 million).

Note that Unicode uses more than one byte to represent each character. As far as VB is concerned Unicode characters are two bytes which gives 2^16 or 65536 possible values. This is enough even for Chinese, Japanase and Korean character sets (CJK). In fact Unicode defines 17 planes each of which has room for 2^16 code points but VB (and Windows) uses only the Basic Multilingual Plane (BMP).

See What Are VB Strings? for a concise explanation of the internal workings of Visual Basic Classic strings.

## Built In String Functions

Visual Basic provides a reasonable set of traditional functions for manipulating and searching strings. These functions are usually all that is needed for most programs that are not primarily concerned with text processing.

## Regular Expressions

A regular expression is a character string in which some characters have special meanings. Such a string can be used to search for substrings in another string in much more sophisticated ways than the built in InStr function.

For example this expression:

 "(dog|cat)"


will match either 'dog' or 'cat'.

Visual Basic has no built in regular expression functions, but these are available in the VBScript regular expression library. If your program does a lot of text processing regular expressions are definitely worth learning even though they may seem intimidating at the start. In practice most programmers find that the more arcane expressions are rarely used and the same idioms are recycled over and over so there is really not as much to learn as it at first appears.

# Built In String Functions

## Comparison

Two strings are equal by value if they have the same content:

• If "Hello" = "Hello" Then MsgBox "A"

The statement Option Compare Text can be placed at the top of a module to make the comparison case-insensitive, impacting =, <, >, <=, >=, <>:

Option Compare Text
Sub Test()
If "Hello" = "hello" Then MsgBox "A"
If "aaa" < "BBB" Then MsgBox "B"
End Sub


To test whether two strings are equal by reference, that is, whether they start at the same address, you can use StrPtr function.

To test whether a string is greater than or less than another using lexicographical order, you can use <, >, <=, >=, <>. To decide whether a string is less than another, the two are compared character by character and as soon as a character from one string is found to have a lower ASCII code than the corresponding (same position) character in the other string, the first is declared to be less than the second. The converse test also works.

Another way of testing whether strings are equal or greater than one another is the StrComp function. Unlike =, <, >, etc., the StrComp function has an optional argument that controls whether the comparison is case-sensitive.

Example:

strHello = "hello": strHello2 = "Hello"
If StrComp(strHello, strHello2, vbTextCompare) = 0 Then
Debug.Print "The strings are the same."
End If


## Concatenation

The operator intended to perform string contatenation is &. The operator + can sometimes be used to the same effect, but not always:

a = "123"
b = "456"
c = a & b 'Yields 123456
d = a + b 'Yields 123456: applied to two strings
Debug.Print c, d

a = 123   'A number, not a string
b = "456" 'A string
c = a & b 'Yields 123456
d = a + b 'Yields 579: applied to a number and to a string
Debug.Print c, d


## Containment

To find out if one string is a substring of another, use the InStr function as follows:

  If InStr("Hello","He") > 0 Then
MsgBox "The second string is contained in the first string."
End If
If InStr("He","Hello") > 0 Then
MsgBox "This never gets printed; wrong argument order."
End If


InStr function returns the position of the substring if it is found or zero otherwise.

The two-argument use of InStr function is case-sensitive. A case-insensitive containment test can be done as follows:

  If InStr(UCase("Hello"), UCase("he")) > 0 Then
MsgBox "The second string is contained in the first string when we " & _
"disregard the case."
End If


## Replacing

To replace a string with another string inside a third string, use the built-in function Replace as follows:

  Result = Replace("Teh Beatles", "Teh", "The") 'results into "The Beatles"


## Indexing and Substrings

Strings can be used almost as if they were lists of characters. The nth character in a string can be returned by subscripting:

Mid$(String1, n, 1)  The values of n start from 1 rather than from 0. It is also possible to return a substring of a string. The same function is used but instead of specifying 1, you specify the length of the substring: Offset = 2: Length = 3 Debug.Print Mid$("abcdef", Offset , Length) 'Prints bcd


If you ask for more characters than are available, you get just what there is, no error is raised:

Debug.Print Mid$("abcdef", 2, 33) 'Prints bcdef  You can also use Mid$ on the left-hand side of an assignment:

String1 = "aaaaaa"
Debug.Print String1 'Prints aaaaaa
Mid$(String1, 2, 3) = "bbb" Debug.Print String1 'Prints abbbaa Mid$(String1, 3, 1) = "cccccc"
Debug.Print String1 'Prints abcbaa
Mid$(String1, 2, 3) = "d" Debug.Print String1 'Prints adcbaa  When Mid$ is on the left, it doesn't change the total length of the string: it just replaces the specified number of characters. If the right-hand side specifies fewer characters than are asked for, only that number of characters is replaced; if it specifies more, only the number asked for is used.

• MID, MIDB, Excel 2003, office.microsoft.com

## String constants

String constants can be declared like any other constant:

 Const s As String = "abcdef"


## String Functions

Strings are not objects so they do not have methods but there is a number of functions that manipulate strings. Note that none of the functions modify the original string, except for Mid$when it is on the left hand side of an assignment statement: Asc Returns the integer code of the first character of the string. The inverse function would be Chr. Len Returns the length of the string. InStr Returns the character index of the first occurrence of the substring in a string or zero if the substring is not found. InstrB Like InStr except that it returns the byte position. It has to be remembered, that Visual Basic 6 strings are Unicode strings. InstrRev Like InStr except that it returns the character position of the last occurrence of the substring. Left$
Returns the specified number of characters from the beginning of the string. If there are fewer characters in the string Left$returns the whole string, no error is raised, Mid$
Returns a number of characters starting at the given position, on the left hand side it replaces those characters,
Right$Returns the specified number of characters from the end of the string, if there are not that many characters, then Right$ returns the whole string.
IsNumeric
Returns true if the string looks like a number.
LTrim$, RTrim$, Trim$Returns a copy of the string with leading, trailing or leading and trailing spaces removed respectively. Note that only ASCII spaces (character code 32) are removed, other whitespace characters such as tabs are treated as non-spaces. LCase$, UCase
Converts the whole string to lower case or upper case respectively.
Val
Returns a number corresponding to the number found at the start of the string. Note that Val is not locale aware, which means that it always expects decimal points regardless of the regional settings of your computer; if you are reading from a comma delimited file this is probably the function you want to use.
Str
Returns a string corresponding to the given number. Like Val this is not locale aware. This is the function you should use if you are creating a text file containing numbers to be read on someone else's computer.
CStr
Converts the expression to a string. This procedure is locale aware and the correct function to use if converting numbers and differently typed values to strings for user-display. Usually it is unnecessary because Visual Basic automatically converts when necessary and uses Regional Settings to do so.
Format$Converts a number to a string using a specific format. The format is provided as a string of characters, that shows how many digits should be given before and after the decimal point. Like CStr, Format$ is locale aware so the decimal separator will be whatever is specified in the user's Regional Settings. Format$also provides for conversion of dates to various built-in and custom string formats. CBool, CByte, CCur, CInt, CLng, CSng, CDbl, CDec Locale aware conversions to Boolean, Byte, Currency, Integer, Long, Single, Double, Decimal. Split Chops a string into pieces and returns a Variant Array. If no delimiter is specified then spaces will be used. Delimiters may be any string of any length. Two adjacent delimiters delimit an empty string. Hex$
Returns a string of Hex characters representing a number.
Oct$Returns a string of Octal characters representing a number. Replace$
Returns a string with occurrences of a specified substring replaced with a new string. Note that the substring and the new string do not have to be the same size.
StrComp
Returns -1 if the first string is less than the second, 0 if they are identical, +1 if the first is greater than the second. Takes an optional argument that determines the comparison algorithm: vbBinary for exact comparisons using the character codes, vbTextCompare for case insensitive comparisons.

## Quotes in strings

Because the double quote (") is used to delimit strings, you can't use it directly to specify a quote within a string. For example

 ' Does not work - syntax error
Debug.Print "Fred says "Hi" to you"


One way is to use that Chr() function to specify the character code (34)

 ' Works fine
Debug.Print "Fred says " & Chr$(34) & "Hi" & Chr$(34) & " to you"


Another is to double the double-quotes. You might find this more or less readable than the above way.

 ' Works fine too
Debug.Print "Fred says ""Hi"" to you"


## Startswith and Endswith

Visual Basic does not have functions "startsWith" (or "BeginsWith") and "endsWith" found in some other programming languages. But it has "Like" comparison operator used for simple pattern matching that does the job when used with "*" to stand for "any string of characters":

If "Hello World" Like "Hello*" Then MsgBox "It starts."
If "Hello World" Like "*World" Then MsgBox "It ends."
If LCase("Hello World") Like "hello*" Then MsgBox "It starts, case insensitive."
If LCase("Hello World") Like "*world" Then MsgBox "It ends, case insensitive."
Ending = "World"
If "Hello World" Like "*" & Ending Then MsgBox "It ends."


Furthermore, you can write these functions yourself using "Left" function:

Function StartsWith(Str1 As String, Str2 As String, Optional CaseIns As Boolean) As Boolean
StartsWith = False
If CaseIns Then 'Case insensitive
If Left(LCase(Str1), Len(Str2)) = LCase(Str2) Then
StartsWith = True
End If
Else
If Left(Str1, Len(Str2)) = Str2 Then
StartsWith = True
End If
End If
End Function

Function EndsWith(Str1 As String, Str2 As String, Optional CaseIns As Boolean) As Boolean
EndsWith = False
If CaseIns Then 'Case insensitive
If Right(LCase(Str1), Len(Str2)) = LCase(Str2) Then
EndsWith = True
End If
Else
If Right(Str1, Len(Str2)) = Str2 Then
EndsWith = True
End If
End If
End Function


For a one-off comparison to a constant string, a function call may be an overkill:

If Left(String1, 9) = "Beginning" Then
'Starts with, case-sensitive
...
Else
...
End If

If Right(String1, 3) = "end" Then
'Ends with, case-sensitive
...
Else
...
End If


## Pattern Matching

You can do simple pattern matching with Like keyword; for complex pattern matching, see Regular Expressions. The special characters in the patterns of Like include ? for a single char, * for any number of chars, # for a single decimal digit, [...] for a single char in the list, and [!...] for a single char not in the list.

Examples:

If "Hello World" Like "Hello*" Then MsgBox "A"
If "Hello World" Like "*World" Then MsgBox "B"
If "Itam" Like "It?m" Then MsgBox "G"
If "AB345" Like "[A-Z][A-Z]###" Then MsgBox "C"
If "Ab345" Like "[a-zA-Z][a-zA-Z]###" Then MsgBox "D"
If "Item" Like "[!;][!;][!;][!;]" Then MsgBox "E"
If Not "It;m" Like "[!;][!;][!;][!;]" Then MsgBox "F"


# Regular Expressions

Sometimes, the built in string functions are not the most convenient or elegant solution to the problem at hand. If the task involves manipulating complicated patterns of characters, regular expressions can be a more effective tool than sequences of simple string functions.

Visual Basic has no built-in support for regular expressions. It can use regular expressions via VBScript Regular Expression Library, though. If you have Internet Explorer installed, you almost certainly have the library. To use it, you must add a reference to the project; on the Project menu choose References and scroll down to Microsoft VBScript Regular Expressions. There might be more than one version; if so, choose the one with the highest version number, unless you have some particular reason to choose an old version, such as compatibility with that version on another machine.

## Class outline

Class outline of VBScript.RegExp class:

• Attributes
• RegExp.Pattern
• RegExp.Global
• RegExp.IgnoreCase
• RegExp.MultiLine
• Methods
• RegExp.Test
• RegExp.Replace
• RegExp.Execute

## Constructing a regexp

A method of constructing a regular expression object:

   Set Regexp = CreateObject("VBScript.RegExp")
Regexp.Pattern = "[0-9][0-9]*"


A method of constructing a regular expression object that requires that, in Excel, you set a reference to Microsoft VBScript Regular Expressions:

   Set Regexp = new RegExp
Regexp.Pattern = "[0-9][0-9]*"


## Testing for match

An example of testing for match of a regular expression

   Set RegExp = CreateObject("VBScript.RegExp")
RegExp.Pattern = "[0-9][0-9]*"
If RegExp.Test("354647") Then
MsgBox "Test 1 passed."
End If
If RegExp.Test("a354647") Then
MsgBox "Test 2 passed." 'This one passes, as the matching is not a whole-string one
End If
If RegExp.Test("abc") Then
MsgBox "Test 3 passed." 'This one does not pass
End If


An example of testing for match in which the whole string has to match:

   Set RegExp = CreateObject("VBScript.RegExp")


## Splitting

There is no direct support for splitting by a regular expression, but there is a workaround. If you can assume that the split string does not contain Chr(1), you can first replace the separator regular expression with Chr(1), and then use the non-regexp split function on Chr(1).

An example of splitting by a non-zero number of spaces:

  SplitString = "a b  c   d"
Set Regexp = CreateObject("VBScript.RegExp")
Regexp.Pattern = "  *"
Regexp.Global = True
Result = Regexp.Replace(SplitString , Chr(1))
SplitArray = Split(Result, Chr(1))
For Each Element In SplitArray
MsgBox Element
Next


## Example application

For many beginning programmers, the ideas behind regular expressions are so foreign that it might be worth presenting a simple example before discussing the theory. The example given is in fact the beginning of an application for scraping web pages to retrieve source code so it is relevant too.

Imagine that you need to parse a web page to pick up the major headings and the content to which the headings refer. Such a web page might look like this:

  <html>
<title>RegEx Example</title>
<body>
<h1>RegEx Example</h1>
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
<h2>Level Two in RegEx Example</h2>
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
<h1>Level One</h1>
cccccccccccccccccccccccccccccccccccccc
<h2>Level Two in Level One</h2>
dddddddddddddddddddddddddddddddddddd
</body>
</html>


What we want to do is extract the text in the two h1 elements and all the text between the first h1 and the second h1 as well as all the text between the second h1 element and the end of body tag.

We could store the results in an array that looks like this:

 "RegEx Example" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n

Level Two in RegEx Example

\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" "Level One" " cccccccccccccccccccccccccccccccccccccc\n

Level Two in Level One

\n dddddddddddddddddddddddddddddddddddd"

The \n character sequences represent end of line marks. These could be any of carriage return, line feed or carriage return followed by line feed.

A regular expression specifies patterns of characters to be matched and the result of the matching process is a list of sub-strings that match either the whole expression or some parts of the expression. An expression that does what we want might look like this:


"<h1>\s*([\s\S]*?)\s*</h1>"



Actually it doesn't quite do it but it is close. The result is a collection of matches in an object of type MatchCollection:


Item 0
.FirstIndex:89
.Length:24
.Value:"<h1>RegEx Example</h1>"
.SubMatches:
.Count:1
Item 0
"RegEx Example"
Item 1
.FirstIndex:265
.Length:20
.Value:"<h1>Level One</h1>"
.SubMatches:
.Count:1
Item 0
"Level One"



The name of the item is in the SubMatches of each item but where is the text? To get that we can simply use Mid\$ together with the FirstIndex and Length properties of each match to find the start and finish of the text between the end of one h1 and the start of the next. However, as usual there is a problem. The last match is not terminated by another h1 element but by the end of body tag. So our last match will include that tag and all the stuff that can follow the body. The solution is to use another expression to get just the body first:

 "<body>([\s\S]*)</body>"


This returns just one match with on sub-match and the sub match is everything between the body and end body tags. Now we can use our original expression on this new string and it should work.

Now that you have seen an example here is a detailed description of the expressions used and the property settings of the Regular Expression object used.

A regular expression is simply a string of characters but some characters have special meanings. In this expression:

 "<body>([\s\S]*)</body>"


there are three principal parts:

 "<body>"
"([\s\S]*)"
"</body>"


Each of these parts is also a regular expression. The first and last are simple strings with no meaning beyond the identity of the characters, they will match any string that includes them as a substring.

The middle expression is rather more obscure. It matches absolutely any sequence of characters and also captures what it matches. Capturing is indicated by surrounding the expression with round brackets. The text that is captured is returned as one of the SubMatches of a match.

 matches just ( begins a capture expression [ begins a character class \s specifies the character class that includes all white space characters \S specifies the character class that includes all non-white space characters ] ends the character class * means that as many instances of the preceding expression as possible are to be matched ) ends the capture expression matches

In the case studies section of this book there is a simple application that you can use to test regular expressions: Regular Expression Tester.