Applecore Pages on Microsoft Access

Getting Drive Names, Types and UNC Names

This page shows you how to return the letters for Drives that are attached to your computer, together with their type, and if they are a network drive, the UNC (Universal Naming Convention) Name.

Option Compare Database
Option Explicit
 
Private Declare Function apiGetLogicalDrives Lib "kernel32" Alias "GetLogicalDrives" () As Long
Private Declare Function apiGetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function apiWNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" _
    (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
 
Sub sListDrives()
'   Procedure to list all drives attached to a computer, as well as the type and if appropriate the UNC Name
    Dim lngReturn As Long
    Dim intLoop As Integer
    Dim strDrive As String
    lngReturn = apiGetLogicalDrives
    For intLoop = 0 To 25 ' Drive letters can only be A-Z
    If (lngReturn And (2 ^ intLoop)) <> 0 Then
        strDrive = Chr(65 + intLoop) & ":"
        Debug.Print strDrive & vbTab & fDriveType(strDrive & "\") & IIf(fDriveType(strDrive) = "Network Drive", "(" & fConvertNetworkDriveToUNC(strDrive) & ")", "")
    End If
    Next intLoop
End Sub
 
Function fDriveType(strDriveLetter As String) As String
'   Function to return the type of drive (ie Removable, Local, Network)
'   Accepts a drive letter in the format of "C:\"

    Dim lngReturn As Long
    lngReturn = apiGetDriveType(strDriveLetter)
    fDriveType = Switch(lngReturn = 0, "Unknown", lngReturn = 1, "No root", lngReturn = 2, "Removeable", _
        lngReturn = 3, "Local Hard Drive", lngReturn = 4, "Network Drive", _
        lngReturn = 5, "CD-ROM Drive", lngReturn = 6, "RAM Disk")
End Function
 
Function fConvertNetworkDriveToUNC(strDrive As String) As String
'   Function to return the UNC Name of a network drive
'   Accepts a drive letter in the format of "W:"
'   Note that unpredicatable results may happen if passed a drive letter for another type of drive - apparently a zero-length string

    Dim strReturn As String
    strReturn = Space(255)
    Dim lngReturn As Long
    lngReturn = apiWNetGetConnection(strDrive, strReturn, Len(strReturn))
    If lngReturn = 0 Then
        fConvertNetworkDriveToUNC = Left(strReturn, InStr(strReturn, vbNullChar) - 1)
    End If
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:13