PC Review


Reply
Thread Tools Rate Thread

ADO query eating CPU power

 
 
Strobo
Guest
Posts: n/a
 
      8th Mar 2010
Hi,

I'm using a macro to query a database for some data which is then placed
onto the worksheet. I use an ADO connection to achieve this, and I do
remember to close the connections at the end of the function.

The problem is that as soon as opened this excel worksheet is eating 100% of
the CPU (or 50% of my dual core). Initially I thought it was my macro, but
disabling the macro (and even removing the macro code all together) did not
help at all.

In the end, via trial an error, I realised that deleting the actual results
obtained using this code causes the problem...and even if the macro code
itself is removed, the CPU is still used. Only deleting the actual data
retreived frees up the CPU.

Any idea what is behind this?

This is the actual ADO function

Private Sub doADO(ByVal refCell As Range, ByVal destCell As Range)

Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stSQL As String

Const stADO As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;"
& _
"Trusted_Connection=True;" & _
"Initial Catalog=SQL_DATABASE;" & _
"Data Source=SQL_SERVER"

stSQL = "SELECT DISTINCT date1, date2 " & _
"FROM SQL_DB_TABLE AS GD " & _
"WHERE GD.ref='" & refCell.Value & "' ORDER BY GD.date1 DESC"

Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset

With cnt
.CursorLocation = adUseClient
.Open stADO
.CommandTimeout = 0
Set rst = .Execute(stSQL)

destCell.CopyFromRecordset rst, 1

End With

rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing

End Sub

Thanks!
 
Reply With Quote
 
 
 
 
Strobo
Guest
Posts: n/a
 
      8th Mar 2010
Awsome, this is the first time there is a solution to my question at this
forum... except for the fact that I am posting both... :-P

Anyway, problem was the background refreshing of the query, so an easy
fix... a code to remove any queries from the worksheet, leaving behind any
previously fetched data.

Dim qtb As QueryTable
For Each qtb In ActiveSheet.QueryTables
qtb.Delete
Next

"Strobo" wrote:

> Hi,
>
> I'm using a macro to query a database for some data which is then placed
> onto the worksheet. I use an ADO connection to achieve this, and I do
> remember to close the connections at the end of the function.
>
> The problem is that as soon as opened this excel worksheet is eating 100% of
> the CPU (or 50% of my dual core). Initially I thought it was my macro, but
> disabling the macro (and even removing the macro code all together) did not
> help at all.
>
> In the end, via trial an error, I realised that deleting the actual results
> obtained using this code causes the problem...and even if the macro code
> itself is removed, the CPU is still used. Only deleting the actual data
> retreived frees up the CPU.
>
> Any idea what is behind this?
>
> This is the actual ADO function
>
> Private Sub doADO(ByVal refCell As Range, ByVal destCell As Range)
>
> Dim cnt As ADODB.Connection
> Dim rst As ADODB.Recordset
> Dim stSQL As String
>
> Const stADO As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;"
> & _
> "Trusted_Connection=True;" & _
> "Initial Catalog=SQL_DATABASE;" & _
> "Data Source=SQL_SERVER"
>
> stSQL = "SELECT DISTINCT date1, date2 " & _
> "FROM SQL_DB_TABLE AS GD " & _
> "WHERE GD.ref='" & refCell.Value & "' ORDER BY GD.date1 DESC"
>
> Set cnt = New ADODB.Connection
> Set rst = New ADODB.Recordset
>
> With cnt
> .CursorLocation = adUseClient
> .Open stADO
> .CommandTimeout = 0
> Set rst = .Execute(stSQL)
>
> destCell.CopyFromRecordset rst, 1
>
> End With
>
> rst.Close
> cnt.Close
> Set rst = Nothing
> Set cnt = Nothing
>
> End Sub
>
> Thanks!

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Query power consumption Marc Gravell Microsoft C# .NET 3 7th Dec 2006 01:15 PM
Power / Battery query Ian Windows XP Basics 8 11th Jan 2005 12:45 PM
Power point query =?Utf-8?B?UGV0ZXJH?= Microsoft Powerpoint 7 21st Nov 2003 03:48 PM
Power Options query Slobodan Brcin Windows XP Embedded 3 20th Nov 2003 10:53 PM
Power LED wont go off query Ray DIY PC 6 1st Nov 2003 01:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:36 AM.