ADO Stream Object sorts the field order - why?

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,912
Office Version
  1. 365
Platform
  1. Windows
Hi All

A little while back Andrew showed me that using the ADO Stream object I could preserve the recordset filter. I am using this:

Code:
Public Function FilterRecordSet(ByVal objRecordSet As ADODB.RecordSet, ByVal strFilterText As String) As ADODB.RecordSet
    Dim objStream As ADODB.Stream
    
    On Error Resume Next
        objRecordSet.Filter = strFilterText
    On Error GoTo 0
    
    Set objStream = New ADODB.Stream
    With objRecordSet
        .Save objStream, adPersistXML
        .Close
        .Open objStream
    End With
    
    Set FilterRecordSet = objRecordSet
End Function

This seems to order the fields alphabetically. Do you know how to avoid this, or do you know an alternative way to use the filter method on a recordset and dump the filtered results (without looping through all records)?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
It doesn't change the order for me.
 
Upvote 0
Does it make any difference for you if you save it to a file using adPersistADTG and then open that?
 
Upvote 0
Does it make any difference for you if you save it to a file using adPersistADTG and then open that?

Nope, afraid not. So it doesn't change the order of the fields for you? I wonder what is different about yours compared with mine. I create the recordset using the Recordset Open method.

Code:
Public Function RunScript(ByVal strSql As String) As ADODB.RecordSet
    Set RunScript = New ADODB.RecordSet
    Call RunScript.Open(strSql, Me.DBconnection, adOpenKeyset, adLockOptimistic)
End Function

The SQL string is build up based on the selection of fields that the user chooses from a listbox on a userform. Grabbing the lot from one of the tables would look like:

Code:
SELECT [COUNTRY], [PERIOD], [KPI_NAME], [KPI_DESCRIPTION], [UNIT_OF_MEASURE], [RECORD_TYPE], [RESPONSIBLE_OWNER], [COMMENT], [SECTOR], [DIVISION], [BUSINESS_UNIT], [COMMODITY_CATEGORY], [MAJOR_COMMODITY], [SUB_COMMODITY], [UPLOAD_USER], [DATE_CREATED], [LAST_UPDATED], [FILE_URL], Avg(IIf([FUNCTION]="Average",[RESULT],0)) AS AVERAGE_RESULT, Sum(IIf([FUNCTION]="Sum",[RESULT],0)) AS SUM_RESULT FROM KPI_TBL GROUP BY [COUNTRY], [PERIOD], [KPI_NAME], [KPI_DESCRIPTION], [UNIT_OF_MEASURE], [RECORD_TYPE], [RESPONSIBLE_OWNER], [COMMENT], [SECTOR], [DIVISION], [BUSINESS_UNIT], [COMMODITY_CATEGORY], [MAJOR_COMMODITY], [SUB_COMMODITY], [UPLOAD_USER], [DATE_CREATED], [LAST_UPDATED], [FILE_URL];

The user then inputs a filter expression into a textbox (or he/she uses the expression builder). Example of basic expression would be:

Code:
SECTOR='Energy'

The recordset is then converted using the following function:

Code:
Public Function FilterRecordSet(ByVal objRecordSet As ADODB.RecordSet, ByVal strFilterText As String) As ADODB.RecordSet
    Dim objStream As ADODB.Stream
    
    On Error Resume Next
        objRecordSet.Filter = strFilterText
    On Error GoTo 0
    
    Set objStream = New ADODB.Stream
    With objRecordSet
        .Save objStream, adPersistADTG
        .Close
        .Open objStream
    End With
    
    Set FilterRecordSet = objRecordSet
End Function

The recordset gets spat out either into a pivot table or a text file, e;g:

Code:
lngFile = FreeFile
strFile = .tbxFileName.Text
strData = objRecordSet.GetString(adClipString, -1, "|", vbCrLf, vbNull)
Open strFile For Binary Access Write As #lngFile
    Put #lngFile, , strData
Close #lngFile

I don't see that I've done anything unusual anywhere in this project. How is what you are doing different?
 
Last edited:
Upvote 0
Dunno - I'm using ADO2.8 and was testing with an extract from Excel. Absolutely no change in data order, although I was extracting using SELECT *. I'll try selecting the columns in a different order from the actual source data.

Edit: nope, that made no difference to me.
 
Last edited:
Upvote 0
Doh! This is really baffling. I am using same library. My data source however is Access. Although not sure why it should make any difference, but I'll test this from an Excel source. :confused:
 
Upvote 0
Doesn't seem as though the data source file format makes any difference for me. :(

Rory, if I email you a file with the class and an excel source file can you please run a test routine for me and let me know if it sorts the field order?

Pretty please...
 
Upvote 0
Thanks Rory. I just sent it. You just need to save the source file somewhere and then run the routine in the module in the file called 'Example_ADO_Filter_Sort_Issue'.

Edit: Er, mistake. You need to update the DBPath property in the routine to the fullname of the source file.
 
Last edited:
Upvote 0
OK, I'll try and have a look when I get home. Or, possibly more likely, tomorrow morning!
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top