Applecore Pages on Microsoft Access

Determining the size of the Hard Drive from Access

If you wish to check the size of the user's hard drive from within Access, for example before creating a large file, then you will need to use a call to an API, GetDiskFreeSpaceExA, to do this. Previously, you would use the GetDiskFreeSpace API, but now that Hard Drives are getting larger, it cannot cope with ones larger than 2Gb. Below are sample functions using this code that will return the number of bytes free on the drive, the toal number of bytes on the drive, and also how many bytes have been used. Just copy the code below into a module:

Option Compare Database
Option Explicit
 
Private Declare Function apiGetDiskFreeSpaceEx Lib "kernel32" _
    Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, _
    lpcurFreecurUserBytes As Currency, _
    lpTotalNumberOfBytes As Currency, _
    lpTotalNumberOfcurFreeBytes As Currency) As Long
 
Function fHDDSpaceTotal(strDriveName As String) As Double
'   Function to return the total number of bytes on the specified drive
'   Accepts - strDriveName - the name of a drive on the computer, such as "C:\"
'   Returns - the total number of bytes if successful, otherwise 0

    Dim lngReturn As Long
    Dim curTotalBytes As Currency
    Dim curFreeBytes As Currency
    Dim curUserBytes As Currency
    If Right(strDriveName, 1) <> "\" Then strDriveName = strDriveName & "\"
    lngReturn = apiGetDiskFreeSpaceEx(strDriveName, curUserBytes, curTotalBytes, curFreeBytes)
    If lngReturn <> 0 Then fHDDSpaceTotal = curTotalBytes * 10000
End Function
 
Function fHDDSpaceFree(strDriveName As String) As Double
'   Function to return the number of free bytes on the specified drive
'   Accepts - strDriveName - the name of a drive on the computer, such as "C:\"
'   Returns - the number of free bytes if successful, otherwise 0

    Dim lngReturn As Long
    Dim curTotalBytes As Currency
    Dim curFreeBytes As Currency
    Dim curUserBytes As Currency
    If Right(strDriveName, 1) <> "\" Then strDriveName = strDriveName & "\"
    lngReturn = apiGetDiskFreeSpaceEx(strDriveName, curUserBytes, curTotalBytes, curFreeBytes)
    If lngReturn <> 0 Then fHDDSpaceFree = curFreeBytes * 10000
End Function
 
Function fHDDSpaceUsed(strDriveName As String) As Double
'   Function to return the number of bytes used on the specified drive
'   Accepts - strDriveName - the name of a drive on the computer, such as "C:\"
'   Returns - the number of bytes used if successful, otherwise 0

    Dim lngReturn As Long
    Dim curTotalBytes As Currency
    Dim curFreeBytes As Currency
    Dim curUserBytes As Currency
    If Right(strDriveName, 1) <> "\" Then strDriveName = strDriveName & "\"
    lngReturn = apiGetDiskFreeSpaceEx(strDriveName, curUserBytes, curTotalBytes, curFreeBytes)
    If lngReturn <> 0 Then fHDDSpaceUsed = (curTotalBytes - curFreeBytes) * 10000
End Function

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 14:53:14