Share this Page URL

Chapter 5. Using Visual Basic to Automat... > Using Standard and Class Modules - Pg. 145

Using Visual Basic to Automate Your Database A. Use the ampersand (&) to concatenate the elements of the message. Q. What's the difference between a function and a Sub procedure? A. A function returns a value. 145 Working with Strings and Long Lines of Code As mentioned, you can use the line continuation character (the underscore, _) to break a lengthy line of code onto separate lines so that your code is easier to read. The line continuation character must be preceded by a space, and you can't include any characters after the line continuation character, such as a space or a comment. You can use the line continuation character anywhere but in a literal text string. If you need to join two or more literal text strings that break across lines, you need both the line continuation character and the ampersand to form one string, as shown here: strLongSaying = "These are the times that try men's souls." & _ "The summer soldier and the sunshine patriot will, in this crisis," & _ "shrink from the service of their country; but he that stands" & _ "by it now deserves the love and thanks of man and woman." Using Standard and Class Modules The code that's part of an Access database is stored in modules. A standard module is a type of database object, and each standard module you create is listed in the Database window. Each form and report you add code to has its own module, known as a class module . (Class modules for forms are sometimes referred to as code-behind forms. ) In general, you use a standard module for code that you'll need throughout a database. A form or report's class module includes procedures tied to events for that object (opening or closing a form, for example) or a control such as a command button, and code for a function or a constant that's used in an event procedure. (You can also create a class module for an object that you define, but we won't be covering modules of that type.) Both standard and class modules can also contain code statements in the module's Declarations section. These statements affect all the procedures in a module. Should I use a form, report module, or a standard module? When you're deciding whether to put code in a standard module or add it to a form or report module, keep the following points in mind. You can refer to and run the code you put in a standard module from anywhere in your database. You can even run the code in a standard module from another database. To run the code in a report or a form, the report or form needs to be open. In general, use standard modules for code that performs tasks that you'll use from more than one place in your database. Code that performs an operation specifically associated with a form or report should be included with that object. Also, the code in a form or report module is included if you import the object or export the object to another database. In the following set of steps, we'll create a standard module in which we'll define a constant you can use throughout the code in the HelloWorld database. A constant is an element of a program whose value doesn't change. Any time you need to refer to the value defined by a constant--in the up- coming example, the constant will store the default path to the book's sample files--you can use the constant rather than the specific value. If you have to change the value of a constant, you need to change it only in the module where you declared the constant. We'll also take a closer look at the makeup of a standard module as we work through this procedure. (You'll see more examples of class modules in this and later chapters.) Use a standard module to store a constant 1. 2. Open the file HelloWorld5.mdb in the Chap05 folder (if the database isn't already open). In the Database window, click Modules in the Objects list, and then click New.