• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5617
  • Last Modified:

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

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
0
zimmer9
Asked:
zimmer9
1 Solution
 
mbizupCommented:
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
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now