Access Cookbook - Ken Getz [243]
Dim intCount As Integer
Dim intI As Integer
Dim varTemp As Variant
Dim lngType As Long
Const conMaxSpace = 1024
strBuffer = Space(conMaxSpace)
intCount = GetLogicalDriveStrings(conMaxSpace - 1, strBuffer)
strBuffer = Left(strBuffer, intCount)
intI = 1
intCount = 0
Do
varTemp = acbGetToken(strBuffer, vbNullChar, intI)
If Len(varTemp & "") > 0 Then
' The next statement will be true except in the
' case where the drive < C and you DON'T want
' to include floppies. Then it'll skip the drive.
If (UCase(Left(varTemp, 1) < "C")) Imp fIncludeFloppies Then
intCount = intCount + 1
' Get the drive name.
astrDrives(intCount).strDrive = varTemp
' Get the drive type, and set the flags accordingly.
lngType = GetDriveType(varTemp)
Select Case lngType
Case DRIVE_REMOVABLE
astrDrives(intCount).fRemovable = True
Case DRIVE_FIXED
astrDrives(intCount).fFixed = True
Case DRIVE_REMOTE
astrDrives(intCount).fRemote = True
Case DRIVE_CDROM
astrDrives(intCount).fCDROM = True
Case DRIVE_RAMDISK
astrDrives(intCount).fRamDisk = True
End Select
' Get the drive space information.
astrDrives(intCount).varTotalSpace = acbGetTotalSpace(varTemp)
astrDrives(intCount).varFreeSpace = acbGetFreeSpace(varTemp)
End If
intI = intI + 1
End If
Loop Until Len(varTemp & "") = 0
acbGetDrives = intCount
End Function
The acbGetTotalSpace and acbGetFreeSpace functions both call the private GetDiskSpace function, which in turn calls the GetDiskFreeSpace API function. GetDiskSpace takes the four pieces of information returned from GetDiskFreeSpace —sectors per cluster, bytes per sector, free clusters, and total clusters—and returns the calculated value that you've requested:
Private Function GetDiskSpace(ByVal strDrive As String, _
fTotal As Boolean) As Variant
' Input:
' strDrive: String representing drive letter
' fTotal: True for total space on drive, False for free space on drive
' Output:
' Free or Total space, if no error. Null, otherwise.
Dim lngSectorsPerCluster As Long
Dim lngBytesPerSector As Long
Dim lngFreeClusters As Long
Dim lngTotalClusters As Long
' Force the string into the correct format.
strDrive = Left(strDrive, 1) & ":\"
If GetDiskFreeSpace(strDrive, lngSectorsPerCluster, lngBytesPerSector, _
lngFreeClusters, lngTotalClusters) Then
GetDiskSpace = lngSectorsPerCluster * lngBytesPerSector * IIf(fTotal, _
lngTotalClusters, lngFreeClusters)
Else
GetDiskSpace = Null
End If
End Function
If you want to dig a bit further, investigate the GetVolumeInformation API function. This function retrieves even more information about the specified drive, including its volume name, serial number, whether or not compression is enabled, the filesystem type (FAT, HPFS, NTFS), and other information about how data is stored on that drive. This information is of less importance to Access developers than to system application developers, so we don't discuss it here.
11.13. Collect and Display Information on the System and the Access Installation
Problem
Your application really needs to know some information about the computer on which it's running. In addition, you'd like to add some professional polish and an About... box that shows information about the computer, the resources, and the user. Access doesn't provide any way to find this information. How can you gather it?
Solution
You can use the Windows API to retrieve information about the system on which your program is running. By using these various functions as the control sources for unbound controls, you can present a selection of system information to your user.
Load 11-13.MDB and open frmSystemInfo in regular form view (see Figure 11-15). This form includes five "pages" of information about the current computer and its resources. If you like the look of this form, use it as-is in your own applications. (You'll need to import the form, frmSystemInfo, its subform, fsubInfo, and the module, basSystemInfo, into your application, as directed in Step 1.)
Figure 11-15. frmSystemInfo shows memory status information