Solved

MS Access VBA syntax help with message box.

Posted on 2015-02-18
4
124 Views
Last Modified: 2016-02-10
Hello Experts.

I need a little help with the syntax here.  I'm trying to get a message box to pop-up warning the user that a file already exists and also asks if they want to overwrite it.  If they choose no, then the export is cancelled.  If they choose yes the file is exported.  There is also a message box that pops-up when the file is exported successfully.

I have tried writing this different ways, but not successfully.  I would really appreciate some help.  I need something like this (only something that works):

Private Sub btn_exportList_Click()
    Const existingFilePrompt As String = "This file already exists in folder X:\EADB\Exports\.  Do you want to overwrite the file?"
    If Dir("X:\EADB\Exports\Agent_List_" & Format(Date, "yyyymmdd") & ".xlsx") <> "" Then
    MsgBox(existingFilePrompt, vbQuestion + vbYesNo) = vbNo
    Cancel = True
    Else
    DoCmd.OutputTo acOutputForm, "agencyListTblForm", acFormatXLSX, "F:\EADB\Exports\Agent_List_" & Format(Date, "yyyymmdd") & ".xlsx"
    MsgBox ("Successful Export"), vbInformation, "Transfer Complete"
    End If
End Sub

Open in new window


Thanks.
J
0
Comment
Question by:ferguson_jerald
  • 2
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40617227
Try this.

Private Sub btn_exportList_Click()
    Const existingFilePrompt As String = "This file already exists in folder X:\EADB\Exports\.  Do you want to overwrite the file?"
    If Dir("X:\EADB\Exports\Agent_List_" & Format(Date, "yyyymmdd") & ".xlsx") <> "" Then
        if MsgBox(existingFilePrompt, vbQuestion + vbYesNo) = vbNo
            Cancel = True
        Else
            Cancel = False
    Else
        Cancel = false
    endif

    if Cancel = false then 
        DoCmd.OutputTo acOutputForm, "agencyListTblForm", acFormatXLSX, "F:\EADB\Exports\Agent_List_" & Format(Date, "yyyymmdd") & ".xlsx"
        MsgBox ("Successful Export"), vbInformation, "Transfer Complete"
    End If
End Sub

Open in new window

0
 

Author Comment

by:ferguson_jerald
ID: 40617267
Thanks for the quick reply.  I get a syntax error on the line "if MsgBox(existingFilePrompt, vbQuestion + vbYesNo) = vbNo".
0
 

Author Comment

by:ferguson_jerald
ID: 40617299
Nevermind - I got it.  I just had to make a couple of additional changes.  Thanks!!!

Private Sub btn_export_agencyListTblForm_Click()
    Const existingFilePrompt As String = "This file already exists in folder /X:\EADB\Exports\.  Do you want to overwrite the file?"
    If Dir("X:\EADB\Exports\Agent_List_" & Format(Date, "yyyymmdd") & ".xlsx") <> "" Then
        If MsgBox(existingFilePrompt, vbQuestion + vbYesNo) = vbNo Then
            Cancel = True
        Else
            Cancel = False
        End If
    Else
        Cancel = False
    End If

    If Cancel = False Then
        DoCmd.OutputTo acOutputForm, "agencyListTblForm", acFormatXLSX, "X:\EADB\Exports\Agent_List_" & Format(Date, "yyyymmdd") & ".xlsx"
        MsgBox ("Successful Export"), vbInformation, "Transfer Complete"
    End If
End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40617305
glad to help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now