Access.2007.VBA.Bibl..

(John Hannent) #1
CopyFilemethod of the FileSystemObjectis used to copy the current database to a backup
with the final save name in the Backups folder.

The tblBackupInfo table stores dates and incrementing numbers for the backup names. The back-
ups you make on a given day will have a number (starting with 1) and the date, so they don’t over-
write each other, and you will know the order in which the backups were created. Figure 9.9
shows the InputBox presented by the BackupDBfunction; you can accept the proposed save name
or edit it as desired.

FIGURE 9.9
Saving a database copy using the BackupDB function.

Working with Text Files ....................................................................................................


For purposes of working with text files in VBA code, there are three types of text files: comma-
delimited, fixed-width (columnar), or free-form. Data from the first two types of text files can be
imported or exported using the TransferTextmethod in Access, and comma-delimited files can
be processed with the TransferSpreadsheetmethod. If you just need to read data from (or
write data to) a text file, but not import into a table, you can work with text files using the
FileSystemObject, the legacy VB methods, or ADO.

Exporting to (and importing from) comma-delimited and fixed-width text files is covered
in Chapter 10.

Writing Data to Text Files ........................................................................................


If your code iterates through a recordset, doing (or not doing) some action for each record, a text
file is a handy way to document which records have been processed, or perhaps just to document
records that were skipped because of missing information. You can write informational data to a
text file using three methods: the legacy VB statements (Open FileNameFor Input/Output As #n);
components of the FileSystemObjectobject model (the TextStreamobject in particular); or
components of the ADO object model (the Streamobject in particular).

The sample Select Contacts for Email form (frmEMailMerge), shown in Figure 9.10, has a multi-
select listbox for selecting contacts to receive an email, textboxes for entering the message subject
and body, and an option group for selecting the method of creating a text file containing informa-
tion about the skipped records.

CROSS-REFCROSS-REF


Part II Writing VBA Code to Exchange Data between Office Components

Free download pdf