473,386 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

count rows then save those rows into excel file with # of rows in name

1
Hi Need help with my code. I have an access query with 1.6 million rows. I need to count 4000 rows then move those rows to an excel spreadsheet then count the next 4000 rows and move to a new excel spreadsheet and continue until all the rows are in different excel spreadsheets.

My looping / counting code is pulling in all of the rows instead of just the 4,000 thus creating an error because excel cannot handle that many rows in one sheet.

Does anybody have some successful looping/ counting then saving code I could see?

Thank you in advance for any time and effort I receive.

Here is my code.....
Expand|Select|Wrap|Line Numbers
  1. Sub Export2Excel()
  2.  
  3.     Dim db As DAO.Database
  4.     Dim rs As DAO.Recordset
  5.     'Dim fileName As Object
  6.     Dim x1APP As Excel.Application
  7.     'Dim objWkb As Workbook
  8.     'Dim objSht As Worksheet
  9.     Dim i As Integer
  10.     Dim j As Integer
  11.     Dim Counter As Integer
  12.     'Dim objFSO As String
  13.     'Dim objFile As String
  14.  
  15.     '1)Identify the database and query
  16.     Set db = CurrentDb
  17.     Set rs = db.OpenRecordset("Pinterest_Query", dbOpenDynaset)
  18.  
  19.     '2)check for records in query
  20.     If rs.EOF And rs.BOF Then
  21.     MsgBox "Query or SQL returned no records."
  22.     Exit Function
  23.     End If
  24.  
  25.     '3)Clear previous contents
  26.     Dim xlApp As Object
  27.     Set xlApp = CreateObject("Excel.Application")
  28.     With xlApp
  29.         .Visible = True
  30.         .Workbooks.Add
  31.         .Sheets("Sheet1").Select
  32.  
  33.     '4)Add column headings
  34.      For i = 1 To rs.Fields.Count
  35.             xlApp.ActiveSheet.Cells(1, i).Value = rs.Fields(i - 1).Name
  36.         Next i
  37.         xlApp.Cells.EntireColumn.AutoFit
  38.  
  39.     '5) Find Number of records in recordset
  40.         Counter = rs.RecordCount
  41.  
  42.     '6) Loop through rows to move to a temp file for export to excel
  43.  
  44.  
  45.          For i = 1 To Int(Counter / 5) + 1
  46.             For j = 1 To 5
  47.                 If Not rs.EOF Then
  48.                 ActiveSheet.Range("a2").CopyFromRecordset rs
  49.                 rs.MoveNext
  50.                 End If
  51.             Next
  52.         Next
  53.  
  54.     End With
Mar 20 '14 #1
1 1455
zmbd
5,501 Expert Mod 4TB
More than likely it's because of these two lines

Set rs = db.OpenRecordset("Pinterest_Query", dbOpenDynaset)

ActiveSheet.Range("a2").CopyFromRecordset rs

Your record set, rs, is what is defined in the "Pinterest_Query"; therefor, if that is pulling the 1.6 million data records, then that is what you are dumping to the worksheet.

Hopefully you are using ACC2010 or newer so that you can use the TempVars collection and a stored query.

Set the query to return your records with a where clause that uses the between verb and the TempVars set by your code based on the record count chunked as you desire.

because this method hasn't been used a lot a simple example:


For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblpuke1.id
  2.     , tblpuke1.puke
  3.     , tblpuke1.puke2
  4. FROM tblpuke1
  5. WHERE (((tblpuke1.id) Between 
  6.             [TempVars]![ztvar_start] 
  7.         And 
  8.             [TempVars]![ztvar_end]));
So if you were to run this as is, nothing would be returned because the [id] is a primary key and neither of the conditionals are set (they are null) so there are no matching records.
Now, in VBA use the tempvars.add method you can set the values...
In this case, I'll use the immediate window (cool hey)
Expand|Select|Wrap|Line Numbers
  1. tempvars.Add name:= "ztvar_start", value:= 1
  2. ?tempvars!ztvar_start
  3.  1 
  4. tempvars.Add name:= "ztvar_end", value:= 3
  5. ?tempvars!ztvar_end
  6.  3 
  7.  
And now the record set only returns the records that match that criteria.

Now, the tempvars collection is persistent, that is, they're "sticky" and have to be cleared either via code or closing down the current Access instance (close the program). So, once again via the immediate window:
Expand|Select|Wrap|Line Numbers
  1. tempvars.RemoveAll
  2. ?tempvars!ztvar_start
  3. Null
  4. ?tempvars!ztvar_end
  5. Null
I could have removed each value seperately - but that's too much work.

Now you should be able to take this information and setup your code to select the correct records.

Rewrite your code and let us know how you make out with your project.
(^_^)
Mar 26 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Luis Esteban Valencia | last post by:
Hello Everyone, Iam an intermediate ASP.Net programmer and iam facing a challenging task. I have a table in MS-SQL server database called 'Members'. The table has following fields... ...
0
by: =?Utf-8?B?Tml5YXpp?= | last post by:
Hi all, Does anyone know how to embed the Excel file in VB.NET 2005 Resources? Here is what I am doing at the moment: 1) In my VB.NET 2005 project code I load the excel with it path into...
2
by: rwiegel | last post by:
I'm trying to read rows from an Excel file and display them in an ASP.NET DataGridview. I am using C# for the code file. I am using OleDb to read from the Excel file. The columns that contain...
7
by: shantanu | last post by:
Hi all i am trying to save a excel file as a tab delimited text file. can i do it some how? i using the code _sheet1.SaveAs(@"C:\PRP \PRPTemp.txt",Excel.XlFileFormat.xlTextWindows...
1
by: neehakale | last post by:
If anybody of you knows how to count the no of rows in the excel file then pls tel me....
2
by: Anitha022 | last post by:
hai friends, i am new c#, i am doing project in c#, my work is i want to import an excel file and display the file in notepad that work is finished .... now i want to interchange the values (ie...
1
roswara
by: roswara | last post by:
Dear all, Currently, I am working on a project to make a web-based application using ASP 2.0 and C#. This application will ask user to input for an excel file which has graphs in it. Then the...
1
by: DennisBetten | last post by:
First of all, I need to give some credit to Mahesh Chand for providing me with an excellent basis to export data to excel. What does this code do: As the title says, this code is capable of...
4
by: Rodthemod | last post by:
Hi All I wish to send excel files to other company staff - but save the file as values (so not showing all my formulae links etc ) Please help Thanks in advance Kind regards
0
by: ndedhia1 | last post by:
Hi, I need help updating my jtextarea. I am reading rows from an excel file and after it is done writing to the jtextarea, the excel file is rewritten with the old rows plus new rows and then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.