Listing 9-9
(continued)
‘ Move to the next database record.
CurrentData.MoveNext
Wend
‘ Create an output string by getting the values from
‘ the collection.
For Counter = 1 To ContactList.Count
‘ Get the current element.
Set Individual = ContactList(Counter)
‘ Access the collection elements by name.
Output = Output + Individual.Name + _
vbTab + Individual.Telephone + _
vbTab + _
CStr(Individual.LastContact) + vbCrLf
Next
The code is easier to understand now because you can quickly identify the
data elements, and each collection entry is equal to a single database record.
Notice that you create a single
Person
object (
Individual
) to store the
information.
A single call to the
Persons
collection object,
ContactList
,
adds all the information at once.
Creating the output is also easier. A single call to
ContactList
places the
information in
Individual
. The
Output
string uses information from this
single object to create an entry for the message box. Notice that you don’t
need any strange math for the expression in the
For...Next
loop.
228
Part III: Expanding Your VBA Horizons
15_046500 ch09.qxp 12/5/06 5:36 PM Page 228
Chapter 10
Working with Disk Files
In This Chapter
Working with data storage
by using various techniques
Interacting with the data found in data storage files
Using data storage to perform tasks such as saving program settings
Y
ou use disk storage every time that you work with an application. Disk
storage is the most convenient and least expensive means of perma-
nently saving data on a computer system. Permanent storage means that the
information on the disk drive remains there after you turn off the computer.
Modern drives include everything from internal hard drives to flash disk
thumb drives that you place in USB ports. In general,
VBA disk storage is lim-
ited to devices you can access as standard hard drives unless you’re willing
to turn to exotic programming techniques not discussed in this book. Standard
hard drive access includes flash disks and rewriteable CD-RW/DVD-RW drives,
but doesn’t include media you must burn. You may also find it difficult to use
online (Internet) storage that requires special processing. Windows Vista
promises to make many forms of media that are hard to access under Windows
XP and earlier operating systems accessible using the same techniques you
use
with an internal hard drive, so you may have other options when working
with Vista. Consequently, you need not limit VBA to an internal drive; you can
easily place your data on a removable drive and carry it with you.
Disk storage can hold several kinds of information.
The program that you use
to write VBA code is an executable file — it contains information but not user
data. The documents that you save are user data. The program that you use
to work with VBA already supports the documents that you want to use.
However, you might want to save information in other formats, which is one
reason to write a VBA program that works with disk storage.
A third kind of disk storage is settings. Some applications use initialization
(INI) files exclusively. In fact, Microsoft has begun recommending using some-
thing other than the Registry or Active Directory to store your settings to
16_046500 ch10.qxp 12/5/06 5:36 PM Page 229
make applications easier to move from one machine to another. Using INI or
eXtensible Markup Language (XML) files works perfectly for this purpose.
(Chapter 11 discusses using XML with VBA in detail.)
Most new applications
rely heavily on the Registry for setting storage options, and some applica-
tions use server storage in the form of Active Directory. All three forms of set-
tings storage are viable, but the INI or XML file is the easiest method to use
from VBA.
This chapter demonstrates various methods for working with disk files. You
might find that you want to store data in a new format or save program set-
tings after you see how easy it is to work with disk files.
Using
Disk Storage
Placing information on disk is an essential task for most applications. The
data that an application creates is the main reason why you have an interest
in it. However, the application already handles data storage for you, so you
don’t need to consider this use for disk files in your program. You might think
that that’s the extent of the data storage needs for your program, but it isn’t.
Your program also has valuable data that it must maintain.
Application configuration information
When a user
interacts with your program, you might want to retain any set-
tings that the user makes. It’s always a good idea to store the user interface
settings so that they’re restored every time the user starts the program.
You can also store usage information. For example, when you open a Word
document, you can press Shift+F5 to return to your previous editing location
in the document. Microsoft stores that information between writing sessions
so that you can resume your work quickly.
The easiest place to store these settings is on disk in an INI file. However, you
can
also use a plain-text file, if you want. You might find the text file easier to
understand and use than an INI file. It’s important to choose a convenient
location for the information. Here are places that you should consider:
Do'stlaringiz bilan baham: