Ever simply needed to rename a file? Well, once again there are a multitude of ways to accomplish this within MS Access. I have seen numerous postings suggest using a scripting object and then copy the file while renaming the copied version. To me, if all you need is to rename the file, this is a uselessly complicated method. Instead, why not simply use the Name Statement? It can all be accomplished in one clean line of code!!!
Name "C:\OldFileName" AS "C:\NewFileName"
Actually, you can even use the Name command to move and rename a file simultaneously.
Name "C:\OldDir\OldFileName" AS "C:\NewDir\NewFileName"
As you can see this is another versatile command to be aware of.
Hi,
I’ve used this method in the past, but it is now failing.
My line of code looks like this:
Name sOldFile As sNewFile
I get the following error:
Run-Time error ‘5’:
Invalid procedure call or argument
When I hover over Name it states “Microsoft Access”
Do you need a reference for this function?
What vesion of Access are you running. Name is a built-in statement and should not require any extra references to work.
There is one notable limitation: “it can only rename an existing directory or folder when both OldPathName and NewPathName are located on the same drive”.
I tried this:(Gadm=”C:\Users\bku\bestanden\test” )
gs1 = Gadm & “.babs”
gs2 = Gadm & “,accdb”
Name gs2 As gs1
but it does not work
The 1st things that jumps out at me are:
It helps to show the full procedure so we can see your declarations.
I’d try something more like:
This assumes gs2 exists and you’re renaming it to file1.accdb.
Obviously, you need to replace ‘file1’ with the actual name of your file.
Stupid! It was the error you told me
gs2 has a typo (, instead of .)
Thanks a lot
I have directories and files with spaces in them, how would i rename these? When I do try Name “C:\Old Dir\OldFileName” As “C:\New Dir\NewFileName” it tells me it can not find the file, I take out the spaces and it works great.
Not too sure what to tell you. I did a few tests and they all worked for me just fine.
Name “C:\Users\Daniel\Documents\My test dir\My Test2.txt” AS “C:\Users\Daniel\Documents\My test dir\My Test.txt”
Name “C:\Users\Daniel\Documents\My test dir” AS “C:\Users\Daniel\Documents\My test dir2”
I saw no diffirence whether or not there were, or were not any spaces.
The only thing I can possibly see is your example does not include the extension of the file, perhaps it was an oversight in the post, but worth checking.
Name “C:\Old Dir\OldFileName.ext” As “C:\New Dir\NewFileName.ext”
I had the same issue. The program interpret ‘Name’ as variable instead of a command.
Access 2007, macro within a form .
Thanks for this!
Brilliant. Thank you for sharing.
Hi,
How come this line is not working
Name “D:\Students\” Like “myPic*” As “D:\Students\myNewPic.jpg”
I have tried my best but failed
Because you’ve added a ‘Like “myPic”‘ into the command.
The command is
Name BeforeName AS AfterName
you can’t do anything else, modify, …