Access/VBA Library

MapShare

Maps a share to a drive and returns a Boolean indicating if the mapping was succesfull.

Syntax

MapShare( drive, share [, persistent] [, closeApplicationOnFail] [, userId] [, password] )
Parameters
Name Type Description
drive String Required. Drive to be used for mapping.
share String Required. Full path of the share to be mapped.
persistent Boolean Optional. Indicating if the mapping should be permanent or not.
Default: False
closeApplicationOnFail Boolean Optional. Indicating if the application should be close when the mapping fails.
Default: True
userId String Optional. UserId when the share is a protected.
Default: ""
password String Optional. Password of the UserId when the share is protected.
Default: ""
Return value

Boolean : True when the mapping was succesfull, otherwise False when closeApplicationOnFail is set to False.

Code
Public Function MapShare(drive As String, _
                         share As String, _
                         Optional ByVal persistent As Boolean = False, _
                         Optional ByVal closeApplicationOnFail As Boolean = True, _
                         Optional ByVal userId As String = "", _
                         Optional ByVal password As String = "") As Boolean

On Error GoTo MapShare_Error
    Dim objNetWork
        
    drive = GetDriveLetter(drive) & ":"
    
    MapShare = False
    
    Set objNetWork = CreateObject("WScript.Network")
    
    If userId = "" Then
        Call objNetWork.MapNetworkDrive(drive, share, persistent)
    Else
        Call objNetWork.MapNetworkDrive(drive, share, persistent, userId, password)
    End If
    
    MapShare = True

MapShare_Exit:
    On Error Resume Next
    Set objNetWork = Nothing
    Exit Function
    
MapShare_Close_Application:
    Call Application.Quit(acQuitSaveAll)
    
MapShare_Error:
    Call ShowError(functionName:=MapShare)
    
    If closeApplicationOnFail Then
        Resume MapShare_Close_Application
    Else
        Resume MapShare_Exit
    End If
End Function
Filesystemtag:FilesystemNetworktag:Network

See also

  • GetDriveLetter
  • ShowError