Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

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




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
Madmarlin
Asked:
Madmarlin
1 Solution
 
MadmarlinAuthor Commented:
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
 
bruintjeCommented:
Hi Madmarlin,

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

:O)Bruintje
0
 
MadmarlinAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MadmarlinAuthor Commented:
As per my last comment I'll fabricate a disconnected recordset..

Thanks anyway guys..
Madmarlin ;?)
0
 
TimCotteeCommented:
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
 
DanRollinsCommented:
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
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now