The VBA 2.0 Engine
by Adam Maloney and by Andrew Lockwood - GUI
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 !
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 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
Function FunctX(Arg1 As String, Arg2 as Integer) as Integer ... End FunctionThe 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.
For Each CompanyProfile IN Company ' Iterate through the profiles in collection Company If CompanyProfile.Name = "Microsoft" then Exit For Else ... End If Next
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 WithAs you can see, the code using the With statement is much easier to read as well.
Dim Company as CollectionThe 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 := CompanyValueAn 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 NextIn Summary