file manipulation with Access using VBA?

  • Thread starter Thread starter rdonges
  • Start date Start date
R

rdonges

Can Access rename and/or copy files using VBA? I need users to specify a
file name in a text box, and then have Access rename the file and copy it to
a different location using VBA. I haven't been able to find a function in
VBA to do what I want. Thanks for any suggestions.

R. Donges
 
Check Access VB Help on the FileCopy Method.

You can also use the CopyFile Method of the FileSystemObject of the MS
Scripting Library. To use this, you will need to add the Scripting Library
into the References Collection of your database. This also has the MoveFile
Method.

Check Access Help on the above-mentioned Methods
 
rdonges said:
Can Access rename and/or copy files using VBA? I need users to
specify a file name in a text box, and then have Access rename the
file and copy it to a different location using VBA. I haven't been
able to find a function in VBA to do what I want. Thanks for any
suggestions.

Sure. The VBA Name statement can rename a file (and also move it from
one place to another if you want), while the FileCopy statement can copy
a file (and rename it in the process, if you want).

For example, to copy file "C:\Folder1\Test.txt" to make
"C:\Folder2\Output.txt", leaving the original in place, you could simply
write

FileCopy "C:\Folder1\Test.txt", "C:\Folder2\Output.txt"

To *move* that same file and rename it, so it no longer exists in the
old location, you could write

Name "C:\Folder1\Test.txt" As "C:\Folder2\Output.txt"
 
To rename a file:
Name <oldfilename> As <newfilename>

To copy a file:
FileCopy <sourcefile> <destination>
 
Note: a comma is required between <sourcefile> and destination>.

Well spotted, Dirk :-)

I didn't get yours and Van's replies until after I'd posted mine!
 
Wow! Quick and easy. It even works with variables. Thanks for the quick
response everyone.

Randy
 
I am using this statement. It seems to work fine in one
part of my code, but I have it in one other place and I
am getting a "file not found" error. What would cause
this?? I have verified the location of the file to be
copied... here is the code!

'sets up file to be copied to the backup directory
strOrig = ROOT_PATH & strPath
strOrig = strOrig & strName
'strBackFile = strName & " " & strRevision
strBackup = ROOT_PATH & "backup\" & strName & " " &
strRevision & ".doc"
FileCopy strOrig, strBackup

Thanks for any suggestions!

Tammy
 
Sorry, got this working now! THANKS!
-----Original Message-----
I am using this statement. It seems to work fine in one
part of my code, but I have it in one other place and I
am getting a "file not found" error. What would cause
this?? I have verified the location of the file to be
copied... here is the code!

'sets up file to be copied to the backup directory
strOrig = ROOT_PATH & strPath
strOrig = strOrig & strName
'strBackFile = strName & " " & strRevision
strBackup = ROOT_PATH & "backup\" & strName & " " &
strRevision & ".doc"
FileCopy strOrig, strBackup

Thanks for any suggestions!

Tammy
 
Back
Top