Posted on 1997-12-23
Last Modified: 2008-02-01
Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

could someone please give me an example of how to get this to work.
Question by:LostOne
  • 2

Accepted Solution

mrmick earned 100 total points
ID: 1447026
'Create a new project, Add a CommandButton and a DriveListBox
'Use Windows Explorer verify the ServerName and ResourceName...
'- go down to Network Neighborhood and open the tree...
'- open the server tree to view mapable resources...

'If, for instance, the Server Name is "TheServer"...
'and under the "TheServer" tree you wanted to map to "C"...
'The resource name would be:


'Replace the following for this example:
Const ServerResource = "\\TheServer\C"
Const Password = "YourPassword"
Const MapToDrive = "X:"

Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long

Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34

Private Sub Command1_Click()

   If ConnectResource(ServerResource, Password, MapToDrive) Then
   End If

End Sub

Function ConnectResource(strRes$, strPass, strDrive) As Boolean
Dim RC As Long

   RC = WNetAddConnection(UCase(strRes) & Chr$(0), strPass & Chr$(0), strDrive & Chr$(0))
   If RC = WN_Success Then
      ConnectResource = True
      MsgBox "Connection Error: " & RC
   End If

End Function


Author Comment

ID: 1447027
Actually I was looking for WNetGetConnection which I thought would give me the NetResource from a drive letter. Sorry if I didn't make that clear.

Expert Comment

ID: 1447028
My fault, I misread it.  Create a new project and add a single command button to Form1.  Paste code below into the From1 general declarations section.  Run the project, click the button and watch the immediate window for the result.

Note: Replace "K" in the Command1_Click event with the local drive for which you wish to retrieve the associated network resource name.

Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

Const NO_ERROR = 0

Private Sub Command1_Click()

 Debug.Print GetResource("K:")

End Sub
Function GetResource(strDrv$) As String

 Dim strRes$, ResLen&, RC&

 ResLen = 32 'Buffersize - will be enlarged if necessary.
  strRes = Space(ResLen)
  RC = WNetGetConnection(strDrv & Chr$(0), strRes, ResLen)

 If RC = NO_ERROR Then
  GetResource = Left(strRes, ResLen)
  MsgBox "Connection Error: " & RC
 End If

End Function


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now