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
363 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
5 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
Comment Utility
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 119

Expert Comment

by:Rey Obrero
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now