Chapter 7. The Language Reference

VBA is a high-level language and, like all high-level languages, it is, by its very nature, a large yet rich language. While this means that it takes time for new users to understand the intricacies of the many and varied functions and statements available to them, at the same time the language's syntax is straightforward, logical, and therefore easy to understand.

Perhaps the biggest obstacle to overcome is deciding which one of a number of functions and statements available to perform roughly the same task should be used. This situation has come about as the language has evolved over a number of years, and older statements have been left in the language to provide backward compatibility. If you always bear in mind that the language was originally made up of statements, that later versions introduced functions, and that only recently have object models been provided, then you can pinpoint the latest functionality within the language.

To help speed the process of finding the right function or statement to perform a particular task, you can use Appendix A, to determine what language elements are available for the purpose you require. You can also make use of the VB or VBA Object Browser to search or browse the VBA library.

As I've stated several times already, this book concentrates on the VBA language, and therefore doesn't include the following components, which don't form a part of the VBA language:

  • The methods, properties, and events associated with VB forms and controls

  • The methods, properties, and events associated with VBA UserForms and the VBA controls available from the Microsoft Forms library

  • The objects, as well as their associated properties, methods, and events, that are provided by each application that hosts VBA

You will, however, come across some (but thankfully very few) language elements that relate only to VB or only to VBA. There are two reasons for this:

  • The VBA DLL that provides VB with its language is slightly different than that used in the hosted version of VBA, since they were released at different times.

  • VB includes several unique functions to support its ability to create stand-alone applications as either EXEs, DLLs, or OCXs.

The elements of the VBA language can be broken into four main areas: statements, functions, operators, and object models.


Statements

Statements form the cornerstone of the language. You'll notice from Appendix A that the largest concentration of statements is in the program structure section. Statements are mainly used for such tasks as declaring variables or declaring procedures.

There is another large concentration of statements in the file manipulation section of VB. Again, these statements predate object programming in VB. It's likely that the new File System object model released with VB6 will replace the intrinsic VBA file statements.

Some statements in VB are quite old, having their roots back in (and before) Version 1 of VB. (Remember that Version 1 of VB itself represented a graphical interface design program grafted onto the Basic language.) In some cases, statements have been replaced by newer and more flexible functions, but the statement remains in the language for compatibility. That said, there are literally only one or two instances where both a statement and function of the same name exist.

Most statements don't accept named arguments, since this concept didn't exist in the early days of VB. In the main, statements don't return a value. Therefore, you are often well-advised to wrap these statements in a custom function that can return a value you can use to determine if a task was successful or not.

Some newer statements are in fact methods of VBA class libraries. For example the DeleteSetting statement is a method of the VBA.Interaction class.


Functions

Functions are relatively new additions to the language, most dating back to the Version 4 rewrite of VB and VBA. In the main, functions return a value, although, as with any function, you can choose to ignore the return value.

In addition to returning a value, there are other important benefits of functions over statements:

  • You can view most functions in the VB Object Browser, whereas most statements don't appear.

  • Most functions accept named arguments. This means that you can improve the readability of your code by using the name of an argument and the special argument assignment operator ":=", as this code fragment shows:

    Dim iReturnVal As Integer
    iReturnVal = MsgBox(Prompt:="Click OK", Title:="Test")


Operators

An operator connects or performs some operation upon one or more language elements to form a single expression. For example, in the code fragment:

strResult = 16 + int(lngVar1)

the addition operator (+) combines 16 and the value returned by int(lngVar1) into a single expression whose value is assigned to the variable strResult.


Object models

Object models are increasingly used to provide additional functionality in the VBA language. This is an ideal way of extending the language, since VBA itself is adept at handling object models.

Interestingly, the VBA language is itself implemented as an object model, although very rarely is it used as such. Since it's a flat model (that is, it doesn't define a class hierarchy), it isn't actually thought of as an object model. Nevertheless, if you use the object browser to examine the VBA component, you'll see that it's an external library containing various classes.

Some of the latest additions to VBA have used object models. For example, the Scripting Runtime provides us with the File System object model and the Dictionary object. VB6 also includes the Data Binding and Data Formatting objects. As time goes by we'll find more and more object models augmenting and replacing statements and functions in VBA.

Because of their importance to VB6, I've included full descriptions of the File System, Dictionary, Data Binding, and Data Formatting object models in this language section. Note that as long as the object libraries are available and are registered in the system registry, their objects are available to all 32-bit VB and VBA applications and macros developed in the current version, as well as earlier versions of VB and VBA-hosted applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset