Solved

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

Posted on 2002-05-24
7
1,007 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

746 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

11 Experts available now in Live!

Get 1:1 Help Now