How to handle the message: Run time error '58 "File already exists" when renaming a file to an existing file name ?

zimmer9
zimmer9 used Ask the Experts™
on
I am developing an Access application with Access VBA.

How to handle the message: Run time error '58 "File already exists" when renaming a file to an existing file name. Is there a way to send a MsgBox with this error instead of the Microsoft Visual Basis run time error being generated ?

For ex:

sfile = "\\nydfs1\root\lib\CONTROLLERS\IIG\CASH_CONTROL\ccshared\AbandonedProperty\UDL\Access\UDLLexisNexis\Morgan_Stanley_UDL_LN_prc" & ".xls"

If isFileExist(sfile) Then
    Name sfile As Left(sfile, Len(sfile) - 4) & "_" & Format(Date, "yymmdd") & ".xls"
End If
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Nerd
Most Valuable Expert 2012
Top Expert 2013
Commented:
Try using error handling around your code:


On Error Goto EH     '*****<----- Redirect on error

sfile = "\\nydfs1\root\lib\CONTROLLERS\IIG\CASH_CONTROL\ccshared\AbandonedProperty\UDL\Access\UDLLexisNexis\Morgan_Stanley_UDL_LN_prc" & ".xls"

If isFileExist(sfile) Then
    Name sfile As Left(sfile, Len(sfile) - 4) & "_" & Format(Date, "yymmdd") & ".xls"
End If

Exit Sub  '***** (or exit function if this is a function)

EH:
  If Err.Number = 58 then     '<----- Special handling for File Exists
        msgbox "File Exists"
  Else
        msgbox "Error " & err.number & ": " & err.description   '<---- General error handling
  end if

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial