Solved

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

Posted on 2002-05-24
7
1,013 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

912 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now