Microsoft KB Archive/319998

= BUG: Memory leak occurs when you query an open Excel worksheet by using ActiveX Data Objects (ADO) =

Article ID: 319998

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition
 * Microsoft OLE DB Provider for Jet 4.0
 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 2002 Standard Edition

-



This article was previously published under Q319998



SYMPTOMS
When you retrieve a Microsoft ActiveX Data Objects (ADO) Recordset from an Excel worksheet that is open in Excel, a memory leak occurs in the Excel process. Repeated queries may eventually cause Excel to run out of memory and raise an error, or cause Excel to stop responding.



RESOLUTION
The memory used by the ADO queries cannot be reclaimed by closing and releasing the ADO objects. The only way to release the memory is to quit Excel.

If possible, query the Excel worksheet only while the file is not open in Excel.

If the worksheet must remain open (for example, to allow dynamic recalculation of worksheet values on an ongoing basis) use one of the following methods to work around the behavior:

Method 1
 Use the SELECT INTO syntax of the Jet OLE DB Provider to export the Excel data to a new worksheet. For additional information about using the SELECT INTO syntax to export data, click the following article number to view the article in the Microsoft Knowledge Base:

295646 How To Transfer Data from ADO Data Source to Excel with ADO



Method 2

 * Use the SaveCopyAs method of the Workbook object in the Excel object model to programmatically save the open Excel file under a new name. You can then query the copy of the file that you previously saved under a new name from the ADO application.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce the Behavior
 Create or locate an Excel worksheet with rows and columns of data that can be queried using ADO. For example, you can use Excel to import the Customers table from the sample Northwind Access database. In Visual Basic 6.0, create a Standard EXE project. By default, Form1 is created. Set a reference to ADO.  Add a module to the project, and then enter the following declarations for application programming interface (API) calls that allow you to check memory usage using Performance Monitor counters: Option Explicit

'Performance monitor functions for Visual Basic from PDH.DLL Declare Function PdhVbOpenQuery Lib &quot;pdh.dll&quot; _ (ByRef QueryHandle As Long) As Long Declare Function PdhCloseQuery Lib &quot;pdh.dll&quot; _ (ByVal QueryHandle As Long) As Long Declare Function PdhVbAddCounter Lib &quot;pdh.dll&quot; _ (ByVal QueryHandle As Long, ByVal CounterPath As String, _   ByRef CounterHandle As Long) As Long Declare Function PdhRemoveCounter Lib &quot;pdh.dll&quot; _ (ByVal CounterHandle As Long) As Long Declare Function PdhCollectQueryData Lib &quot;pdh.dll&quot; _ (ByVal QueryHandle As Long) As Long Declare Function PdhVbGetDoubleCounterValue Lib &quot;pdh.dll&quot; _ (ByVal CounterHandle As Long, ByRef CounterStatus As Long) As Double Declare Function PdhVbIsGoodStatus Lib &quot;pdh.dll&quot; _ (ByVal StatusValue As Long) As Long Declare Function PdhVbGetOneCounterPath Lib &quot;pdh.dll&quot; _ (ByVal PathString As String, ByVal PathLength As Long, _   ByVal DetailLevel As Long, ByVal CaptionString As String) As Long Declare Function PdhVbCreateCounterPathList Lib &quot;pdh.dll&quot; _ (ByVal DetailLevel As Long, ByVal CaptionString As String) As Long Declare Function PdhVbGetCounterPathFromList Lib &quot;pdh.dll&quot; _ (ByVal Index As Long, ByVal Buffer As String, _   ByVal BufferLength As Long) As Long Declare Function PdhVbGetCounterPathElements Lib &quot;pdh.dll&quot; _ (ByVal PathString As String, ByVal MachineName As String, _   ByVal ObjectName As String, ByVal InstanceName As String, _    ByVal ParentInstance As String, ByVal CounterName As String, _    ByVal BufferSize As Long) As Long   Add a command button to Form1, and then insert the following code in the Click event. This code repeatedly queries an Excel file and displays memory usage in the Debug (Immediate) window. Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim i As Integer Dim max As Integer Dim r As Long Dim hPDHQuery As Long           'Handle to performance monitor query Dim hPDHCounter As Long         'Handle to performance monitor counter Dim strCounterPath              'Path to performance monitor counter Dim lngCounterStatus As Long    'Status of counter when checked Dim dblPrivateBytes As Double   'Value of counter when checked Set cn = New ADODB.Connection 'Jet connection string. cn.Open &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot; & _ &quot;Data Source=&quot; & App.Path & &quot;\test.xls;&quot; & _ &quot;Extended Properties=Excel 8.0&quot; 'Initialize PDH query object. r = PdhVbOpenQuery(hPDHQuery) 'Initialize counter. 'Edit the value of the &quot;strCounterPath&quot; variable. strCounterPath = &quot;\\ \Process(Excel)\Private Bytes&quot; r = PdhVbAddCounter(hPDHQuery, strCounterPath, hPDHCounter) 'Gather data. r = PdhCollectQueryData(hPDHQuery) 'Get counter value and process data. dblPrivateBytes = PdhVbGetDoubleCounterValue(hPDHCounter, _       lngCounterStatus) If PdhVbIsGoodStatus(lngCounterStatus) Then Debug.Print &quot;Memory used by Excel: &quot; & CLng(dblPrivateBytes) Else Debug.Print &quot;Invalid data.&quot; End If 'Edit the value of the &quot;max&quot; variable. max = 100 ReDim alngPrivateBytes(max) For i = 1 To max Set rs = New ADODB.Recordset rs.CursorLocation = adUseClient 'Edit the query to reflect the name of the worksheet. rs.Open &quot;SELECT * FROM [Customers$]&quot;, cn       Do Until rs.EOF rs.MoveNext Loop rs.Close Set rs = Nothing r = PdhCollectQueryData(hPDHQuery) 'Get counter value and process data. dblPrivateBytes = PdhVbGetDoubleCounterValue(hPDHCounter, _           lngCounterStatus) 'Process data. If lngCounterStatus = 0 Then Debug.Print &quot;Memory used by Excel: &quot; & CLng(dblPrivateBytes) alngPrivateBytes(i) = CLng(dblPrivateBytes) Else Debug.Print &quot;Invalid data.&quot; alngPrivateBytes(0) = 0 End If   Next cn.Close Set cn = Nothing Debug.Print &quot;Total increase for &quot; & max & &quot; iterations: &quot; & _ (CStr(alngPrivateBytes(max) - alngPrivateBytes(0))) Debug.Print &quot;Average increase per iteration: &quot; & _ (CStr((alngPrivateBytes(max) - alngPrivateBytes(0))) / max) 'Clean up. r = PdhRemoveCounter(hPDHCounter) r = PdhCloseQuery(hPDHQuery) </li> Make the following changes in the code:

 Edit the Jet connection string to reflect the location of the Excel test file.

Note: It is commented as follows: 'Jet connection string.</li> Edit the value of the &quot;strCounterPath&quot; variable to use the local workstation name.

Note: It is commented as follows: 'Edit the value of the &quot;strCounterPath&quot; variable.</li> Edit the query to reflect the name of the worksheet in the file with the test data.

Note: It is commented as follows: 'Edit the query to reflect the name of the worksheet.</li> Edit the value of the &quot;max&quot; variable for the desired number of repetitions.

Note: It is commented as follows: 'Edit the value of the &quot;max&quot; variable.</li></ul> </li> Run the Excel application, and then open the test Excel file.</li> Run the Visual Basic application. Note that there is a steady increase in memory that is used by the Excel process, even though the ADO objects have been closed and released.</li></ol>

<div class="references_section">