Solved

using vba to copy file to a network share with username and password

Posted on 2004-08-26
6
3,697 Views
Last Modified: 2012-05-05
I have an access project and I am using the file dialog to allow a user to select a file. I need to copy this file to a network share that is not mounted as a drive, since I do not want anyone access to the shared folder, only the application.

So I need to be able to pass the username and password in the copy command somehow. I tried copy (xcopy) and cannot seem to pass username, password information. I could perhaps use net use to mount it, copy the file, then unmount it. If that turns out is the only solution, how do I find the first available drive letter to use for mounting.

0
Comment
Question by:drakkarnoir
  • 3
  • 2
6 Comments
 

Author Comment

by:drakkarnoir
ID: 11906872
I tried using Shell() to do a net use to mount the network share, then filecopy to copy it, then net use to delete the drive. it is very very slow that way, so I hope there may be a better alternative
0
 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 11909341
Well, I don't know if this is what you're looking for, but it gets the job done.  I don't know what you consider to be "very very slow" so this might be too slow to.  I tested it by copying a 15mb file over an 11mbps wireless network.  It took about 30 seconds in that setting.  Hope this helps.

set objFSO = CreateObject("Scripting.FileSystemObject")
set wshNet = CreateObject("WScript.Network")
strUsername = "Domain\Username"
strPassword = "Password"
strSource = "Path and file to copy"
strDestination = GetNextLetter("F")     '<-- You can change the letter.  It represents the starting letter to work from.
wshNet.MapNetworkDrive strDestination, "\\Server\Share", , strUsername, strPassword
objFSO.CopyFile strSource, strDestination & "\"
wshNet.RemoveNetworkDrive strDestination, True
Set objFSO = Nothing
Set wshNetwork = Nothing

'The following function is from: http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q303209&ID=KB;EN-US;Q303209
Function GetNextLetter(DriveLetter)
   'Start x at the ascii value of the drive letter to start the search
   'unless something is passed in. This sample uses capital letters and
   'starts at F.
   If IsEmpty(DriveLetter) Then
    x = 70
   Else
    x = Asc(DriveLetter)
   End If
   
   Set oDrives = wshNet.EnumNetworkDrives

   'Step by two since the first item in the collection is the drive letter
   'and the second item is the network mapping
   For i = 0 To oDrives.Count - 1 Step 2
    If Chr(x) & ":" = oDrives.Item(i) Then
        x = x + 1
    End If
   Next
   
   GetNextLetter = Chr(x) & ":"
End Function
0
 

Author Comment

by:drakkarnoir
ID: 11914896
that works good, my anti-virus popped up when doing Set oDrives = wshNet.EnumNetworkDrives but I can work around that, thanks.
0
Highfive Gives IT Their Time Back

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!

 
LVL 76

Expert Comment

by:David Lee
ID: 11915349
You're welcome.  Glad I could help.
0
 

Expert Comment

by:andybrooke
ID: 32942562
I tried this but get object required error 424. And debugs to Set oDrives = wshNet.EnumNetworkDrives
0
 
LVL 76

Expert Comment

by:David Lee
ID: 32966996
Hi, andybrooke.

That would mean that the object wshNet wasn't created successfully on line #2 of the code or was destroyed prematurely.  Can you run the code from a debugger to see what's happening?
0

Featured Post

Highfive Gives IT Their Time Back

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!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

746 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

11 Experts available now in Live!

Get 1:1 Help Now