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
402 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 500 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 57
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 57
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

749 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