Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

how to SPLIT an ADO recordset ?

can anyone tell me how i can split an ado recordset ? for exmple if i have 1000 records in my recordset how can i split it into 2 recordset of 500 records each ?
[161 byte] By [ranasrule] at [2007-11-11 9:51:20]
# 1 Re: how to SPLIT an ADO recordset ?
Why not simply create two recordsets of 500 records each by using the appropriate SQL query to populate each recordset? Can you provide more information about what you're trying to accomplish?
Phil Weber at 2007-11-11 17:23:39 >
# 2 Re: how to SPLIT an ADO recordset ?
Why not simply create two recordsets of 500 records each by using the appropriate SQL query to populate each recordset? Can you provide more information about what you're trying to accomplish?

thanks for ur reply...iam writing a procedure that exports records to excel...the problem is that excel only allows 65k records per sheet....i need to split the recordset so i can export each recordset to a new sheet
ranasrule at 2007-11-11 17:24:39 >
# 3 Re: how to SPLIT an ADO recordset ?
Is the original data source a SQL Server database? Does the table have a unique column on which you can sort the recordset? If the answer is yes to both questions, you can use SQL's SET ROWCOUNT statement to limit the number of rows returned by your query. After each batch of results (enough to populate a single Excel worksheet), simply execute another query, selecting rows where the unique column's value is greater than the largest value in the previous recordset.
Phil Weber at 2007-11-11 17:25:37 >
# 4 Re: how to SPLIT an ADO recordset ?
yes its a SQL Server database.....and yes there is a creditcard number field that could be used as a key field...thanks for ur reply
ranasrule at 2007-11-11 17:26:42 >
# 5 Re: how to SPLIT an ADO recordset ?
Can you perform the export with SQL directly?

Dim cnn As New ADODB.Connection
Dim strSQL As String
Dim rs As New ADODB.Recordset

cnn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Test Files\Book20.xls;" & _
"Extended Properties=""Excel 8.0;HDR=No"""

strSQL = "SELECT TOP 500 * INTO [Orders] FROM [Orders] IN '' [ODBC;Driver={SQL Server};Server=(local);Database=Northwind;Trusted_Connection=yes] ORDER BY OrderID;"

cnn.Execute strSQL

cnn.Close
Set cnn = Nothing
pclement at 2007-11-11 17:27:38 >
# 6 Re: how to SPLIT an ADO recordset ?
Can you perform the export with SQL directly?

Dim cnn As New ADODB.Connection
Dim strSQL As String
Dim rs As New ADODB.Recordset

cnn.Open _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\Test Files\Book20.xls;" & _
"Extended Properties=""Excel 8.0;HDR=No"""

strSQL = "SELECT TOP 500 * INTO [Orders] FROM [Orders] IN '' [ODBC;Driver={SQL Server};Server=(local);Database=Northwind;Trusted_Connection=yes] ORDER BY OrderID;"

cnn.Execute strSQL

cnn.Close
Set cnn = Nothing

no iam trying to export the dats from the recordset into excel
ranasrule at 2007-11-11 17:28:48 >
# 7 Re: how to SPLIT an ADO recordset ?
Then use Phil's suggestion. You can also use the TOP statement (as in my example to limit the number of rows returned).
pclement at 2007-11-11 17:29:40 >
# 8 Re: how to SPLIT an ADO recordset ?
Then use Phil's suggestion. You can also use the TOP statement (as in my example to limit the number of rows returned).

Is the original data source a SQL Server database? Does the table have a unique column on which you can sort the recordset? If the answer is yes to both questions, you can use SQL's SET ROWCOUNT statement to limit the number of rows returned by your query.
ok one last question...do i have to manually exceute some sort of command in SQL Server to sort the database of it is sorted by default ?
ranasrule at 2007-11-11 17:30:43 >
# 9 Re: how to SPLIT an ADO recordset ?
I don't believe so. From the examples I've seen the data is retrieved from SQL Server as it is stored. In any event, you can use the ORDER BY statement (as in my example) to sort the data if necessary.
pclement at 2007-11-11 17:31:43 >