Cover | Table of Contents | Colophon
For Each c In Worksheets("Sheet1").Range("C4:C17").Cells If c.Value = iCond Then tempTot = tempTot + c.Offset(0, 1).Value End If Next c
Set myRange = ActiveDocument.Range( _ Start:=ActiveDocument.Paragraphs(2).Range.Start, _ End:=ActiveDocument.Paragraphs(2).Range.End) myRange.Select myRange.Bold = True
Form_Form1.RecordSource =
"SELECT Products.ProductCode, " _
& " Products.BinLocation, Descriptions.Description" _
& " FROM Products INNER JOIN Descriptions " _
& " ON Products.ProductCode = Descriptions.ProductCode" _
& " WHERE (((Descriptions.Language)="
& iLangCode & "));"
Text0.ControlSource = "ProductCode"
Text2.ControlSource = "Descriptions.Description"
Text4.ControlSource = "BinLocation"
Unload Me
Sub Main()
Dim oForm as frmStartUp
Set oForm = New frmStartUp
oForm.Show vbModal
Set oForm = Nothing
End Sub
Me.Hide—or unloaded. Once this
happens, program execution is handed back to the Sub Main procedure,
which destroys the form object it created by setting the reference to
Nothing. When the End Sub
statement is executed, the whole application terminates.True or False. The keywords
True and False are constants that are predefined in VBA, so you can
make use of them in your code when you want to assign a value to a
Boolean variable, as the following code fragment shows:var1 = True var2 = False
True or False. In addition,
within programs, Boolean variables often serve as flags to control
program flow, as the following example, which toggles (or reverses)
the value of myBool within the
If...Else...End
If construct,
shows:If myBool = False Then myVar = 4 myBool = True Else myVar = 5 myBool = False End If
True or False. The keywords
True and False are constants that are predefined in VBA, so you can
make use of them in your code when you want to assign a value to a
Boolean variable, as the following code fragment shows:var1 = True var2 = False
True or False. In addition,
within programs, Boolean variables often serve as flags to control
program flow, as the following example, which toggles (or reverses)
the value of myBool within the
If...Else...End
If construct,
shows:If myBool = False Then myVar = 4 myBool = True Else myVar = 5 myBool = False End If
CBool(variablename)
False; otherwise, it returns
True (–1).myVar as a variant:Dim myVar
Private Function GoodStuff(vAnything, vSomething, _
vSomethingElse)
If vAnything > 1 And vSomething > "" Then
GoodStuff = vAnything * vSomethingElse
Else
GoodStuff = vAnything + 10
End If
End Function
Private Function GoodStuff(iAnything As Integer, _
sSomething As String, _
iSomethingElse As Integer) _
As Integer
If iAnything > 1 And sSomething > "" Then
GoodStuff = iAnything * iSomethingElse
Else
GoodStuff = iAnything + 10
End If
End Function
Dim,
Private, or Public statements.
Their syntax is:
Dim VariableName As datatype Private VariableName As datatype Public VariableName As datatype
Dim statement:Dim iRefNo As Integer, iAnyVar As Integer
Dim,
Private, and Public statements,
see their entries in . There is further
discussion later in this chapter about how the declaration of
variables affects their scope and lifetime.Option
Explicit
statement is good programming practice. It forces us to declare all
variables and constants. You can automatically have VB add this to
new modules as they are created by checking the Require Variable
Declaration option, which can be found on the Editor tab of the
Options dialog. (Select the Options option from the Tools menu to
open the dialog.)Dim statement like the following:Dim myArray(5) As Integer
myArray,
with six elements. Why six? All VBA arrays start with location 0, so
this Dim statement creates an array whose
locations range from myArray(0)to myArray(5).Private Type custRecord
custAccNo As Long
custName As String
RenewalDate As Date
End Type
Private custArray(10) As custRecord
Private Type custOrders
OrderNo As Long
OrderDate As Long
End Type
Private Type custRecord
custAccNo As Long
custName As String
RenewalDate As Date
orders(10) As custOrders
End Type
Private custArray(10) As custRecord
custOrders, is defined
to hold the OrderNo and OrderDate fields; then, within the custRecord
UDT, an array of type custOrders is defined.Private keyword can be accessed
by all the procedures within the module. Variables declared in the
declaration section of a code module using the
Public keyword can be accessed by the whole
project. Variables declared in the declaration section of a class
module using the Public keyword can be accessed by
the whole project once an object reference has been made to the
class. And variables declared using the Dim
statement within a subroutine or function can only be accessed in the
procedure in which they've been declared.For...Next loop). You can even use the same
variable names in a calling procedure and in a procedure that it
calls, and they will be treated as two separate variables.End
Sub or
End
Function statement is
executed. As soon as the procedure is complete, references to the
variables defined within that procedure are erased from the
computer's memory. This makes procedure-level variables ideal
for temporary, localized storage of information.Dim myObject As LibName.ClassName Dim myObject As New LibName.ClassName Dim myObject As Object
Private or
Public statement can replace the
Dim statement, and the same scope rules apply as
for other variables.myObject is set to
Nothing. To reference the class in this manner,
you must have used the References dialog to add a reference to the
class to your project. To assign a reference to a real instance of
the class, you must use the Set statement prior to
using the variable; for example:Set myObject = LibName.ClassName
ByRef or
ByVal for each of the variables in the argument
list.ByVal nor ByRef, VB
treats the variable as though it had been specified as
ByRef.ByRef means that the variable is passed
by reference. In other words, only a reference
to the original variable is passed to the called procedure. The
called procedure doesn't get its own copy of the variable; it
simply references the original variable. This is very similar in
concept to the pointers you find in C and C++. The result is that if
you make a change to the variable in the called procedure, that
change is reflected in the variable in the calling procedure, because
they are actually the same variable.Const keyword, VBA
includes a number of built-in or intrinsic constants whose values are
predefined by VBA. Along with saving you from having to define these
values as constants, the major advantage of using intrinsic constants
is that they enhance the readability of your code. So, for instance,
instead of having to write code like this:If myObject.ForeColor = &hFFFF Then
If myObject.ForeColor = vbYellow Then
Public Property Get Count() As Long
If mcolAnyColl.Count = 0 Then
Call PopulateCollection()
End If
Count = mcolAnyColl.Count
End Property
Public Const statement) within
a class module. How do you make some constants available both to
yourself and possibly to other users of your class? The answer lies
in the use of enumerated constants.vbCrLf and vbRightButton,
within VB itself. By using enumerated constants within your class,
you can associate a constant name and its value with the class, in
the process providing the user of the class with a set of meaningful
constants that are instantly available from the IntelliSense
drop-down list for statement completion.Enum
statement, which defines the name of
the set of constant values, the names of the individual constants
within the set, and the individual values of these constants. You
place the Enum statement in the declarations
section of your class module. For example:Public Enum empTypes empTypeOne = 1 empTypeTwo = 2 empTypeThree = 3 End Enum
empTypes is defined is a part of
your project, you have access to the enumerated constants via the
IntelliSense drop-down list. For example, you could access the
constants shown in the example above by typing
emp, then pressing the Ctrl key and spacebar
together; in the list of available items, you would see all three of
the constants and the New instance of an
object:Dim oVar As New svrObject
New. For example:Dim oVar As New svrObject ' Initialize event not called
oVar.AnyProp = sAnyVal ' Initialize event fired _
immediately prior to the Property Let