简体   繁体   中英

Get table data in Excel 2007 from query in Access 2007

I have an automated process that is mostly run in Access. But, in the middle, it puts some data in Excel to scrub it into the correct form (it's much faster than doing it in Access), and at the end it opens another Excel file and puts data from some Access queries into the Excel file. For these connections from Excel to Access, I accomplished them all by going into Excel and doing Data --> Get External Data --> From Access, then selecting the Access file and the query I want to get the data from and tell Excel to make it into a Table.

So, I do that one time and then I want to be able to run this automated process that simply refreshes the data. To do this refreshing of the data, I do a line like:

Worksheets("Data").Range("A1").ListObject.QueryTable.Refresh _
    BackgroundQuery:=False

The problem is, half the time (and I can't figure out why it does it one time and not another), it says "Do you want to connect to path\filename?" Of course I do, how else would the table refresh? So, this stops the automation. Even if I click Yes, I still can't get it to continue on. If I click Yes, it opens up the Data Link Properties. After I click OK for that, it opens a window titled "Please Enter Microsoft Office Access Database Engine OLE DB Initialization Information". It has info in it, including the path and name of the data source I want to access, but if I click OK, it says, sorry that didn't work, would you like instead to connect to (and then it lists the exact same path and file name it just said didn't work). It repeats the steps I just mentioned, and after that it errors out.

In case it matters, here is the (basic idea) code I use to connect to Excel from Access:

Public Sub ExportToExcel()
    Dim ObjXLApp As Object
    Dim ObjXLBook As Object

    Dim ExcelFilePath As String

    ExcelFilePath = CurrentProject.Path & "\"

    Set ObjXLApp = CreateObject("Excel.Application")
    Set ObjXLBook = ObjXLApp.Workbooks.Open(ExcelFilePath & "filename.xlsm")        

    ObjXLApp.Visible = True

    ' Runs the "DataSetUp" macro in the Excel file.
    ObjXLApp.Run ("DataSetUp")

    ' The DataSetUp macro saves the Excel file

    ' Quit Excel
    ObjXLApp.Quit

    ' Free the memory
    Set ObjXLBook = Nothing
    Set ObjXLApp = Nothing
End Sub

I have no idea how to fix this. Any help would be much appreciated.

This may be happening because your access database is still open from which the new excel file needs to input data back into. The database cannot be open when this takes place, hense the reason why excel errors and asks for another location to connect to.

So, I would work on generating the needed scrubbing via vba inside access probably.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM