+ Reply to Thread
Results 1 to 6 of 6

How to query an open ListObject?

  1. #1
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    How to query an open ListObject?

    I want to pull data from a ListObject into VBA using SQL statements. I don't need to pivot. I don't need any front-end display or controls.

    My first thought was ADO, but a memory leak has been reported when querying an open workbook with ADO:

    https://www.access-programmers.co.uk...ordset.313008/
    https://stackoverflow.com/questions/...ource-exceeded
    https://www.msofficeforums.com/excel...ce-script.html
    https://www.decisionmodels.com/memlimitsd.htm

    What options are there to pull data from a ListObject in an open workbook into VBA?

    I'm looking at "Connections" and "Data Model". It seems that creating a "new Connection" is the simplest way to connect to a ListObject, correct? Can i execute a SQL statement against a Connection? Is it necessary to add the table to the Data Model?

    I think i don't need to use a Query object, because i think that's a front-end display component which sits on top of Connections, and therefor just adds more overhead, correct?

    I think i might make use of one of the Power addins, but prefer to use the core features without addins, if possible. Again, seems like unnecessary overhead.

    Is there a different/better/easier way?

    i'm interested in performance, bugs, leaks, and simplicity of code.

    Also asked here-
    https://www.vbforums.com/showthread....ject&p=5538435
    http://www.vbaexpress.com/forum/newt...newthread&f=17
    Last edited by AliGW; 10-03-2021 at 10:50 AM.

  2. #2
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    Re: How to query an open ListObject?

    I'm able to create a WorkbookConnection object pointing to a ListObject. Not sure how to pulll data from it, tho.

    Please Login or Register  to view this content.
    The following fails. It's identical to the above statement, but i tried to use a SQL statement

    Please Login or Register  to view this content.
    Also fails with "!" instead of "$".
    Last edited by johnywhy; 10-03-2021 at 11:11 AM.

  3. #3
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: How to query an open ListObject?

    Sometimes you create a temporary copy of the source file by SaveCopyAs (it will be closed) and use ADO to get data from it. Finally, you delete the copy of the file.

    Artik

  4. #4
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    Re: How to query an open ListObject?

    Quote Originally Posted by Artik View Post
    Sometimes you create a temporary copy of the source file by SaveCopyAs (it will be closed) and use ADO to get data from it. Finally, you delete the copy of the file.
    Thx Artik. For now, i'd like to try to do it without caching to disk.

    I understand the table gets added to the workbook data model, and i understand i can query the data model (hope i don't have to use DAX).

    Works:
    Please Login or Register  to view this content.
    But .... still ADO. Does the data model eliminate the memory leak issue?
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,545

    Re: How to query an open ListObject?

    Unfortunately, I don't know much about it, so I won't be able to help. I struggled with data in a data model once, but without much results.
    From the old technology, as far as my memory is correct, DAO does not make a memory leak on an open workbook.

    Artik

  6. #6
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    Re: How to query an open ListObject?

    I just ran the test code from the original leak article. The original code failed on the following line:

    Please Login or Register  to view this content.
    The reason is:
    The Microsoft OLE DB Provider for Jet and the Jet ODBC driver are available in 32-bit versions only. You can't run them in 64 bit mode.
    https://www.connectionstrings.com/us...-environments/
    This affects me because i'm on Excel 2016 64 bit. The 64-bit solution is:

    With Office 2010, there are new drivers, the 2010 Office System Driver, which will be provided in both 32-bit and 64-bit versions. You can use these drivers to let your application connect to Access, Excel and text files in a 64 bit environment utilizing the new 64-bit drivers. The provider name is "Microsoft.ACE.OLEDB.12.0".
    The driver is already installed, so i just need to use the ACE connection string.
    Please Login or Register  to view this content.
    My data contained 1 million+ records. While running, i watched Excel in Task manager (i didn't use the memory counters from the original MS article). On each loop, Excel memory consumption varied from about 200 MB to about 400 MB. Never grows beyond 400 MB, never crashes, always jumps back to 250 MB on each loop. There was no steady increase, as you'd see with a leak.

    Screenshot (95).png

    Screenshot (96).jpg

    So i think i can conclude that the memory leak doesn't affect 64-bit Excel with the 64-bit driver.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Delete listobject with query table based on connection name
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-03-2024, 07:12 PM
  2. Connect Listobject to QueryTable and To Power Query
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2021, 06:36 AM
  3. Web Query as Listobject from CSV table
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-18-2021, 04:05 AM
  4. Reading CSV into listobject query table
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-08-2021, 02:19 AM
  5. [SOLVED] Create listobject from external query
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-26-2018, 03:18 AM
  6. ListObject SQL query not setting NumberFormat
    By cheetah05 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-22-2014, 06:08 AM
  7. [SOLVED] Converting Query Table to ListObject
    By x10sion in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-04-2013, 06:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1