Solved

MS Access VBA syntax help with message box.

Posted on 2015-02-18
4
126 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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, when working with VBA, learn some techniques for writing readable and easily maintained code.

776 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