Solved

Network Drives

Posted on 1998-08-28
10
158 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
  • 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
 
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
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.

 
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

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now