Checking for a Directory Problem

FoeHunter

Board Regular
Joined
Nov 12, 2008
Messages
236
The following code works in two of the following conditions, but not for the 3rd. How can it be re-written to work for all 3?
Condition1 - Directory does not exist at all (the code works)
Condition2 - Directory does exist, but there is no excel file (the code does not work)
Condition3 - Directory does exist, and the excel file is already present (the code works)

As always, thanks in advance

Code:
'Defines variables
    Dim rs As DAO.Recordset
    Dim FPath As String
    Dim FName As String
    Set rs = CurrentDb.OpenRecordset("SELECT t.[File Location], t.[File Name] FROM [ExportInformation] t", dbOpenDynaset)
' Pulls info from table
    FPath = rs.Fields("File Location").Value
    FName = rs.Fields("File Name").Value
'Verifies to see if export directory exists.  If it does not already exist, it is created.
    If Dir(FPath & "\") = "" Then
        MkDir (FPath)
    Else
    'do nothing for the "C:\Junk" directory already exists
    End If
' Error handeling routine
    On Error GoTo Errorhand
' Exports the database to an excel file
    DoCmd.TransferSpreadsheet acExport, 10, "MasterTable", FPath & "\" & FName, True, , False
If ErrorCount > 0 Then GoTo Errorhand2 Else GoTo Errorhand3
Errorhand:
     ErrorCount = ErrorCount + 1
     Resume Next
Errorhand2:
    MsgBox ("Custom export was not complete."), vbCritical, "//// ERROR ////"
    GoTo FinalEnd
    
Errorhand3:
    MsgBox ("Successfully exported to " & FPath & "\" & FName), vbInformation, "!!!! SUCCESS !!!!"
    GoTo FinalEnd
FinalEnd:
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Rich (BB code):
    FPath = rs.Fields("File Location").Value
    FName = rs.Fields("File Name").Value
'Verifies to see if export directory AND file exists.
    If Dir(FPath & "\" & FName) <> "" Then
        ' excel file exists
    End If
 
Upvote 0
Perhaps as a variation on the them you just want to delete the file if it exists:
Code:
    FPath = rs.Fields("File Location").Value
    FName = rs.Fields("File Name").Value
    FFullPath = FPath & "\" & FName
'Verifies to see if export directory AND file exists.
    If Dir(FFullPath) <> "" Then
        [COLOR="Blue"]Kill FFullPath[/COLOR]
    End If
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,461
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top