I am trying to create Lists in Excel based on a Query I create. I am getting a variety of errors, inconsistently with the same code. My research points to this article which describes the problem very well. Microsoft Support Article
The code gives the various errors that are outlined in the article on the line "With wkb.ActiveSheet.ListObjects.Add"
wkb.Queries.Add Name:="qry" & strProcedure, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=" & strDSN & """, ""select * from " & strProcedure & "()"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
With wkb.ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=qry" & strProcedure & ";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & "qry" & strProcedure & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "qry" & strProcedure
' .Refresh BackgroundQuery:=False
End With
The article gives an example of how to reproduce the problem, and how to fix it. But it's not easy to see from their example how I should change my code to avoid the error.
From the article:
Note The error message occurs because the code refers to the method of the cell without preceding the call with the xlSheet object variable.
Stop the project and change the following line of code: xlSheet.Range(Cells(1,1),Cells(10,2)).Value = "Hello"
Change the line of code to resemble the following line of code. xlSheet.Range(xlSheet.Cells(1,1),xlSheet.Cells(10,2)).Value = "Hello"
Run the program again. Notice that you can run the code multiple times without receiving an error message.
Edit: Here is the full code cleaned up. I run this from Access 2019 (365) Note: the excel.quit forces me to enter a file name and save and close the spreadsheet. Even after doing it, and no other Excel windows open, Excel stays in the task manager until I close Access.
Edit 2: As in the article, this code runs fine the first time but fails on subsequent runs with an inconsistent error message.
Public Sub CreateExcelQueryTest()
Dim AppExcel As Excel.Application
Dim wkb As Excel.Workbook
Dim qry As Excel.WorkbookQuery
Dim strProcedure As String
Dim strDSN As String
Set AppExcel = New Excel.Application
strDSN = "INSPIRE33"
strProcedure = "wwcustomers_addresses"
Set wkb = AppExcel.Workbooks.Add
wkb.Queries.Add Name:="qry" & strProcedure, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=" & strDSN & """, ""select * from " & strProcedure & "()"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
wkb.Worksheets.Add.Name = strProcedure
With wkb.ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=qrywwcustomers_addresses;Extended Properties=""""" _
, Destination:=Range("wwcustomers_addresses!$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & "qry" & strProcedure & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "qry" & strProcedure
.Refresh BackgroundQuery:=False
End With
AppExcel.Visible = True
AppExcel.Quit
End Sub
Just now I got an error report from Excel when testing this function. Needless to say I sent MS a 'frown'.
Feedback Type:
Frown (Error)
Error Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack Trace: at Microsoft.Mashup.Client.Excel.NativeExcelFunctionsC2RBase.Microsoft.Mashup.Client.Excel.INativeExcelFunctions.SaveMashupData(IntPtr workbookPointer, String data) at Microsoft.Mashup.Client.Excel.Shim.NativeWorkbookStorageServices.SaveMashupData(IWorkbook workbook, MashupCustomXmlPart data) at Microsoft.Mashup.Client.Excel.Shim.NativeWorkbookStorageServices.NativeWorkbookStorageDeferrer.Release() at Microsoft.Mashup.Client.Excel.Shim.NativeWorkbookStorageServices.Microsoft.Mashup.Client.Excel.Shim.IDeferredStorageInvoker.InvokeDeferredStorageAction(IWorkbook workbook, Action action) at Microsoft.Mashup.Client.Excel.Shim.NativeCoAuthServices.InvokeCoauthAction(IWorkbook workbook, UndoableActionType actionType, Action action) at Microsoft.Mashup.Client.Excel.Shim.NativeCoAuthServices.InvokeCoauthAction(IWorkbookIdentity workbookIdentity, UndoableActionType actionType, Action action) at Microsoft.Mashup.Client.Excel.Fill.FillManager.ForEachFillSessionByWorkbook(Action`1 action) at Microsoft.Mashup.Client.Excel.Fill.PassiveFillManager.UpdateQueriesProgress() at Microsoft.Mashup.Client.Excel.Fill.PassiveFillManager.b__9_0() at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action)
Stack Trace Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Invocation Stack Trace: at Microsoft.Mashup.Host.Document.ExceptionExtensions.GetCurrentInvocationStackTrace() at Microsoft.Mashup.Client.UI.Shared.StackTraceInfo..ctor(String exceptionStackTrace, String invocationStackTrace, String exceptionMessage) at Microsoft.Mashup.Client.UI.Shared.FeedbackErrorInfo..ctor(String message, Exception exception, Nullable`1 stackTraceInfo, String messageDetail) at Microsoft.Mashup.Client.Excel.Native.NativeUserFeedbackServices.RaiseErrorDialog(IWindowHandle activeWindow, IUIHost uiHost, FeedbackPackageInfo feedbackPackageInfo, Exception e, LocalizedString dialogTitle, LocalizedString dialogMessage, Boolean useGDICapture) at Microsoft.Mashup.Client.Excel.Native.NativeUserFeedbackServices.ReportException(IWindowHandle activeWindow, IUIHost uiHost, FeedbackPackageInfo feedbackPackageInfo, Exception e, Boolean useGDICapture) at Microsoft.Mashup.Client.UI.Shared.UnexpectedExceptionHandler.<>c__DisplayClass14_0.b__0() at Microsoft.Mashup.Host.Document.SynchronizationContextExtensions.SendAndMarshalExceptions(SynchronizationContext context, Action callback) at Microsoft.Mashup.Client.UI.Shared.UnexpectedExceptionHandler.HandleException(Exception e) at Microsoft.Mashup.Host.Document.ExceptionHandlerExtensions.HandleExceptions(IExceptionHandler exceptionHandler, Action action) at Microsoft.Mashup.Client.Excel.Fill.PassiveFillManager.OnFillUpdateTimerTick(Object sender, EventArgs eventArgs) at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Supports Premium Content: False
Update OK, I am calling this one sub from the immediate window. It works on the first attempt, I know it gets to the end because I now have a message box that says 'done' But the excel process does not end. If I click the Stop button (blue square) then the Excel process ends and the second attempt does not fail. The code should not still be running at this point. Never seen this before.