ACC2000: How to Return a Universal Naming Convention (UNC) Path from an Existing Drive Letter (210050)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210050
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article shows you how to return the Universal Naming Convention (UNC) path for a mapped drive letter.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access.

MORE INFORMATION

To find the UNC path associated with a mapped network drive letter, follow these steps:
  1. Create a new module, and then type or paste the following lines in the Declarations section:
    Option Explicit
    
    ' These represent the possible returns errors from API.
    Public Const ERROR_BAD_DEVICE = 1200&
    Public Const ERROR_CONNECTION_UNAVAIL = 1201&
    Public Const ERROR_EXTENDED_ERROR = 1208&
    Public Const ERROR_MORE_DATA = 234
    Public Const ERROR_NOT_SUPPORTED = 50&
    Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
    Public Const ERROR_NO_NETWORK = 1222&
    Public Const ERROR_NOT_CONNECTED = 2250&
    Public Const NO_ERROR = 0
    
    ' This API declaration is used to return the
    ' UNC path from a drive letter.
    
    Declare Function WNetGetConnection Lib "mpr.dll" Alias _
                     "WNetGetConnectionA" _
                     (ByVal lpszLocalName As String, _
                     ByVal lpszRemoteName As String, _
                     cbRemoteName As Long) As Long
    					
  2. Type or paste the following procedure:
    Function GetUNCPath(strDriveLetter As String) As String
       On Local Error GoTo GetUNCPath_Err
       Dim Msg As String, lngReturn As Long
       Dim lpszLocalName As String
       Dim lpszRemoteName As String
       Dim cbRemoteName As Long
       lpszLocalName = strDriveLetter
       lpszRemoteName = String$(255, Chr$(32))
       cbRemoteName = Len(lpszRemoteName)
       lngReturn = WNetGetConnection(lpszLocalName, _
                                     lpszRemoteName, _
                                     cbRemoteName)
       Select Case lngReturn
          Case ERROR_BAD_DEVICE
             Msg = "Error: Bad Device"
          Case ERROR_CONNECTION_UNAVAIL
             Msg = "Error: Connection Un-Available"
          Case ERROR_EXTENDED_ERROR
             Msg = "Error: Extended Error"
          Case ERROR_MORE_DATA
             Msg = "Error: More Data"
          Case ERROR_NOT_SUPPORTED
             Msg = "Error: Feature not Supported"
          Case ERROR_NO_NET_OR_BAD_PATH
             Msg = "Error: No Network Available or Bad Path"
          Case ERROR_NO_NETWORK
             Msg = "Error: No Network Available"
          Case ERROR_NOT_CONNECTED
             Msg = "Error: Not Connected"
          Case NO_ERROR
             ' all is successful...
       End Select
       If Len(Msg) Then
          MsgBox Msg, vbInformation
       Else
          ' Display the path in a Message box or return
          ' the UNC through the function.
          MsgBox Left$(lpszRemoteName, cbRemoteName)
          GetUNCPath = Left$(lpszRemoteName, cbRemoteName)
       End If
    GetUNCPath_End:
       Exit Function
    GetUNCPath_Err:
       MsgBox Err.Description, vbInformation
       Resume GetUNCPath_End
    End Function
    
    					
  3. To test this function, type the following line in the Immediate window, and then press ENTER:

    ?GetUNCPath("h:")

    A simple message box appears containing the UNC path of the specified network drive letter.

    NOTE: This example assumes that you have a mapped network drive assigned to drive letter H. Some of the possible return values for the GetUNCPath function include ERROR_BAD_DEVICE, ERROR_CONNECTION_UNAVAIL, and ERROR_NOT_CONNECTED. Other run-time errors may be returned from the function, in which case, error trapping should be implemented.

REFERENCES

For more information about declaring API functions, click Microsoft Visual Basic Help on the Help menu, type declare statement example in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbinfo kbProgramming KB210050