Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Access VBA syntax help with message box.

Posted on 2015-02-18
4
Medium Priority
?
138 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 48

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 48

Expert Comment

by:Dale Fye
ID: 40617305
glad to help
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

722 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