Query export save file if file exist

I have a query I save/export to Drive C in Excel (2007) format with the click of a command button.
I want to name the Excel file name MyFile plus the current date. When I click this command button I want to check if the file already exist, if it does, gave the user the opportunity to decide with the response of a message box action  if overwrite of exit the  routine. This is what I got but is not working.

Private Sub cmdExportMyQuery_Click()

    Dim response As Boolean
    Dim Msg As String
    Msg = "This file already been save to your C Drive, do you want to overwrite existing file?"


    If FileExist("C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx") Then
        response = MsgBox(Msg, vbYesNo, "Data validation")
        If response = 6 Then
            DoCmd.OutputTo acOutputQuery, "myQuery", acFormatXLSX, "C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx", True
        ElseIf response = 7 Then
            Exit Sub
        End If
    End If
    
End Sub
'-----------------
Public Function FileExist(strPath) As Boolean
         If Len(Dir$(strPath)) > 0 Then
            FileExist = True
         Else
            FileExist = False
         End If
End Function

Open in new window

LVL 1
Exl04Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
what is not working? please be specific


   If FileExist("C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx") Then
        response = MsgBox(Msg, vbYesNo, "Data validation")
        If response = vbyes Then
            DoCmd.OutputTo acOutputQuery, "myQuery", acFormatXLSX, "C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx", True
        Else
            Exit Sub
        End If
    End If

Open in new window

0
Rey Obrero (Capricorn1)Commented:
 
  If FileExist("C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx") Then
        response = MsgBox(Msg, vbYesNo, "Data validation")
        If response = vbyes Then
        kill "C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx"
            DoCmd.OutputTo acOutputQuery, "myQuery", acFormatXLSX, "C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx", True
        Else
            Exit Sub
        End If
    End If

Open in new window

0
JezWaltersCommented:
You need to define your "response" variable to be an Integer, not a Boolean:
Private Sub cmdExportMyQuery_Click()

    Dim intResponse As Integer
    Dim strFile As String

    strFile = "C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx"
    If Dir(strFile) <> "" Then
        intResponse = MsgBox("This file has already been saved to your C Drive, do you want to overwrite the existing file?", _
                             vbYesNo, _
                             "Data validation")
        If intResponse = vbYes Then
            DoCmd.OutputTo acOutputQuery, "myQuery", acFormatXLSX, strFile, True
        End If
    End If
      
End Sub

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rey Obrero (Capricorn1)Commented:
nice catch Jez, didn't see that
0
JezWaltersCommented:
Sorry, slightly wrong logic:
Private Sub cmdExportMyQuery_Click()

    Dim intResponse As Integer
    Dim strFile As String

    strFile = "C:\MyFile " & Format(Now(), "yyyy-mm-dd") & ".xlsx"
    If Dir(strFile) <> "" Then
        intResponse = MsgBox("This file has already been saved to your C Drive, do you want to overwrite the existing file?", _
                             vbYesNo, _
                             "Data validation")
    Else
        intResponse = vbYes
    End If
    If intResponse = vbYes Then
        DoCmd.OutputTo acOutputQuery, "myQuery", acFormatXLSX, strFile, True
    End If
      
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exl04Author Commented:
Great, that was the problem. Thanks to both of you!
0
Exl04Author Commented:
Thanks buddy!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.