File write error if file already exists

G

Guest

Hello,

I have this code that writes a file. Everything works okay if the file is a new one. But if the file that is being written already exists, then I get a “file already exists†error. Is there a call I can make to just delete the file from the harddisk, and then when I go to perform the write I will not get an error because the old file is gone?

Here is the code I am using:

** start code **
Private Sub cmdExportXML_Click()
Dim qryName As String
Dim strFilter As String
Dim strSaveFileName As String
Dim conn As ADODB.Connection, rst As New ADODB.Recordset

Set conn = Application.CurrentProject.Connection
qryName = "qryNodeTableXmlExport"

' obtain file name and location to put file from user using Save As dialog box
strFilter = ahtAddFilterItem(strFilter, "XML Files (*.xml)", "*.xml")
strSaveFileName = ahtCommonFileOpenSave( _
OpenFile:=False, _
DialogTitle:="Please select or enter name of XML file to export.", _
Filter:=strFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT)
'Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)

' generate the XML file
With rst
.Open qryName, conn, adOpenDynamic, adLockOptimistic
.Save strSaveFileName, adPersistXML
.Close
End With

MsgBox "Successfully generated XML File [ " & strSaveFileName & " ]", , "Export XML"

End Sub
** end code **

Thanks,
Mike
 
K

Ken Snell

Check out the Kill command:

If Dir("C:\MyFolder\MyFile.txt") <> "" Then Kill "C:\MyFolder\MyFile.txt"


--
Ken Snell
<MS ACCESS MVP>

Mike P said:
Hello,

I have this code that writes a file. Everything works okay if the file is
a new one. But if the file that is being written already exists, then I get
a "file already exists" error. Is there a call I can make to just delete
the file from the harddisk, and then when I go to perform the write I will
not get an error because the old file is gone?
Here is the code I am using:

** start code **
Private Sub cmdExportXML_Click()
Dim qryName As String
Dim strFilter As String
Dim strSaveFileName As String
Dim conn As ADODB.Connection, rst As New ADODB.Recordset

Set conn = Application.CurrentProject.Connection
qryName = "qryNodeTableXmlExport"

' obtain file name and location to put file from user using Save As dialog box
strFilter = ahtAddFilterItem(strFilter, "XML Files (*.xml)", "*.xml")
strSaveFileName = ahtCommonFileOpenSave( _
OpenFile:=False, _
DialogTitle:="Please select or enter name of XML file to export.", _
Filter:=strFilter, _
Flags:=ahtOFN_OVERWRITEPROMPT)
'Flags:=ahtOFN_OVERWRITEPROMPT Or ahtOFN_READONLY)

' generate the XML file
With rst
.Open qryName, conn, adOpenDynamic, adLockOptimistic
.Save strSaveFileName, adPersistXML
.Close
End With

MsgBox "Successfully generated XML File [ " & strSaveFileName & " ]", , "Export XML"

End Sub
** end code **

Thanks,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top