Mapping a network drive

Posted on 2002-07-10
Medium Priority
Last Modified: 2008-03-10

I'm a total rookie at VB som please bare with me. I have been given the task of making a vb-exe-file that maps up three different network-shares depending on commandbutton-clicks.

I.e. I want to execute the following system-command at the click of a button:

net use u: /d > NUL
net use u: \\myshare\myfolder
Can anyone help me...?

Question by:HenningF
  • 3
  • 3
  • 2
  • +4
LVL 56

Expert Comment

by:Ryan Chong
ID: 7142828
Don't know if this link make help:


Author Comment

ID: 7142850
Unfortunatley I'm not experienced enaugh to understand if the link you gave me would do the trick or not. But something tells me that running two lines of system command shouldn't be all that hard, and the two lines are always the same so it needs no user-interface or nothing, just the click of a button. It will then map three different shares to the same drive-letter depending on which button is pushed.

If I have explained poorly please let me know...

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7142862
what ryancys gave you is the API way of doing things, which is the "proper" way to do things.

The "quick and dirty" way would be:
shell "net use u: /d > NUL" & vbcrlf & "net use u: \\myshare\myfolder"

Or, if that gives problems, save the lines to a .bat file and run that batch file using the Shell function.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7142864
Bare in mind that the Shell function as such is async, so when your code continues, the NET command might not have finished, and you won't get reported any problems. And that's the reason why you would use the API, which would return an error value to your application.


Expert Comment

ID: 7143076
I don't know if the above link is what you want.  It sounds like you want to connect to a share, not create one.  Here is the link you would want...



You can use the quick and dirty Shell function to execute the net use command.  Do the following...

Create a new project.
Place a command button on the form named Command1.
Paste the following code into the General declarations section of the form...

Private Sub Command1_Click()
   Shell "net use u: /d > NUL"
   Shell "net use u: \\SERVER\SHARE"
End Sub

Change the name to your path and drive letter.  This should work.
LVL 28

Expert Comment

ID: 7143445
This will use shell to issue the "net use" command to delete the mapping and then it will wait for the command to complete before attempting to re-map the drive.

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103

Private Sub Shell32Bit(ByVal JobToDo As String)
         Dim hProcess As Long
         Dim RetVal As Long
         hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(JobToDo, vbHide))
             GetExitCodeProcess hProcess, RetVal
             DoEvents: Sleep 100
         Loop While RetVal = STILL_ACTIVE
End Sub

Private Sub Command1_Click()
' Delete mapping and wait for it to complete before remapping
Shell32Bit "command.com /c net use u: /d > NUL"
' This will not run until above command is completed
Shell32Bit "command.com /c net use u: \\myshare\myfolder"
End Sub
LVL 16

Expert Comment

ID: 7144136
The MapNetworkDrive method maps the share point specified by strRemoteName to the local resource name, strLocalName.

WshNetwork.MapNetworkDrive strLocalName, strRemoteName, [bUpdateProfile], [strUser], [strPassword]
Local resource name to map to.
Remote share to map.
If bUpdateProfile is supplied and its value is TRUE, this mapping is stored in the user profile.
If you are mapping the share point using the credentials of someone other than current user, you can specify strUser and strPassword.
If you are mapping the share point using the credentials of someone other than current user, you can specify strUser and strPassword.
Set WshNetwork = Wscript.CreateObject("Wscript.Network")
WshNetwork.MapNetworkDrive "Z:", "\\Server\Share"


Author Comment

ID: 7145540
Thanx guys...I'm trying out these solutions as we speak, I will get back to you shortly...

LVL 16

Expert Comment

ID: 7145976
Take in mind that you need to have an error handler in the mapping procedure.

Accepted Solution

jayeshshah earned 600 total points
ID: 7152221
Hi there,
You can use the below code to map a network drive
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long

Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String

    strLocalDriveLetter = "K:"                  'Local drive letter to be mapped
    strPassword = ""                            'specify network password if required
    strNetworkPathName = "\\NETWORKPATH\VOL1"   'path to network drive
    If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
        MsgBox ("An Error occurred mapping the drive")
        MsgBox ("Drive successfully mapped!")
    End If


Hope this sufficies your work


Author Comment

ID: 7153686
Yeehaa! Finally a soultion I got to work on the fly! I needed to add another function to remove the mapping, but once you got me on to the WNetAddConnection I searched MSDN Online and found how to do that aswell.

I increased the points to 150 for the excellent answer :) I would have given some points to all you other guys aswell, but unfortunatly I don't have many left... sorry.

LVL 16

Expert Comment

ID: 7154222
"Yeehaa! Finally a soultion I got to work on the fly!
Why?, code from MSDN didn't works?. I tried/used it whitout problems...!

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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 …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

584 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