If file already exists ask user to save as a different file

Posted on 2011-02-27
Last Modified: 2012-06-27

Currently I am using the following code to check and see if a file with the same name exists. If so, delete the file and replace with a new file.

What I would like to do is check to see if the file exists, if so, then generate a message that says something like, "file already exists, would you like to save it with a different file name?" If the user says 'yes' then I would like to generate a 'save as' dialogue box OR a input box to alow the user to save the file name differently.

How would I code this?

If Dir(TheFile) <> "" Then
   Kill (TheFile)
    TheFile = CurrentProject.Path & "\TripReport-" & strPlace & "-" & strUserName & "-" & TheDate & ".rtf"
 End If
    DoCmd.OutputTo acReport, "rptTrip", "RichTextFormat(*.rtf)", TheFile, , , acExportQualityScreen

Open in new window

Question by:shogun5
  • 2
LVL 19

Accepted Solution

Eric Sherman earned 500 total points
ID: 34994406
Try this ...

 TheFile = CurrentProject.Path & "\TripReport-" & strPlace & "-" & strUserName & "-" & TheDate & ".rtf"
 End If

If Dir(TheFile) <> "" Then
    If MsgBox("File already exist, save with a different name?", vbYesNo, "") = vbYes Then
          ThFile = InputBox("Enter a path and file name.")
    End If  
End If
DoCmd.OutputTo acReport, "rptTrip", "RichTextFormat(*.rtf)", TheFile, , , acExportQualityScreen



Author Closing Comment

ID: 34996156
Okay, this got me to the final solution:

TheFile = CurrentProject.Path & "\TripReport-" & strPlace & "-" & strUserName & "-" & TheDate & ".rtf"
         If Dir(TheFile) <> "" Then
            If MsgBox("File already exist, save with a different name?", vbYesNo, "") = vbYes Then
                  TheFile = InputBox("Enter a path and file name.", "File Name", TheFile)
            End If
        End If

LVL 19

Expert Comment

by:Eric Sherman
ID: 34998996
Thanks for the points.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now