Access/VBA Library

GetFreeDrive

Returns a String containing the first free drive letter.

Syntax

GetFreeDrive( [startFromDrive] )
Parameters
Name Type Description
startFromDrive String Optional. The first drive letter to search from.
Default: "A"
Return value

String : The first unused drive letter.

Code
Public Function GetFreeDrive(Optional ByVal startFromDrive As String = "A") As String
    Dim fs As New FileSystemObject
    Dim drive As String
    
    drive = GetDriveLetter(startFromDrive)
    If drive < "A" Or drive > "Z" Then drive = "A"
        
    GetFreeDrive = ""
    
    While fs.DriveExists(drive)
        drive = Chr(Asc(drive) + 1)
        If drive = "Z" Then
            Call Err.Raise(vbObjectError + 4000, , "No free network drive found!")
        End If
    Wend
            
    GetFreeDrive = drive & ":"
End Function
Filesystemtag:FilesystemNetworktag:Network

Remarks

  • An error is raised when no free drive is found.

See also

  • GetDriveLetter