Query Tables (QueryTables) in Excel 2010 with VBA with VBA creating many connections

前端 未结 8 727
北荒
北荒 2021-02-06 12:49

I\'m following code I found on another site. Here\'s the basics of my code:

Dim SQL As String
Dim connString As String

connString = \"ODBC;DSN=DB01;UID=;PWD=;Da         


        
8条回答
  •  你的背包
    2021-02-06 13:31

    Still relevant years later...battling the same issue and this is the most helpful thread out there. My situation is a variant of the above and I will add my solution when I find it.

    I am using an Access database for my data source and establish a querytable on a new sheet. I then add two more new sheets and try to establish a querytable using the same connection on each of them, but to a different Access table. The first querytable works just fine and I use .QueryTables(1).Delete and setting the querytable object to Nothing to make it disconnected.

    However, the next sheet fails on establishing a new querytable using the same connection, which was not closed. I suspect (and will add the solution below) that I need to drop the connection before deleting the querytable. Rasmus' code above looks like the likely solution.

提交回复
热议问题