Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to rename a file even if a file by the same name already exists using Access 2003?

Posted on 2013-11-06
5
Medium Priority
?
425 Views
Last Modified: 2013-11-06
I am developing an Access 2003 application..

How would you rewrite the following code to rename a file even if a file with the same name (AGCS_NyCashBreaksBod.txt) already exists:

MyPath = CurrentProject.Path

filedat = "\AGCS_NyCashBreaksBod.dat"

newfiledat = "\AGCS_NyCashBreaksBod.txt"

If Dir(MyPath & filedat) <> "" Then
   Name MyPath & filedat As MyPath & newfiledat
End If
0
Comment
Question by:zimmer9
[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
5 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 39627705
Change the last block to check for the file's existence, and if it does exist you can delete it
If Dir(MyPath & filedat) <> "" Then
   If Dir(MyPath & newfiledat) <> "" Then Kill MyPath & newfiledat
   Name MyPath & filedat As MyPath & newfiledat
End If

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39627715
try


If Dir(MyPath & filedat) <> "" Then
   if dir(MyPath & newfiledat) <>"" then
      kill MyPath & newfiledat  'delete the file if exists
      end if
   Name MyPath & filedat As MyPath & newfiledat
End If
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39627722
Try this - you'll need a reference to the MS Scripting runtime library.
Dim fso As New FileSystemObject

MyPath = CurrentProject.Path

filedat = "\AGCS_NyCashBreaksBod.dat"

newfiledat = "\AGCS_NyCashBreaksBod.txt"

fso.CopyFile MyPath & filedat , MyPath & filedat , True

Open in new window

0
 
LVL 58
ID: 39627729
MyPath = CurrentProject.Path

filedat = "\AGCS_NyCashBreaksBod.dat"

newfiledat = "\AGCS_NyCashBreaksBod.txt"

If Dir(MyPath & filedat) <> "" Then
   On Error Resume Next
   Kill MyPath & newfiledat
   On Error Goto 0
   Name MyPath & filedat As MyPath & newfiledat
End If

Jim.
0
 
LVL 58
ID: 39627749
Just an FYI, you'll find that the code I posted is a tad faster because you only do the directory search once rather then twice.

If the file is local, it's not a big difference, but if it's on a network share, the difference is noticeable.

Jim.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

636 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