0

I have an excel program which writes data to an Access database. In this database, I have a macro which, when run, copies the data onto another excel file. I want to edit the original excel program so that, after it writes the data to the Access database, it will automatically run the macro. Right now, I have it set so that it writes the data to the database, then opens the database, runs the macro and then closes the database - this seems somewhat ineffecient. Here is the macro in the access database:

Sub copy()



Dim strTable As String
Dim strWorksheetPath As String


strWorksheetPath = "FilePath" & "test.xlsx"
strTable = "test"

DoCmd.OutputTo acOutputTable, strTable, acFormatXLSX, strWorksheetPath



End Sub

The relevant code in the program simply opens the database, calls the macro and closes it. If there is a way around this, without opening the database at all it'd be great. Thanks.

i.e. just to make it clear, I'm looking for a way to mimic the above code in excel, so that I don't need to open the access database at all i.e. someway to transfer data from access, to an excel workbook, without using access VBA.

7
  • 1
    You cannot run code which is contained in an Access database unless the database is open in an Access session.
    – HansUp
    Commented Aug 3, 2016 at 15:26
  • Is there any way to copy code from access without opening access? I.e. using Excel-VBA code to mimic the above code?
    – user33484
    Commented Aug 3, 2016 at 15:30
  • No, connecting is fine - but a way without opening Access is what I'm looking for. I've edited my post as requested.
    – user33484
    Commented Aug 3, 2016 at 15:44
  • Can you edit your question & description? It sounds like you want a way to extract the data from Access database without opening the DB and running the macro from Access. On that note: google for "excel vba adodb.connection access database" and see what turns up, you should be able to use ADODB to read the database without opening access. Commented Aug 3, 2016 at 15:51
  • Alternatively, in the other Excel file, you can create a connection to an existing database table, see here. That would avoid the need for the Access macro, and also you most likely wouldn't need to "convert" that macro to Excel VBA, either. Commented Aug 3, 2016 at 15:54

1 Answer 1

2

I think I understand what you're asking, but correct me if I misunderstand.

You can plug this code into a Excel Workbook - add the required reference to Microsoft Office 14.0 Access database engine Object Library (under Tools | References) and change the variables to match your parameters.

You should also add error checking

Option Explicit

Public Sub SaveAccessTableToXL()

    ' Under References, Add This
    ' Microsoft Office 14.0 Access database engine Object Library

    Dim db              As DAO.Database
    Dim rs              As DAO.Recordset

    Dim wb              As Workbook
    Dim ws              As Worksheet
    Dim rge             As Range

    Dim strDatabase         As String
    Dim strTable            As String
    Dim strWorksheetPath    As String

    ' These are variables you need to set before running
    strDatabase = "c:\databases\heatlink\linktest.accdb"
    strWorksheetPath = "c:\databases\heatlink\" & "test.xlsx"
    strTable = "tbl_areas"

    ' Create New Workbook
    Set wb = Workbooks.Add()
    Set ws = wb.Sheets("Sheet1")
    Set rge = ws.Range("A1")

    ' Get Table Data from Database
    Set db = DBEngine.OpenDatabase(strDatabase)
    Set rs = db.OpenRecordset(strTable, dbOpenDynaset, dbReadOnly)

    ' Copy Recordset into Worksheet
    rge.CopyFromRecordset rs
    rs.Close

    ' Save Workbook
    wb.SaveAs strWorksheetPath
    wb.Close

    MsgBox "Workbook Created: " & strWorksheetPath
    Set wb = Nothing
    Set ws = Nothing
    Set rge = Nothing

    Set rs = Nothing
    Set db = Nothing
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.