In the earlier lesson you were introduced to coding, and reviewed a chart which groups VB commands into useful categories. In this section of the tutorial, I take each one of the commands and provide the syntax and a brief description of what the command does. For details, you can turn to the VB HELP file. My intent is to allow you to skim quickly over the commands to determine which one can help out in your situation. This page is kind of long, but I wanted to keep it all on one page to make it easier for you get scroll through it. I've also summarized the contents of this page in a command summary chart.

Operators

Here are the VB operators used to perform mathematical operations on one or more variables. Aside from the normal multiply/add/subtract and divide, you will find the AND, OR, Not Equal, MOD and Integer Division operators very useful.

Math

VB also provides built-in functions which can act on variables. Most are self-explanatory. In my experience, the VAL, RND, and ROUND functions are among the most valuable, so be sure to pay close attention to them!

Strings

In my experience these functions are used more than just about any of the other VB built-in functions. The FORMAT, MID, and INSTR functions are incredibly powerful and I use them extensively. If you don't understand what they are, they worth the time to figure out! The LEN and CHR functions are also valuable as are the variations on the trim and case functions.

Arrays

Every programmer eventually uses arrays. Mostly they're pretty easy to understand. Take note, however, that you can resize an array with REDIM without losing the data. For details, see the PRESERVE keyword in the HELP entry on REDIM. If you use the LBound/UBound in your code instead of hard-coding the dimension of the array, you can later change the size of the array without touching your code!

File Handling (Generic)

While VB is working on a better approach (FileSystemObject), the built-in file handling statements are still the only way to access data other than through the VB database capabilities. Your skills in this area can make or break your ability to work with various formats. The OPEN/CLOSE statements are critical to success, but the LOF, EOF, and LEN functions are used even more often! It's also a given that you'll use the DIR function regularly.

File Handling - ASCII-specific

While VB is working on a better approach (FileSystemObject), the built-in file handling statements are still the only way to access data outside of a data base. Your skills in this area can make or break your ability to work with various formats. The OPEN/CLOSE statements are critical to success, but the LOF, EOF, and LEN functions are necessary to build useful code.

File Handling - Binary-specific

VB also support features which allow you to access a file on a byte-by-byte basis. The good thing about it is that you have more control, the bad thing is that you may have to write more code. Generally, a programmer will use the option (ASCII or Binary access) according to the least code he has to write. For binary access the Get/Put are equivalent to the Line Input and Print functions used in ASCII text file access. The big difference between the two is that binary access will read (Get) an exact number of bytes of data, and the reading can start at any byte within the file.

Declarations

I probably get more questions about the functions in this section than about any other group. In general, the concepts are pretty simple, but the details of getting it exactly right can cause even experienced programmers trouble. Focus on understanding Dim/ReDim/Public/Private/Sub/Function/Type and Set. However, they're all useful at times, so bear down and commit these to memory. I'll try to add more text and tips on these than I have on the others.

Dim - Used to define a variable as a certain type

i = dim i as integer, r as single

You can use the Option Explicit to make sure that VB forces you to declare every variable you use. DIM is that simplest way to declare a variable

ReDim - Used to change the dimensions of a dynamic array

redim arrayname(37)

Don't be afraid of this one. You can use ReDim to create an array whose size grows by 1 every time you want to add a number to it. Then, the UBound tells you how many numbers you've added.

Static - Establishes a procedure variable which keeps its value between calls

static i as integer

For example, if you want to keep track of how many times you've been in a procedure, set a counter as STATIC and increment it by one for each visit to the procedure. It will never go away until the program is terminated.

Public - Creates a variable which can be accessed outside its own procedure

Public i as integer

Even if you're the only programmer writing code in your application, use of Private vs Public will help catch errors if you inadvertently try to access an out-of-scope variable

Private - Creates a variable that can be read only in its own procedure or module, according to where the declaration took place.

private i as integer

Use this as often as possible to avoid unnecessary exposure of your variables to coding mistakes.

This is a really excellent way to keep several kinds of data under one variable name. Plus, you can PUT or GET a user-defined type with a single line of code.

Basically, use this to give easy to remember names to values. For example, suppose you use the value 37.2 a lot in your code, then if you put CONST MyAge = 37.2 in your code you'll be able to insert the MyAge where the 37.2 should have gone. Easier to type and easier to read. Also, you can chane the value of the constant by changing only the declaration line of code, rather than searching out every place the value was used!

Date/Time

These functions are pretty self-explanatory so I've not added any extra comments to them.

Miscellaneous

In this list you'll find some of the features of VB about which I get a lot of email questions! The MsgBox is easily the most used of the bunch. It handles all of the "Y/N" queries to your user so get to know it well. Also, the DoEvents, Shell, and Command functions are indispensable in certain occasions so make sure you know when they should be used.

Registry

I've never quite understood why Microsoft gotaway from the use of an INI file. The ability to use a simple text editor to resolve problems with a program's settings was a key feature about INI files. Also, no matter how Windows crashed, the INI file was protected.

Whining aside, VB has made it incredibly easy to access values in the registry. The following VB functions are simple to use and there's hardly any excuse for not taking advantage of them. One thing to remember is that the registry save strings so if you're saving or reading numeric information then may have to do some string manipulation with the results.

Loops and Conditional Decisions

While the event-driven model of VB has taken out a lot of the need for controlling the flow of your application, don't think for a second that you can get by without being an expert on these features of VB. Virtually every single procedure you'll ever write will have one or more of these in it. The concepts are simple, so take the time to become a master of each one! The For...Next and the Select Case statements are the two most used, so concentrate on them first.

Special Values

There are some keywords in VB which take on special meaning. Their use can be confusing at times, but you'll get used to the terminology as your programming experience grows.

Error Handling

Try as I might, I cannot create error free code! So, I turn to these VB features to help me figure out what went wrong.

Financial Calculations

For those folks who want to use VB for performing routine investment calcuations, VB provides a variety of functions. Personally, I use them very infrequently, but I suspect they are used regularly by a lot of programmers. I've never gotten a single question in the mail about these functions!