VB & VBA in a Nutshell: The Language: The Language

VB & VBA in a Nutshell: The Language: The Language

by Paul Lomax
VB & VBA in a Nutshell: The Language: The Language

VB & VBA in a Nutshell: The Language: The Language

by Paul Lomax

Paperback

$39.99 
  • SHIP THIS ITEM
    Qualifies for Free Shipping
    Choose Expedited Shipping at checkout for delivery by Thursday, April 4
  • PICK UP IN STORE
    Check Availability at Nearby Stores

Related collections and offers


Overview

The online documentation of VB/VBA language components seems to follow the 80/20 rule: the basic facts that you need to use a language statement are provided in the documentation. But the additional 20 percent that you need to use it effectively or to apply it to special cases is conspicuously absent. To a professional VB/VBA programmer, though, this missing 20 percent of the language's documentation isn't a luxury, it's a necessity. And in VB & VBA in a Nutshell: The Language, it finally is available. The bulk of the book consists of an alphabetical reference to the statements, procedures, and functions of the VB/VBA language. Each entry has a standardized listing containing the following information:
  • Its syntax, using standard code conventions
  • Differences in the operation of the keyword in a macro environment (e.g., in Office) and in Visual Basic, if there are any
  • A list of arguments accepted by the function or procedure, if any
  • A description of the data type returned by a function
  • The finer points of a keyword's usage that are often omitted from or blurred over by the documentation
  • Tips and gotchas that include undocumented behaviors and practical applications for particular language elements — a section particularly invaluable for diagnosing or avoiding potential programming problems
  • A brief, nonobvious example that illustrates the use of the keyword
Also included in VB & VBA in a Nutshell: The Language is a brief overview of the VB/VBA language, including:
  • Basic VBA programming concepts, such as its data types and its support for variables, constants, and arrays
  • Error handling in VBA applications
  • Object programming with VBA
  • Using VBA with particular applications. Excel and Project are utilized to show how to work with an application's integrated development environment and to take advantage of its object model Regardless of how much experience you have programming with VBA, this is the book you'll pick up time and time again both as your standard reference guide and as a tool for troubleshooting and identifying programming problems.
VB & VBA in a Nutshell: The Language is the definitive reference for Visual Basic and VBA developers.

Product Details

ISBN-13: 9781565923584
Publisher: O'Reilly Media, Incorporated
Publication date: 10/28/1998
Series: In a Nutshell (O'Reilly)
Pages: 650
Product dimensions: 6.00(w) x 9.00(h) x 1.50(d)

About the Author

Paul Lomax, author of O'Reilly's VB & VBA in a Nutshell and a coauthor of VBScript in a Nutshell, is an experienced VB programmer with a passion for sharing his knowledge—and his collection of programming tips and techniques gathered from real-world experience.

Read an Excerpt


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, Language Elements by Category, 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 standalone applications as either FXFs, 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 I 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:

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(lngVarl)

the addition operator (+) combines 16 and the value returned by int (lngVarl) 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.

#Const Directive
Named Arguments
No
Syntax

#Const constantname - expression
constantname

Name of the constant.

expression

Data Type: Literal

Any combination of literal values, other conditional compilation constants defined with the #Const directive, and arithmetic or logical operators except Is.

Description

Defines a conditional compiler constant. By using compiler constants to create code blocks that are included in the compiled application only when a particular condition is met, you can create more than one version of the application using the same source code. This is a two-step process:

  • Defining the conditional compiler constant. This step is optional; conditional compiler constants that aren't explicitly defined by the #Const directive but that are referenced in code default to a value of 0 or False.
  • Evaluating the constant in the conditional compiler #If ... Then statement block.
A conditional compiler constant can be assigned any string, numeric, or logical value returned by an expression. However, the expression itself can consist only of literals, operators other than Is, and another conditional compiler constant.

When the constant is evaluated, the code within the conditional compiler #If ... Then block is compiled as part of the application only when the conditional compiler constant evaluates to True.

You may wonder why you should bother having code that is compiled only when a certain condition is met, when a simple If ... Then statement could do the same job. The reasons are:

You may have code that contains early bound references to objects that are present only in a particular version of the application. You'd want the code compiled only when you know it would create an error. You may wish to include code that executes only during the debugging phase of the application. it's often wise to leave this code in the application even after the application has been released, so that you can check back over a procedure if an issue arises. However, you don't want the code to be executed in the final application. The answer is to wrap your debugging code in a conditional statement. You can then provide a conditional constant that acts as a switch to turn debugging code on or off, as the example below demonstrates.

Although most operations performed with conditional compilation can be replicated with normal if ... Then code blocks, conditional compilation reduces the size of the compiled application and thereby the amount of memory required for the application, making for a more efficient application.

Rules at a Glance

  • Conditional compiler constants are evaluated by the conditional compiler #If ... Then statement block.
  • You can use any arithmetic or logical operator in the expression except Is.
You can't use other constants defined with the standard Const statement in the expression.

According to the documentation, you can't use intrinsic functions in the expression; e.g., #Const MY_CONST = Chr (13) is illegal. In most cases, VBA displays a "Compile error : Variable not found" message if you try this. But there are numerous exceptions. For example, the use of the int function in the following code fragment doesn't produce a compiler error, and in fact, successfully defines a constant ccDef Int whose value is 3:

#Const ccDefFloat = 3.1417 #Const ccDefInt = int(ccDeFloat)

When using #Const, you can't use variables to assign the conditional constant a value.

Constants defined with #Const can be used only in conditional code blocks.

Constants defined with #Const have scope only within the module in which they are defined; i.e., they are private.

You can place the #Const directive anywhere within a module.

You can't use the #Const directive to define the same constant more than once within a module. Attempting to do so produces a "Compile Error: Duplicate Definition" error message.

Interestingly, you can define the same constant both through the VB or VBA interface (see the second item in the "Programming Tips & Gotchas" section) and using the #Const directive. In this case, the constant defined through the interface is visible throughout the application, except in the routine in which the #Const directive is used, where the private constant is visible.

The #Const directive must be the first statement on a line of code. it can be followed only by a comment. Note that the colon, which combines two complete sets of statements onto a single line, can't be used on lines that contain #Const. . . .

Table of Contents

Preface; Why Another VB Book?; What's Wrong with the Online Help?; Who Should Read This Book?; How This Book Should Be Used; How This Book Is Structured; Conventions Used in This Book; How to Contact Us; Acknowledgments; Part II: The Basics; Chapter 1: Introduction; 1.1 What Is VBA?; 1.2 A Brief History of VBA; 1.3 What Can You Do with VBA?; 1.4 Object Models: The Power of Programming with VBA; Chapter 2: Program Structure; 2.1 Getting a VB Program to Run; 2.2 The Structure of a VB Program; 2.3 Ending Your VB Program; Chapter 3: VBA Variables and Data Types; 3.1 Visual Basic Data Types; 3.2 Type Conversion; 3.3 The Variant; 3.4 Declaring Variables and Constants; 3.5 Array Variables; 3.6 User -Defined Types; 3.7 Variable Scope and Lifetime; 3.8 Object Variables and Binding; 3.9 Passing Parameters; 3.10 Intrinsic Constants; Chapter 4: Class Modules; 4.1 Properties; 4.2 Enumerated Constants; 4.3 Class Module Events; 4.4 Implementing Custom Class Methods; 4.5 Creating ActiveX Components; 4.6 Using ActiveX Components in a Project; Chapter 5: Automation; 5.1 Creating Object Model References; 5.2 Reading the Object Model; 5.3 Working with the Object Model; 5.4 Collection Objects; 5.5 Trapping an Automation Server's Events; 5.6 Automation Examples; 5.7 Automation Performance Tips; Chapter 6: Error Handling; 6.1 Building a Robust Application; 6.2 Error Handling in Procedures; 6.3 Error Handling in ActiveX Servers; 6.4 Reporting Errors; Part III: Reference; Chapter 7: The Language Reference; Chapter 8: The Language Reference, Part 1: #Const Directive–Drives Collection Object (VB6); Chapter 9: The Language Reference, Part 2: End... Statement–FV Function; Chapter 10: The Language Reference, Part 3: Get Statement–PV Function; Chapter 11: The Language Reference, Part 4: QBColor Function–Year Function; Part IV: Appendixes; Appendix A: Language Elements by Category; A.1 Collection Object; A.2 Conditional Compilation; A.3 Data Type Conversion Functions; A.4 Data Bindings Objects (VB6 only); A.5 Date and Time Functions; A.6 Dictionary Object (VB6 Only); A.7 Err Object; A.8 FileSystem Objects (VB6 only); A.9 File and Folder Handling; A.10 Financial Functions; A.11 Formatting Functions; A.12 Information Functions; A.13 Interaction Functions; A.14 Math Functions; A.15 Programming and ActiveX Programming; A.16 Printer ObjectNot available in hosted versions of VBA; A.17 Program Structure and Flow; A.18 Registry Functions; A.19 String Manipulation; A.20 Variable Declaration; Appendix B: Language Constants; B.1 Application Window Style (VbAppWinStyle); B.2 Calendar (VBCalendar); B.3 Call Type (VB6 only); B.4 Compare Method (VbCompareMethod); B.5 Data Binding Update Mode (VB6 Only); B.6 Date Time Format Styles (VB6 Only); B.7 Day of Week (VbDayOfWeek); B.8 Drive Type (VB6 only); B.9 File Attributes (VbFileAttribute); B.10 File System Objects File Attributes (VB6 Only); B.11 First Week of Year (VbFirstWeekOfYear); B.12 IME Status (VbIMEStatus); B.13 Language Constants; B.14 Log Event Type Style (LogEventTypeConstants); B.15 Log Mode Constants (VB6 only); B.16 Message Box Result (VbMsgBoxResult); B.17 Message Box Style (VbMsgBoxStyle); B.18 Special Folder Types (VB6 only); B.19 String Conversion (VbStrConv); B.20 TextStream IOMode (VB6 only); B.21 Tristate Constants (VB6 Only); B.22 Variant Type (VbVarType); Appendix C: Operators; C.1 Arithmetic Operators; C.2 String Operator; C.3 Comparison Operators; C.4 Logical and Bitwise Operators; C.5 Operator Precedence; Appendix D: What's New in VB6?; D.1 Create DataSource and Data Consumer Classes; D.2 Functions Can Return Arrays; D.3 Dynamic Arrays Can Be Assigned; D.4 Remote User-Defined Types; D.5 New Functions; D.6 Enhanced CreateObject Function; D.7 New Object Models; Colophon;
From the B&N Reads Blog

Customer Reviews