Question

I have an Access DB that has a VBA function that triggers a refresh in an Excel Workbook. The Excel Workbook is fed by a .iqy file that is hitting Oracle OBIEE.

Depending on the network traffic, the time it takes to refresh the Excel workbook varies.

I included a pause(seconds) function elsewhere in my VBA modules that delays further execution within a VBA function, but the problem with this is: when network traffic is fast, I unnessecarily delay the execution of VBA, because the Excel refresh is relatively fast. When the network traffic is slow, the specified number of seconds fed to pause() is not enough, and the program crashes.

Is there a way to get VBA to track the refresh process in Excel, and resume executing right when the process finishes?

My code so far is below:

Function import_tblFromIQY()

    Dim Path As String
    Path = CurrentProject.Path & "\"

    Dim rbFileName As String
    rbFileName = "dataPipeline.xls"

    ' If table tblFromIQY exists, then delete.
        If TableExists("tblFromIQY") = True Then
            DoCmd.DeleteObject acTable, "tblFromIQY"
            Debug.Print ">>> Deleted table tblFromIQY"
        End If

    ' Refresh linked table
        Dim appexcel As Excel.Application
        Dim wr As Excel.Workbook
        Dim wrksheet As Excel.Worksheet

        Set appexcel = CreateObject("Excel.Application")
        Set wr = appexcel.workbooks.Open(Path & rbFileName)
        Set wrksheet = appexcel.worksheets("Main Worksheet")

        wr.RefreshAll

        Pause (420) ' pause for 7 mins. this is approx how long it takes for the file to refresh.

        wrksheet.Rows("1:1").Select
        wrksheet.Rows("1:1").Delete Shift:=xlUp
        wr.CheckCompatibility = False
        wr.Save
        wr.CheckCompatibility = True

        appexcel.Visible = False

        wr.Close

        Set wr = Nothing
        Set appexcel = Nothing


    ' Import Data into tblFromIQY

        DoCmd.TransferSpreadsheet TransferType:=acImport, SpreadsheetType:=acSpreadsheetTypeExcel8, tableName:="tblFromIQY", fileName:=Path & rbFileName, _
            HasFieldNames:=True
        Debug.Print ">>> Imported data to tblFromIQY"

End Function
Was it helpful?

Solution

After a bit of Googling, I found this solution:

Set wrksheet = appexcel.worksheets("Main Worksheet")
wrksheet.Range("A:BN").QueryTable.Refresh BackgroundQuery:=False

Documentation on BackgroundQuery.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top