Solved

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

Posted on 2004-08-26
6
3,991 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 42
Reading the Contents of a Directory In Access VBA 5 68
Passing a Text Box name to a Sub 6 96
Zip Folders Using Chilkat Routines 1 36
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

813 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

10 Experts available now in Live!

Get 1:1 Help Now