Accessing a remote network drive

Posted on 2006-04-06
Medium Priority
Last Modified: 2010-04-07

I have a vb6 appl. that will run at different sites.  I will be using a central server located at one of the sites.  I will have crystal reports and other files at this site that I will need to access or copy from any of the sites.

I am using the crystal viewer and would like to open reports using the following:
Private Const str As String = "\\IP ADDR\share\folder\"
Set app = New CRAXDRT.Application
Set rpt = app.OpenReport(str & "Report1.rpt", 1)

I need to include the user login info with the IP addr and I don't want to map the drive with a letter.  What is the most efficent way to do this in my code?

Also, what's the best way to copy files over to the local machine?


Question by:villny123
  • 2
LVL 76

Expert Comment

by:David Lee
ID: 16397359
Hi villny123,

> I need to include the user login info with the IP addr
Sorry, I don't follow what you mean.  What user login info are you talking about and what's the connection to the IP addr?

> what's the best way to copy files over to the local machine
If the files are being copied on a local network, then a normal copy is fine.  If instead they're being copied across a WAN, then I recommend using either ftp or http.


Author Comment

ID: 16400612
By the login I meant:  At one of the remote sites, I mapped a network drive to the server and in addition to using the ip addr, I also had to login with a user name.  I am assuming that I need to duplicate this in my code.
LVL 28

Accepted Solution

vinnyd79 earned 2000 total points
ID: 16401182
Here is a simple example. Paste this onto a form with 2 command buttons. Be sure to edit the UNC path,Username &  Password. This will connect you to the share without having a drive mapped.

Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long

   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

Private Sub Command1_Click()
Dim l As Long
With nr
    .lpLocalName = ""
    .lpRemoteName = "\\\MyShare\Somefolder" ' unc to access
    .lpProvider = ""
End With

' pass the username & password
l = WNetAddConnection2(nr, "Mypassword", "MyUsername", 0)

' do a filecopy here if you wish
'FileCopy "C:\Myfile.txt", "\\\MyShare\Somefolder\MyFile.txt"

End Sub

Private Sub Command2_Click()
' close connection
Dim l As Long
l = WNetCancelConnection2("\\\MyShare\Somefolder", 0, True)
End Sub

Author Comment

ID: 16436614
I tried your solution and it worked... thanks a lot.

Featured Post

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.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

809 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