ACCESS: Macro to export query results to specific tab in Excel spreadsheet, whilst overwriting existing contents.

Anonymous
2011-01-18T20:33:05+00:00

Hi,

I need to write a macro for access, that will export the contents of a query to a specific tab on an excel spreadsheet, whilst overwriting the exiting contents. Is this possible?

I have tried various ways, but it always wants to overwrite the excel file altogether!

Thank you.

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

47 answers

Sort by: Newest
  1. Anonymous
    2011-01-19T20:42:48+00:00

    Hi,

    I need to write a macro for access, that will export the contents of a query to a specific tab on an excel spreadsheet, whilst overwriting the exiting contents. I will need the code to work on various queries/spreadsheets independantly.

    Bob Larson responded to me on a previous thread, and gave me advice, but when I try to replicate the code for another query/spreadsheet, the macro fails.

    I wondered if anyone can help me?

    Thank you.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2011-01-19T19:14:50+00:00

    Hi Bob,

    Using the code you gave me, I can export one query to one excel spreadsheet.

    I need to do this on several different queries/spreadsheets, but all independantly.

    I have tried just copying the code again, and then setting up a new call, but this does not work.

    Any ideas on how I can do this?

    Many thanks.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-01-18T21:56:02+00:00

    I'm sorry, I misundestood you. It now works, however is there anyway of stopping it coming up with this error at the end of the process?

    3021 : No current record

    Thank you.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-01-18T21:41:55+00:00

    Okay, first off.  I TOLD you NOT TO MODIFY THE FUNCTION you got from my website AT ALL. Do NOT CHANGE ANYTHING.  Just copy and paste it into a standard module.

    You then CALL the function like this from a DIFFERENT EVENT or macro:

    Call SendTQ2XLWbSheet("Jack Powell Completions", "Completions", "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

    Again DO NOT MESS WITH THE FUNCTION AT ALL.  It is a generic function written to handle anything you throw at it, but you must CALL the function and PASS the parameters, you do not modify the function.


    Bob Larson, Former Access MVP (2008-2010) http://www.btabdevelopment.com (free Access tools, tutorials, and samples)

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2011-01-18T21:29:55+00:00

    I'm in a VBA module. I've probably got it all wrong but here goes:

    Public Function SendTQ2XLWbSheet("Jack Powell Completions" As String, "Completions" As String, "O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm" As String)

        Dim rst As DAO.Recordset

        Dim ApXL As Object

        Dim xlWBk As Object

        Dim xlWSh As Object

        Dim fld As Field

        Dim strPath As String

        Const xlCenter As Long = -4108

        Const xlBottom As Long = -4107

        On Error GoTo err_handler

        strPath = strFilePath

        Set rst = CurrentDb.OpenRecordset("Jack Powell Completions")

        Set ApXL = CreateObject("Excel.Application")

        Set xlWBk = ApXL.Workbooks.Open("O:\Public\Public Users Folders\Jack Powell\Jack Powell Target Sheet 2011.xlsm")

        ApXL.Visible = True

        Set xlWSh = xlWBk.Worksheets("Completions")

        xlWSh.Range("A1").Select

        For Each fld In rst.Fields

            ApXL.ActiveCell = fld.Name

            ApXL.ActiveCell.Offset(0, 1).Select

        Next

        rst.MoveFirst

        xlWSh.Range("A2").CopyFromRecordset rst

        xlWSh.Range("1:1").Select

    End Sub

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments