Accessing a remote network drive

Posted on 2006-04-06
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
    LVL 76

    Expert Comment

    by:David Lee
    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

    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

    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
    Private Const RESOURCETYPE_DISK = &H1

    Private Type NETRESOURCE
       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
        .dwType = RESOURCETYPE_DISK
        .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

    I tried your solution and it worked... thanks a lot.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now