Solved

MS Access VBA syntax help with message box.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 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

16 Experts available now in Live!

Get 1:1 Help Now