Solved

Network Drives

Posted on 1998-08-28
10
162 Views
Last Modified: 2010-04-30
How do you map a network drive with VB, not using Shell() and "net use"? (no dos commands)
0
Comment
Question by:IsleOfView
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 2

Expert Comment

by:dabellei
ID: 1431678
Wich network are you using
0
 

Author Comment

by:IsleOfView
ID: 1431679
Microsoft Networking - I need to connect to a CD Server and to an NT File Server.  (I believe all are running WINS over TCP/IP)
0
 

Expert Comment

by:mskannan
ID: 1431680

I faced a few problems while mapping a network drive.
Suppose if i use net use to map a drive and assign a letter(say M) and if the user has already mapped any network drives for this letter, then this command automatically overwrites the drive letter and my end users are sometimes complaining about this feature.

Later, instead of mapping a drive and assigning a letter, I used to map the drive by its path. This is working fine.

\\Server1\foldername

You can also try this one.




0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 14

Expert Comment

by:waty
ID: 1431681
Here is complete code to map network drive in VB. I have to do that because, I map drives from a particular NT Server :

If you like this code, award me points.


Option Explicit
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) 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

'-- Error number and message
Public ErrorNum         As Long
Public ErrorMsg         As String

Public rc               As Long

Private Const ERROR_NO_CONNECTION = 8
Private Const ERROR_NO_DISCONNECT = 9

Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")
   
   On Error GoTo Err_Connect
   Me.ErrorNum = 0
   Me.ErrorMsg = ""
   rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive & Chr(0))
   If rc <> 0 Then GoTo Err_Connect
   
   Exit Sub

Err_Connect:
   Me.ErrorNum = rc
   Me.ErrorMsg = WnetError(rc)

End Sub

Public Sub DisConnect(sDrive As String)
   
   On Error GoTo Err_DisConnect
   Me.ErrorNum = 0
   Me.ErrorMsg = ""
   rc = WNetCancelConnection(sDrive + Chr(0), 0)
   If rc <> 0 Then GoTo Err_DisConnect
   
   Exit Sub
Err_DisConnect:
   Me.ErrorNum = rc
   Me.ErrorMsg = WnetError(rc)

End Sub

Private Function WnetError(Errcode As Long) As String

   Select Case Errcode
      Case WN_Not_Supported:
         WnetError = "Function is not supported."
      Case WN_Out_Of_Memory:
         WnetError = "Out of Memory."
      Case WN_Net_Error:
         WnetError = "An error occurred on the network."
      Case WN_Bad_Pointer:
         WnetError = "The Pointer was Invalid."
      Case WN_Bad_NetName:
         WnetError = "Invalid Network Resource Name."
      Case WN_Bad_Password:
         WnetError = "The Password was Invalid."
      Case WN_Bad_Localname:
         WnetError = "The local device name was invalid."
      Case WN_Access_Denied:
         WnetError = "A security violation occurred."
      Case WN_Already_Connected:
         WnetError = "The local device was connected to a remote resource."
      Case Else:
         WnetError = "Unrecognized Error " + str(Errcode) + "."
   End Select

End Function


0
 

Author Comment

by:IsleOfView
ID: 1431682
In response to waty's answer--I tried that code, but when I print the debugging info, I get an unrecognized error "1326".  Any ideas?
0
 
LVL 2

Expert Comment

by:mkmccreary
ID: 1431683
There are a few Microsoft Articles on dealing with this one.  These may help, the numbers are:
     Q173011
     Q138905
     Q138904
     

I have some code at the office.  If some of these look like they will help, just let me know.

Later
0
 
LVL 14

Expert Comment

by:waty
ID: 1431684
What do you try to connect, I use those API to connect NT drives, and Unix drives.

Could you explain a little more
0
 

Author Comment

by:IsleOfView
ID: 1431685
Thanks to mkmccreary, Q173011 detailed the answer to my question perfectly.
0
 
LVL 2

Accepted Solution

by:
mkmccreary earned 40 total points
ID: 1431686
Glad it helped out.

Later,
Martin
0
 
LVL 6

Expert Comment

by:alamo
ID: 1431687
?!?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

696 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