?
Solved

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

Posted on 2004-08-26
6
Medium Priority
?
4,775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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