Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access VBA syntax help with message box.

Posted on 2015-02-18
4
Medium Priority
?
140 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 49

Accepted Solution

by:
Dale Fye earned 2000 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 49

Expert Comment

by:Dale Fye
ID: 40617305
glad to help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

971 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