Using the Code Examples

This guide contains over 200 code examples that show how to correctly use the Microsoft Jet database engine. To help you learn about these features and incorporate them into your applications, the companion CD-ROM contains an electronic version of each code example used in the printed guide.

The code examples are stored in the JetBook\Samples subfolder on the companion CD-ROM. All of the code examples for Microsoft Access 97 are stored as modules in a Microsoft Access database named JetSamples.mdb. All of the code examples for Visual Basic 5.0 and other applications that support VBA 5.0 are stored as modules (.bas files) that are referenced in a Visual Basic project named JetSamples.vbp. Both JetSamples.mdb and JetSamples.vbp use tables and queries in NorthwindTables.mdb, also located in the JetBook\Samples subfolder. Additionally, some of the code examples for Chapter 8, “Accessing External Data,” use sample data files in other formats, such as the Paradox sample data file, Author.db. These sample data files are located in subfolders in the JetBook\Samples folder and must be present in order for these code examples to work properly.

All of the code examples are designed to be executed from your hard disk. Use the Windows Explorer to copy the complete directory structure to the root directory of your C drive. Because files stored on a CD-ROM must have their read-only file attribute set, you may need to clear the read-only file attribute after copying the files. To make this process easier, you can run the function named RemoveReadOnlyAttr from the FileAttributes module in JetSamples.mdb, or from FileAttributes.bas. For more information about specific file names or additional requirements for the code examples in each chapter, see the introduction in that chapter.

To use the Visual Basic code modules referenced in JetSamples.vbp in Visual Basic 5.0, you can open JetSamples.vbp directly. To use these modules from a VBA 5.0 host application other than Microsoft Access 97, such as Microsoft Word 97, you must import the module(s) into the Visual Basic Editor. Additionally, you must set a reference to the Microsoft DAO 3.5 object library (see below).

Important Don’t copy and paste code examples from JetSamples.mdb into Visual Basic 5.0 or other VBA 5.0 host applications. Many code examples in JetSamples.mdb include code that uses Microsoft Access-specific Visual Basic keywords and techniques that will fail if run from other applications. The modules referenced in JetSamples.vbp are designed to run in Visual Basic 5.0 and other VBA 5.0 host applications.

Û To import a .bas file into the Visual Basic Editor

  1. Start the VBA 5.0 host application and open the document you want to work with.

  2. Open the Visual Basic Editor. To do this in Microsoft Excel, Microsoft PowerPoint, or Microsoft Word, point to Macro on the Tools menu, and then click Visual Basic Editor. A different procedure may be required in other VBA 5.0 host applications. Check your product documentation for details.

  3. On the File menu, click Import File.

  4. In the Import File dialog box, navigate to the folder where you installed the code samples, select the module (.bas) you want to work with, and then click Open.

  5. To view the code in the module, expand the Modules folder in the Project Explorer, and then double-click the module you imported.

Û To set a reference to the Microsoft DAO 3.5 Object Library

  1. Start the VBA 5.0 host application, open the document you want to work with, and then open the Visual Basic Editor.

  2. On the Tools menu, click References.

  3. In the Available References box, select Microsoft DAO 3.5 Object Library, and then click OK.

Note The reference for the Microsoft DAO 3.5 object library is available only if you selected the Data Access Objects For Visual Basic option under the Data Access option when you ran the Setup program for Microsoft Access 97, Microsoft Excel 97, Microsoft Office 97, or Microsoft Office 97 Professional Edition. If the object library is not available, re-run the Setup program and select this option. The Data Access Objects For Visual Basic option is not available in the Setup program for the stand-alone versions of Microsoft PowerPoint 97 or Microsoft Word 97.