FIGURE 9.12
An email message created from code.
ADO ..............................................................................................................
The ActiveX Data Objects (ADO) model isn’t the best method for working with Access data, but it
does offer some extra features that are very handy for working with text files, in particular the
Streamobject. The ADO case in the cmdCreateEMails_Clickevent procedure starts by set-
ting a variable to a Streamobject in the ADO object model (note that the object model prefix is
ADODB), using the Newkeyword. The next line opens the new Streamobject (you can’t write to
it unless it is open). A string of introductory text to be written to the file is saved to the strText
variable and then written to a line in the stream using the WriteTextmethod. To create two
blank lines in the text file, WriteTextis then used with two vbCrLfconstants (representing a
CR + LF, meaning carriage return plus linefeed — antique terminology dating back to the days of
manual typewriters).
You need a reference to the ActiveX Data Objects object library to support this code.
The code iterates through the ItemsSelected collection of the lstSelectContacts listbox, checking
each record for required fields (Email and CompanyName). For each record that lacks data in one
or both of the required fields (and thus won’t get an email message), an If... Thenstatement
writes a line containing information on which record has been skipped, and why, plus another
blank line. At the end of the Case, a final line is written, and the stream is saved to a text file using
the SaveToFilemethod with the adSaveCreateNotExistoption, which creates the file if it
does not exist.
NOTENOTE
Working with Files and Folders 9