VB Commands
VB Commands
Operators
Here are the VB operators used to perform mathematical operations on one or more variables. Aside from
the normal multiply/add/substract and divide, you will find the AND, OR, Not Equal, MOD and Integer
Division operators very useful.
z / - Normal division
z \ - Integer division (truncates the answer)
z ^ - Exponentiation operator
z * - Multiply
z + - Plus
z - - Minus
z = - Equal
z > - Greater Than
z < - Less Than
z <> - Not Equal
z >= - Greater than or equal
z <= - Less than or equal
z AND - Defines a boolean value that is the AND of two values
{ result = expression1 AND expression2
z OR - Defines a boolean value that is the OR of two values
{ result = expression1 OR expression2
z XOR - Defines a boolean value that is the exclusive OR of two values
{ result = expression1 XOR expression2
z NOT - Defines an opposite boolean value
{ A = NOT B
z EQV - Performs a logical equivalence on two expressions (result is true if both expressions are true)
{ result = expression1 EQV expression2
z IMP - Performs a logical implication on two expressions
{ result = expression1 IMP expression2
z IS - Determines if 2 variables reference the same object
{ result = object1 IS object2
z LIKE - Determines if one string matches a pattern
{ result = string LIKE pattern
z MOD - Returns the integer remainder of a division
{ i = 27 MOD 5
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!
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 2 of 11
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 are worth the time to figure out! The LEN and CHR functions are also
valuable as are the variations on the trim and case functions.
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 3 of 11
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!
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 4 of 11
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.
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 5 of 11
z Width # - Set the output line width used by the OPEN statement
{ Width #2, 80
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.
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.
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 6 of 11
{ 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.
z 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.
z 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
z 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.
z Sub - Defines a procedure which can execute a block of code
{ Sub NewProcedure (var1 as integer, var2 as string)
{ Be sure to check out HELP for how to handle Sub arguments. There are more questions and
mistakes made concerning the use of arguments than just about anything else I've seen.
z Function - Declares a procedure which can return a value
{ Function NewFunction (var1 as integer, var2 as string) as SINGLE
{ This is actually the most versatile of the Sub/Function procedure types. It can do anything a
Sub can do as well as returning a value for use in an expression.
z Call - Transfers control to a Sub or Function (is optional)
{ Call Procedure 1
{ Since the use of CALL is optional, forget you ever saw it
z CallByName - Executes a method of an object or set/returns a property
{ CallByName(form1,procedurename,vbMethod)
{ The really cool thing about this is that you don't have to hardcode a procedure call. Just use a
string variable with the name of the procedure to call.
z Option Explicit - Instructs VB to force an explicit declaration of all variables
{ Option Explicit
{ You're borderline stupid if you don't use it to catch typing errors. Set up the VB IDE to
automatically include this in all projects.
z Option Compare - Instructs VB on how to make string comparisons
{ Option Compare Binary
{ This can add case-insensitivity for those times when you don't want to hard-code it
z Option Private - Prevents a module's content from being referenced outside a project.
{ Option Private Module
{ Generally doesn't apply to most VB applications. If you find a good use for it let me know.
z Property Get - Declares how to get the value of a property
{ Property Get Name()
{ You won't use this much until you get into creating classes of your own
z Property Let - Declares how to assign a value to a property
{ Property Let Name()
{ You won't use this much until you get into creating classes of your own
z Property Set - Declares how to set a variable reference to an object
{
{ You won't use this much until you get into creating classes of your own
z Set - Assigns an object reference to a variable
{ Set X = form1.txtInputFromUser
{ Very useful for making code more readable or simply to cut down on how much typing you
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 7 of 11
have to do!
z Let - Precedes assignment of a value to a variable
{ Let i = 3
{ It's optional, no one uses, so forget you ever saw it
z Type...End Type - Creates a user defined part type which consists of standard VB data types
{ type anytypename
{ one as string
{ two as integer
{ three as boolean
{ End Type
{ 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.
z Const - Creates a variable whose value is fixed
{ const anyname
{ 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!
z Declare - Used to define a procedure that exists in another file
{ declare functionname (arg1 as integer, arg2 as string) as integer
{
{ ArrayName = Array (10, 20, 30)
{ Implements - Specifies a class to be implemented in a module
{ Friend - Allows procedure to be callable from modules outside the class
{ GetObject - Return a reference to an ActiveX component
{ CreateObject - Creates and returns a reference to an ActiveX object
{ GetAutoServerSettings - Returns information about the state of an ActiveX component's
registration.
{ Enum - Declares a type for an enumeration
{ Event - Declares a user-defined event
{ TypeName - Returns the type of data in a variable
{ VarType - Returns the type of data in a variable
{ DefType - Sets the default data type of variables
DefInt A-Z
{ IS - A variety of data type or status checking options
IsArray, IsBindable, IsBroken, IsDate, IsDirty, IsEmpty, IsError, IsMissing, IsNull,
IsNumber, IsObject, IsReady, IsRootFolder
Date/Time
These functions are pretty self-explanatory so I've not added any extra comments to them.
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 8 of 11
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.
z MsgBox - A built-in dialog box that gives a message and allows a user input
i = msgbox "Read this!", vbokonly, "Test Message"
z DoEvents - Allows VB to complete pending tasks
doevents
z Shell - Executes a 2nd program from within the current program
shell "notepad.exe"
Note - VB does not wait for the Shell'd program to quit before executing the next line of code!
z Command - Gives any text that followed a VB .EXE execution command
temp$ = command
z Environ - Returns the system environmental space content
temp$ = environ
z Beep - Makes the computer beep once.
beep
z InputBox - A built-in dialog box that allows entry of a text string
inputbox "Input a value!", 5
z AddressOf - Provides an entry point for an external program to use a procedure
AddressOf ( procedurename )
z AppActivate - Activates an applications window
AppActivate ( windowtitle )
z RaiseEvent - Fires an event declared at module level
RaiseEvent ProcedureName
z Load - Load an object
load form1
z Unload - Unload an object
Unload form1
z LoadPicture - Load a picture into a control property
form1.picture = loadpicture (filename)
z SavePicture - Save a picture to a file
SavePicture(form1.picture,filename)
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 9 of 11
Registry
I've never quite understood why Microsoft got away 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.
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.
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 10 of 11
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.
http://www.5starsupport.com/info/vb.htm 30/11/2010
Visual Basic Command Descriptions Page 11 of 11
Financial Calculations
For those folks who want to use VB for performing routine investment calculations, 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!
http://www.5starsupport.com/info/vb.htm 30/11/2010