We help IT Professionals succeed at work.

Mapping Network Drive

cplau
cplau asked
on
2,181 Views
Last Modified: 2007-12-19
Hi All,

I would like map a network drive by connecting with different username & password.

I try this:

Private Const CONNECT_UPDATE_PROFILE = &H1

Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&

Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
  Alias "WNetAddConnection2A" (lpNetResource As NETCONNECT, _
  ByVal lpPassword As String, ByVal lpUserName As String, _
  ByVal dwFlags As Long) As Long
 
Private Type NETCONNECT
   dwScope As Long
   dwType As Long
   dwDisplayType As Long
   dwUsage As Long
   lpLocalName As String
   lpRemoteName As String
   lpComment As String
   lpProvider As String
End Type

Public Function MapDrive(LocalDrive As String, _
  RemoteDrive As String, Optional Username As String, _
  Optional Password As String) As Boolean

On Error GoTo Handler:

   Dim NetR As NETCONNECT

   NetR.dwScope = RESOURCE_GLOBALNET
   NetR.dwType = RESOURCETYPE_ANY
   NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
   NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
   NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
   NetR.lpRemoteName = RemoteDrive
   MapDrive = (WNetAddConnection2(NetR, Username, Password, _
       CONNECT_UPDATE_PROFILE) = 0)

End Function


I found I cannot map a drive by providing username & password.

I can estabilish a connection by inputting the
local drive & remote drive only.

How can I solve this problem?
Thanks!



Comment
Watch Question

Commented:
Try scripting ;

Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
    WshNetwork.MapNetworkDrive "E:", "\\Server\Share", False, "username", "password"
Set WshNetwork = Nothing

Author

Commented:
Hi aelatik,

I've tried your code, but this error popup while I am tring to map
a network drive

Run-time error '-2147023677 (800704c3)'
The credentials supplied confilct with an existing set of credentials.

How can I solve this?

Thanks!
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
cplau,
    Take a look at METHOD 2 on this page. Perhaps it will help.

http://www.vbusers.com/code/codeget.asp?ThreadID=248&PostID=1&NumReplies=0

Dang123





Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.