Solved

VB 6 writing to Excel

Posted on 2001-06-27
18
210 Views
Last Modified: 2008-02-01
I am using a Visual Basic 6 application to read a SQL Server 6.5 database and write to an Excel spreadsheet.  In the application I pull back a recordset which I then filter on different IDs. I attempt to write the filtered recordset directly to Excel using CopyFromRecordset.  The problem is that the result that gets printed to Excel is the unfiltered recordset. Here is a portion of the code....

Set rsDetail = FilterField2(rsTemp)   'Function that filters the original recordset, rsTemp
Set rsDetail2 = rsDetail                          'Tried reassigning the filtered recordset to another recordset
If rsDetail2.EOF Then
    Exit Sub
Else
    rsDetail2.MoveLast
    iRows = rsDetail2.RecordCount
    rsDetail2.MoveFirst
    DetailHead                                              'write a header row
    r = r + 1
    lSumr = r + iRows                                   'get botoom row of insert
    ws.Cells(r, 3).CopyFromRecordset rsDetail2, iRows, 10
0
Comment
Question by:jgaull
  • 5
  • 5
  • 4
  • +2
18 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6231530
If you can filter the recordset when it is returned then you can filter the recordset in the original query. This way the copyrecordset method will work the way you expect it to. If you need help on how to create the appropriate sql statement then I can help with that.
0
 

Author Comment

by:jgaull
ID: 6231606
I can't filter the original recordset.  It is returned from a stored procedure, then I go through a loop to filter on just one id each time through, work with that information, then go to the next.
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6231645
How the function Filter2Field looks like?
0
 

Author Comment

by:jgaull
ID: 6231723
I can't filter the original recordset.  It is returned from a stored procedure, then I go through a loop to filter on just one id each time through, work with that information, then go to the next.
0
 

Author Comment

by:jgaull
ID: 6231734
Public Function FilterField2(rstTemp As ADODB.Recordset) As ADODB.Recordset
Dim strFilter
   ' Set a filter on the specified Recordset object and then open a new Recordset object.
   strFilter = "PoolId = " & lngPoolId
   rstTemp.Filter = strFilter
   Set FilterField2 = rstTemp
End Function
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6231831
Beside can you look if you have truly an sorted recordset
after calling this function ? Because this could be wrong:
strFilter = "PoolId = " & lngPoolId
should be:
strFilter = "PoolId ='" & lngPoolId & "'"
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6231842
Check this with:
rsTemp.RecordCount
Set rsDetail = FilterField2(rsTemp)  
Set rsDetail2 = rsDetail                          
rsDetail2.RecordCount
0
 

Author Comment

by:jgaull
ID: 6231960
The original recordset's count is 7067.  After being filtered it is 13.
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6232128
Thinking you must only close and reopen the rs:
-->
Set rsDetail = FilterField2(rsTemp)  
Set rsDetail2 = rsDetail
rsDetail2.Close
rsDetail2.Open
<--

Same like:
rsDetail2.requery

WoK
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.

 

Author Comment

by:jgaull
ID: 6232603
Closing and reopening the recordset takes too long for it to be useful.  Additionally, it reopens with only one record.  If I change the code to say, 'rsTemp.Filter = "PoolId = " & lngPoolId', rsTemp now has the 13 records, but when I use CopyFromRecordset rsTemp,  it copies the unfiltered recordset.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6232962
I have done some research a couple of months ago to discover that when the recordset is marshaled (from VB to Excel), the filter is ignored. Even a clone and other method failed.
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6234417
Thanks an best wishes go to mircosoft ...

;o)
WoK
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6239705
There isn't a filter property of recordset object?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6239973
There is a filter on the ADO Recordset object but when you use the CopyFromRecordset, the Filter is ignored.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6240146
But, i did read somewhere that copyfromrecordset returns ADO recordset on office 2000...
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 100 total points
ID: 6240158
CopyFromRecordset (as its name says) takes a recordset (ignoring the filter if any) and copy it to a range on an Excel sheet.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6241456
Well, i can't post code here since it is in spanish, but do this:

Open your MSDN and goto search and type:

filter AND dao

If everything is OK, the first or second entry is an example of filtering a recordset, title:( i hope)
Filter property example.

The header should reads like this:

This example uses filter property to creates a new recordset object from an existing recordset based
on a specific condition. It needs FieldFilter function to execute this procedure.....

(FiledFilter is provided)

Good luck!
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 6263100
jgaull, still around?
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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 55
Adding to a VBA? 6 63
How to incorporate an error traping routing to existing code in VB6 11 39
Exit a vb6 apps when a calling it apps closes 15 43
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

22 Experts available now in Live!

Get 1:1 Help Now