Access Cookbook - Ken Getz [242]
Solution
The Windows API provides three functions that you can use to extract information about the drives in your computer: GetLogicalDriveStrings, which returns a string containing a list of all the logical drives; GetDriveType, which returns information about the specified drive; and GetDiskFreeSpace, which returns information about the total and free disk space for a specified drive.
Load and run frmDiskSpace from 11-12.MDB. This form, shown in Figure 11-14, contains a list box with information about all the logical drives in your system. To fill the list box, the example code walks through all the drives returned from a call to GetLogicalDriveStrings, calling the other two functions for each drive.
Figure 11-14. frmDiskSpace shows information about all the installed drives
To use these functions in your own applications, follow these steps:
Import the modules basDiskInfo and basToken from 11-12.MDB.
To call the functions, use the information in Table 11-12. Each function takes only a single parameter, the drive to be interrogated.
Table 11-12. The functions in basDiskInfo
Function
Purpose
Return value
Example
acbGetFreeSpace
Retrieve the amount of free space on the specified drive.
Variant (the amount of free disk space, in bytes), or Null if the function failed
lngFree = acbGetFreeSpace("C")
acbGetTotalSpace
Retrieve the total amount of space on the specified drive.
Variant (the amount of total disk space, in bytes), or Null if the function failed
lngTotal = acbGetTotalSpace("C")
acbIsDriveCDROM
Verify that a drive is a CD-ROM.
True if CD-ROM, False otherwise
fCD = acbIsDriveCDROM("D")
acbIsDriveFixed
Verify that a drive is a hard disk.
True if a hard disk, False otherwise
fFixed = acbIsDriveFixed("C")
acbIsDriveLocal
Verify that the specified drive is local.
True if local, False if remote
fLocal = acbIsDriveLocal("C")
acbIsDriveRAMDisk
Verify that a drive is a RAM disk.
True if RAM disk, False otherwise
fRAM = acbIsDriveRAMDisk("F")
acbIsDriveRemote
Verify that the specified drive is a network drive.
True if remote, False if local
fNetwork = acbIsDriveRemote("E")
acbIsDriveRemovable
Verify that the specified drive is for removable media.
True if removable, False otherwise
fRemovable = acbIsDriveRemovable("A")
Discussion
The sample form doesn't actually use any of the acbIs functions listed in Table 11-12; these functions are supplied only for your own applications. Instead, it calls the acbGetDrives function in basDiskInfo, which fills an array of acb_tagDriveInfo structures directly with information about each of the installed drives, physical or logical.
The structure looks like this:
Type acb_tagDriveInfo
strDrive As String
varFreeSpace As Variant
varTotalSpace As Variant
fRemovable As Boolean
fFixed As Boolean
fRemote As Boolean
fCDROM As Boolean
fRamDisk As Boolean
End Type
It stores all the information that the sample form displays. The sample form then uses a list-filling callback function to display the information in a list box. (For more information on list-filling callback functions, see Chapter 7.)
The acbGetDrives function starts out by calling the Windows API function GetLogicalDriveStrings. This function returns a string containing all the logical drives on your machine, in this format:
C:0D:0G:0H:0
where the 0s indicate null characters, Chr$(0). (VBA provides the vbNullChar constant that's equivalent to Chr$(0).) The acbGetDrives function loops through this string, using the acbGetToken function in basTokens to pull out the drive names, one at a time, and then gathering information about each. The source code for acbGetDrives is:
Public Function acbGetDrives(astrDrives( ) As acb_tagDriveInfo, _
fIncludeFloppies As Boolean)
' Fill astrDrives( ) with all the available logical drive letters.
Dim strBuffer