The VBA 2.0 Engine

by Adam Maloney and by Andrew Lockwood - GUI Computing

With the inclusion of the VBA 2.0 engine in VB4, we didn't just get dragged kicking and screaming into the wonderful world of Microsoft-standard VBA syntax... we actually got some real benefits as well. Now there's a change.

One of the handy additions to VB4 is that of a line continuation character, the underscore "_". Its main benefit is in making code easier to read, especially when dealing with long statements such as message boxes, API Declarations or long SQL statements.

Another useful addition is VB4's built-in constants. Now you don't have to worry about going to the help file to find the numeric value needed for an exclamation mark icon in a message box, just use vbExclamation. Most of the standard functions within VB4 now have associated in built constants which are well named to be representative of their use and with over 1000 of them, there's plenty to go round !

The Public and Private Statements.

The Public and Private statements are new to VB4 and can be used to precede function, procedure and even variable declarations. When a procedure is declared as Private, it is exposed only to other procedures within the module in which it is declared. Variables declared as Private are also only accessible within the module or procedure in which they were declared, much like VB3.

When a procedure is declared as Public, it is exposed 'globally' within the project, allowing any other module access to the procedure at any time. However Visual Basic Help neglects to inform you that you must use the following syntax FormName.ProcedureName when calling procedures across forms. A Variable declared as public has the same scope as a procedure and must also be preceded by its FormName when being referenced across forms. As a general rule however, Public procedures and variables should be located in a single code module, as this makes them easier to reference and locate.

In VB3, whenever a function or procedure was declared within a .BAS file, it became accessible to all other functions in the current project. In VB4, through use of the Public and Private statements, the functions, procedures and declared variables within any given module, not just .BAS files, can be exposed or hidden from the rest of the project as desired.

Optional Arguments

The Optional keyword indicates that an argument is not required. If used, all subsequent arguments in the argument list must also be declared Optional and all Optional arguments must be Variant. Additionally, Optional arguments can't be used in conjunction with a ParamArray.

Optional parameters must be catered for in a procedure using the IsMissing function as follows:

  Sub DoSomething(arg1$, Optional arg2 as Variant)
    If IsMissing(arg2) then

      'Set arg2 to a default here
      arg2 = DefaultValue
    End If
  End Sub

Named Arguments

VB4 now allows for the use of Named arguments. This allows values to be set for a particular argument within a procedure by naming the argument/s when that procedure is called. Here is a simple function we can use for demonstrating named arguments.
  Function FunctX(Arg1 As String, Arg2 as Integer) as Integer
  End Function
The function can be called in the following way - using named arguments to assign the Arg2 variable the value of 7.
  RetVal = FunctX(Arg2 := 7, "Something")	
One feature of named arguments is that the order in which the arguments are passed or set doesn't matter as the values are being passed directly into the argument named. This is why the following code samples work in the same way.
  RetVal = FunctX("A Lucky Number",7)
  RetVal = FunctX(Arg2 :=  7, Arg1 :=  "A Lucky Number")
The ParamArray keyword simply enables a function to accept a variable number of arguments which are passed into a Parameter Array. This is good to use when the procedure or function is always going to require at least one variable and may require more.
  Function FunctY(FirstArg As String, ParamArray XArgs())
If the above function is invoked as follows:
  ReturnValue = FunctY ("A","B","C","D")
The variables within the function are assigned the following values:
  FirstArg = "A", XArgs(1) = "B", XArgs(2) = "C", XArgs(3) = "D"
Two items of note when using the ParamArray keyword is that the parameter array argument must be passed at least one value and that only the last argument can be declared as a ParamArray.

The For Each Statement

The For Each block repeats a group of statements for each element in an array or collection. This statement grants the ability to cycle through a collection or array without having to know about the type of elements or how many elements exist. This can be useful in cycling through large groups of information such as database tables and fields, or OLE object collections where you wish to apply a set of statements to all elements in a group.
  For Each CompanyProfile IN Company

  ' Iterate through the profiles in collection Company
  If CompanyProfile.Name = "Microsoft" then
    Exit For
    End If

The With Statement

The with statement allows you to reference an object once, and from then on reference only the properties of that object without the need to qualify what object each property relates to. This is very useful in minimizing code when referencing the same object many times.

This statement will also increase the use of longer and more meaningful object names, for the simple reason that these names will only have to be referenced once in order to utilize its properties.

Without using the With statement

  frmBusinessPrintPage.Windowstate = 2
  frmBusinessPrintPage.Caption = "Business 1"
  frmBusinessPrintPage.Enabled = True
  frmBusinessPrintPage.MinButton = False
  frmBusinessPrintPage.MaxButton = False
  frmBusinessPrintPage.Visible = True

Using the With Statement

  With frmBusinessPrintPage
  .Windowstate = 2		
  .Caption = "Business 1"
  .Enabled = True
  .MinButton = False
  .MaxButton = False
  .Visible = True
  End With
As you can see, the code using the With statement is much easier to read as well.

User Definable Collections

The idea of an ordered array of differing sizes is great, and also gives you ideas of dynamic arrays with lots of code work in VB3 using the redim and preserve statements. In VB4, the collections object has now been made available to the programmer, in effect giving you this functionality and more.
  Dim Company as Collection
The collection object is a dynamic structure to which you can add or remove differing types of data as elements. These elements can be referenced by an index number, which is recalculated whenever an item is added or removed from the list, or by an indexed key string value, which is set when an item is added to the collection.
  Company.Add Key := KeyValue, Item := CompanyValue
An important note about adding Keys to a collection is to always make sure that the key is unique. If it isn't unique then an error will occur.

For ordered sets of data the collection object also allows you to add items before or after items already in the collection. As collections are not able to be sorted automatically, these methods aid in this process. Here is a small code sample indicating how this could be done.

  ' Assume Company holds a sorted list of Values.
  For X = 1 to Company.Count
    If  Company.Item(X) > NewValue then

      ' Inserts the item before item X
      Company.Add Item = NewValue, Key = KeyValue, Before = X
    End If
In Summary
The new features VB4 inherits from the VBA 2.0 Engine shouldn't be ignored. Yes, we can live without them, at least for non-OLE Server development. But they add significant new functionality and, more importantly, productivity. Try them sometime.
Written by: Adam Maloney and Andrew Lockwood
Feb 1996