PC Review


Reply
Thread Tools Rate Thread

CopyFromRecordSet vs QuerrTables

 
 
Justin Dearing
Guest
Posts: n/a
 
      11th Sep 2005
I have a question of best practices. I needed to be able to insert the
results of a SQL query into an excel worksheet. I figured out 2 ways to
do this. The fisrt method is creating, refreshing, and deleting a
querytable. The second is to use copyfromrecordset. Both work equally
well from my perspective. However, I'm not sure which method is better.
I've included both functions I wrote. There class emember function and
both reference a private member ADODB.Connection object called cn.
Also, any memory or performance benifits to using querytable with
something besides an ADODB.Recordset object for the querytable?

Public Sub createQueryTable(ByRef sht As Worksheet, ByVal strSQL As
String)
Dim qt As QueryTable
Dim rs As Recordset

On Error Resume Next
cn.Execute "drop view qryTMRC"
cn.Execute "create view qryTMRC as " & strSQL
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical
End If
Set rs = cn.Execute(CommandText:="qryTMRC", Options:=adCmdTable)
Set qt = sht.QueryTables.Add(rs, sht.Application.ActiveCell)
qt.Refresh
qt.Delete
cn.Execute "drop view qryTMRC"
On Error GoTo 0
End Sub

Public Sub getQueryAsRange(ByRef sht As Worksheet, ByVal strSQL As
String)
Dim rs As Recordset

On Error Resume Next
cn.Execute "drop view qryTMRC"
cn.Execute "create view qryTMRC as " & strSQL
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical
End If
Set rs = cn.Execute(CommandText:="qryTMRC", Options:=adCmdTable)
sht.Application.ActiveCell.CopyFromRecordset rs
cn.Execute "drop view qryTMRC"
On Error GoTo 0
End Sub

 
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
CopyFromRecordset =?Utf-8?B?Sm9zZSBQZXJkaWdhbw==?= Microsoft Access Form Coding 3 16th Sep 2006 08:35 PM
SQL CopyFromRecordset =?Utf-8?B?QnJldA==?= Microsoft Excel Programming 2 23rd Jul 2006 06:56 AM
CopyfromRecordset MattShoreson Microsoft Excel Programming 2 15th Mar 2006 01:36 PM
CopyFromRecordset does nothing Hafeez Microsoft Excel Programming 2 13th Aug 2004 07:20 PM
CopyFromRecordset Microsoft Excel Misc 0 18th Dec 2003 08:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:10 PM.