Access VBA Macro Programming

(Joao Candeias) #1
The next step is to put in code to call this function:
Sub Test_Api()
Dim x as Double
Dim FreeBytesAvailableToCaller As Currency, TotalNumberOfBytes As _
Currency, TotalNumberOfFreeBytes As Currency
x = GetDiskFreeSpaceEx("c:\", FreeBytesAvailableToCaller, _
TotalNumberOfBytes, TotalNumberOfFreeBytes)
MsgBox "Total Space " & Format(TotalNumberOfBytes * 10000, "#,##0")
MsgBox "Free Space " & Format(TotalNumberOfFreeBytes * 10000, "#,##0")
End Sub

This code sets up variables to hold the values returned by the API call. The actual value
returned through the variable calling it (in this case,x) is related to errors. It has a value of 1
for success.
The call is then made passing the root directory “C:\” and the variables already defined. It
is then a simple case of multiplying the value by 10,000 (this is because of the way in which
the API call returns the values) and formatting it to comma format for easy display. A message
box (as shown in Figure 20-1) then displays the result.

Chapter 20: API Calls 255


Figure 20-1 Getting the total space from a disk drive using the GetDiskFreeSpaceEx API call

Free download pdf