Solved

Network Drives

Posted on 1998-08-28
10
160 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 / DAO 3.6 / run time error: Couldn't find installable ISAM 7 103
MsgBox 2 59
Using "ScreenUpdating" 6 73
Add and format columns in vb6 7 63
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

821 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