Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / July 2006



Tip: Looking for answers? Try searching our database.

ADO VB6 Extracting a recordset from existing recordset

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Alsop - 07 May 2006 07:18 GMT
I have two recordsets from two databases, both recordsets have identical
fields.

Is there some way to extract a new recordset from each that contains a
subset of the records based on a WHERE clause without going back to the
database itself?

Is there some way to combine the subset of records from each source
recordset into a single recordset in one pass using ADO?

I'm using ADODB in a VB6 application

TIA
bradggel@gmail.com - 08 May 2006 07:15 GMT
use only one recordset to access the two tables by using the JOIN in
your SQL statement.

here's an example:

SELECT Table1.Field1, Table2.Field1
FROM Table1 INNER JOIN Table2 ON Table1.FieldID = Table2.FieldID

the FieldID of the two tables has the same value.

n.a. Im using the access database
Mark Alsop - 09 May 2006 05:23 GMT
Thanks for the reply...

I don't have any problem using joins (inner/outer) when retrieving data from
the database although I try to create these as parameterized queries saved
in the DB.

This issue is this...

I was under the impression that ADO tools provided a way to filter a local
recordset locally a second time for one or more conditions without going
back to the database itself. I already do this using arrays (which seem to
work very fast) but I thought it might be cleaner to do it directly with the
recordsets.
Terry Kreft - 10 May 2006 21:01 GMT
1) Use the filter property.
2 You can create recordsets dynamically so you could:-
   Declare a recordset variable
   Instantiate the recordset
   Loop through the fields collection of one of your recordsets to append
fields to your new recordset
   Then loop through the records and fields of each recordset adding
records to your new recordset.

Alternatively use getrows to put the recordsets into arrays and then iterate
the arrays which might be faster.
Signature


Terry Kreft

> I have two recordsets from two databases, both recordsets have identical
> fields.
[quoted text clipped - 9 lines]
>
> TIA
news.microsoft.com - 12 Jul 2006 11:48 GMT
> 1) Use the filter property.
> 2 You can create recordsets dynamically so you could:-
[quoted text clipped - 8 lines]
> iterate
> the arrays which might be faster.

Actually there is another way that is almost infinitely easier to code, and
is faster as well:

1. Filter recordset to desired subset
2. Persist the filtered recordset to an XML object
3. Open new recordset using XML object as source

' rs is assumed to be an open ADODB.Recordset
'
Dim xml As MSXML2.DOMDocument, rsSubset as ADODB.Recordset
Set xml = New MSXML2.DOMDocument
Set rsSubset = New ADODB.Recordset

rs.Filter = "[your conditions]"
rs.Save xml, adPersistXML ' save to IStream exposed by xml object
rsSubset.Open xml

As opened above, rsSubset is entirely disconnected from rs and its
underlying data.  It can, however, be reconnected, by setting
rsSubset.ActiveConnection.

To combine rows from two recordsets into one, let's assume that xmlDest was
created using the technique above, to include the subset of rows from one of
the source recordsets, and xmlSrc contains the rows from the other source
recordset, that are to be combined.

Dim DestNode As IXMLDOMNode, SrcNode As IXMLDOMNode
Dim SrcNodeList As IXMLDOMNodeList

Set DestNode = xmlDest.selectSingleNode("xml/rs:data")
Set SrcNodeList = xmlSrc.selectNodes("xml/rs:data/z:row")
For Each SrcNode in SrcNodeList
   DestNode.appendChild SrcNode.cloneNode(True)
Next
rsCombined.Open xmlDest

In most cases (read: all cases I have observed), moving rows as XML nodes is
appreciably faster than adding rows to a recordset object, and iterating
fields to copy rows from another -- in part I'm sure to all of the variant
overhead that's skipped.

Note that these source fragments are by no means all-inclusive -- for one
thing, if any inserts/updates/deletes have been performed on a source
recordset, the rows involved will be ignored by the node paths used above.
But if you're interested in manipulating persisted recordsets at the xml
level, it's a fair jumping-in point, at least.

-Mark

> Terry Kreft
>
[quoted text clipped - 11 lines]
>>
>> TIA
Terry Kreft - 12 Jul 2006 14:59 GMT
Ah yes, I have saved to a persisted file and reopened from there before but
had never thought of usiing a domdocument object directly, nice technique.

I wouldn't have thought this would work though, where you had summarised
data that you wanted to update as the summarised field would still be marked
as non-updatable, this is the reason why I would normally do something
similar to that which I posted.

Signature

Terry Kreft

> > 1) Use the filter property.
> > 2 You can create recordsets dynamically so you could:-
[quoted text clipped - 73 lines]
> >>
> >> TIA
Terry Kreft - 13 Jul 2006 08:11 GMT
I've tested this and found this to be the case (not surprisingly).

Still, obviously, as I said before, a nice technique.

Signature

Terry Kreft

<SNIP>

> I wouldn't have thought this would work though, where you had summarised
> data that you wanted to update as the summarised field would still be marked
[quoted text clipped - 84 lines]
> > >>
> > >> TIA
Mark J. McGinty - 30 Jul 2006 09:56 GMT
> I've tested this and found this to be the case (not surprisingly).

True enough, however, it is a trivial matter to change the relevant schema
node[s] to meet your needs:

Select the node "xml/s:Schema/s:ElementType", set its "rs:updatable"
attribute to "true"; this node will have one or more child "s:AttributeType"
nodes (one per field); locate the one[s] you want to make writable and set
the "rs:writeunknown" attribute to "true".

Another example of the "dirty tricks" that are possible: handling SQL Server
timestamp/rowversion (binary(8)) values is a serious PITA on the client
side; instead of dealing with safe arrays, I change the dt:type attribute
from "bin.hex" to "string".  The advantages are many.

Of course by doing this sort of thing, you take responsibility for the
validity of the XML, ADO still has to be able to render it, and it is less
than forgiving if any mistakes are made.  That not withstanding, I've found
it [persistance provider XML, that is] to be quite workable... effectively
it adds a great deal of range to what I can do with ADODB.Recordset.

> Still, obviously, as I said before, a nice technique.

Thanks!  :-)

-Mark

> <SNIP>
>>
[quoted text clipped - 97 lines]
>> > >>
>> > >> TIA
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.