Results 1 to 5 of 5

Thread: Export Button

  1. #1
    Join Date
    Mar 2004
    Posts
    10

    Unanswered: Export Button

    What would be the VBA code behind a form button that exports data from a query or table to an xls or a csv document?

    Thanks.

  2. #2
    Join Date
    Mar 2004
    Posts
    82

    Re: Export Button

    Originally posted by dataMonkey
    What would be the VBA code behind a form button that exports data from a query or table to an xls or a csv document?

    Thanks.

    Hi,

    There were two separate posts on this awhile ago; I am pasting them both below, credits to the individual authors (I copy and paste messages of interest to me in a Notepad, so don't have author information).

    I tried the first one, but it didn't work for some reason and I moved on to other pressing matters. If you are able to get it to work, I'd appreciate an update.

    Here they are:

    Excel output from Access:
    DoCmd.OutputTo acTable, "yourTableName", "MicrosoftExcel(*.xls)", _
    "PATH\NameOfReport.xls", False, ""

    Or try:

    Private Sub PrintExcel(ReportName As String)
    Dim rstParameters As New ADODB.Recordset
    Dim strPath As String

    DoCmd.OpenForm "frmOpen"
    strSQL = "SELECT P_Value FROM T_Parameter WHERE P_ID = 'PATH EXCEL REPORTS'"
    rstPara.Open strSQL, CurrentProject.Connection
    strPath = rstParameters.Fields(0)
    rstParameters.Close
    DoCmd.OutputTo acOutputReport, ReportName, acFormatXLS, _
    strPath & "ListOfContacts.xls", True
    DoCmd.Close acForm, "frmOpen"

    Exit Sub
    ErrorHandler:
    msgbox "Error in PrintExcel" & err.Description

    End Sub


    Jabo

  3. #3
    Join Date
    Mar 2004
    Posts
    10
    I tried the first bit of code:

    DoCmd.OutputTo acOutputQuery, "currenttasks", "MicrosoftExcel(*.xls)", "c:\curtasks.xls", False


    when i run it in the Immediate section of the Visual Basic window it works!

    but when i push the button it does not. Here is the full code:

    Private Sub cmdExportCurrent_Click()

    DoCmd.OutputTo acOutputQuery, "currenttasks", "MicrosoftExcel(*.xls)", "c:\curtasks.xls", False


    End Sub

    Any ideas why the code would work but the button does not. (and yes the button is named the same as the VB code).

  4. #4
    Join Date
    Mar 2004
    Posts
    10
    Oops.. never mind i got it to work..


    For some reason on the button properties "[event procedure]" was not selected. Once it was selected it work perfectly.

    Thanks for your help.

  5. #5
    Join Date
    Mar 2004
    Posts
    82
    Originally posted by dataMonkey
    Oops.. never mind i got it to work..


    For some reason on the button properties "[event procedure]" was not selected. Once it was selected it work perfectly.

    Thanks for your help.


    Great! Glad it worked. I'll try it tomorrow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •