?
Solved

Excel Object .CopyFromRecordset method (and ADODB.Recordset.Filters)

Posted on 2002-05-24
7
Medium Priority
?
1,069 Views
Last Modified: 2013-11-25



Hello Experts..

I have a little probel with the CopyFromRecordset method of an Excel object.

Baiscally All I want to do is return all from a table and send to Excel object, usually this is very simple but the twist is that I wish to filter the recordset first. And when I so the CopyFromRecordset seems to disregard the filter and send the whole recordset rather than the filtered records..

If this is not at all possible I guess I'd ashve to somehow fabricate a third recordset and populate that with the records adhering to the filter then pass the third recordset through CopyFromRecordset. But again I'd need help with that.


The code below is the method I use.
Have a look see what you can come up with..






Private Sub ExportReport(DateFrom As String, DateTo As String)
Dim RS As ADODB.Recordset
Dim RS2 As ADODB.Recordset
Dim strSQL As String
Dim clnRS As ADODB.Recordset

'Excel application objects
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add

'Grab all sites on DB
Set RS = OpenRecordSet("Select Distinct(ProductGroup) From tblagentProduct Order by ProductGroup asc")
If RS.RecordCount > 0 Then
    RS.MoveFirst
Else
    'should be data so log error
    Log_Error "No Product Groups Found in TBLAgentProduct", "frmCrossSales-ExportReport"
End If


'For each site grab all info
strSQL = "exec sp_XSalesReport '" & DateFrom & "', '" & DateTo & "'"
Set RS2 = OpenRecordSet(strSQL)

While Not RS.EOF
    RS2.Filter = "ProductGroup = " & RS!ProductGroup
    Set clnRS = RS2.Clone
   
    'Add worksheet
    Set xlSheet = xlBook.Worksheets.Add
    'Name it by Site Name from DB
    xlSheet.Name = RS!ProductGroup
   
    'Make visible and give user control
    xlApp.Visible = True
    xlApp.UserControl = False
   
    'Set titkes for coloumns.(unable to make BOLD for some reason
    xlSheet.Cells(1, 1) = "Method Of Sale"
    xlSheet.Cells(1, 2) = "Product Sold"
    xlSheet.Cells(1, 3) = "Outcome"
    xlSheet.Cells(1, 4) = "Roll Number"
    xlSheet.Cells(1, 5) = "Amount"
    xlSheet.Cells(1, 6) = "Application Decision"
   
    'Main import function that imports data from recordset
    xlSheet.Cells(4, 1).CopyFromRecordset RS2
       
    'Set coloumn widths and tidy up.
    With xlApp
        .Columns("A:f").Select
            .Selection.ColumnWidth = 25
        .Cells(1, 1).Select
    End With
   
    'RS2.Filter = adFilterNone
    RS.MoveNext
Wend

'Give User COntrol
xlApp.UserControl = True


'Hides the first 3 default worksheest created automatically by the Excel app.
'Couldnt find a away to remove those without user interaction so I'm just hiding them..<CDS>
On Error Resume Next
xlApp.Worksheets("Sheet1").Visible = False
xlApp.Worksheets("Sheet2").Visible = False
xlApp.Worksheets("Sheet3").Visible = False

'Free resources
Set RS = Nothing
Set RS2 = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing

End Sub






Well thats it..
As i'm in a good mood successfull source without too much change will result in a 50points bonus..

Madmarlin..
0
Comment
Question by:Madmarlin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Author Comment

by:Madmarlin
ID: 7032092
PS

I am aware that it would be possible to use another DB table to hold the filtered results (Temp Table)then post another RS with the Temp Table records to CopyFromRecordset..

If I can help it I'd prefer not to use this method.

madmarlin
0
 
LVL 44

Expert Comment

by:bruintje
ID: 7032156
Hi Madmarlin,

what about changing the SQL select statement to return the records you want sorted as you need?

:O)Bruintje
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 7032180
No cant do that, but thanks anyway..

Iv'e found out that I can simple create a disconnected recordset,
append the fields,
open and add to it..

then pass the disconnected RS to the OPenFormRecordset method..

A doddle when you think about it..

Thanks Anyway
Madmarlin
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:Madmarlin
ID: 7032185
As per my last comment I'll fabricate a disconnected recordset..

Thanks anyway guys..
Madmarlin ;?)
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7032187
CopyFromRecordset does as you have noted ignore the filter. This is apparently by design. The only work-arounds are to either ensure that you get the correct information the first time as bruintje suggests or you could disconnect the recordset from the data source, delete the offending records and then use that as the source for the copyfromrecordset function.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7818499
Hi Madmarlin,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

Madmarlin, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
==========
DanRollins -- EE database cleanup volunteer
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7909926
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question