473,386 Members | 1,801 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.

Insert row excel after each record exported from access to excel

I would like to export a recordset from access to excel but after each record is exported I want to insert a new row.
The first recordset does this ok. However, the second recordset onwards does not not export line excelsheet.Cells(9, 2) = rsschedulesrecords.Fields(2).Value.

Below is my code. Please help?

Private Sub CreateDailyRoster(rsschedulesrecords As DAO.Recordset)

Dim excelapp As New Excel.Application
Dim excelfile As New Excel.Workbook
Dim excelsheet As New Excel.Worksheet
Dim savefilepath As String
Dim tempi As Integer
Dim currdt As Date

currdt = txtStartDate.Value
tempi = 50 ''where remarks begin

Set excelfile = excelapp.Workbooks.Open(CurrentProject.Path & "\Template05.xls")
Set excelsheet = excelfile.Worksheets.Item(1)

excelsheet.Range("A1") = CDate(txtStartDate.Value)
excelsheet.Range("K4") = "" & Format(txtStartDate.Value, "dddd")
excelsheet.Range("P4") = "" & Format(txtStartDate.Value, "dd")
excelsheet.Range("T4") = "" & Format(txtStartDate.Value, "mmmm")
excelsheet.Range("AB4") = "" & Format(txtStartDate.Value, "yyyy")


If Not (rsschedulesrecords.EOF) Then
rsschedulesrecords.MoveFirst
excelsheet.Range("A8", "L8").Insert
'excelsheet.Range(tempr).Insert
tempi = tempi + 1
rsschedulesrecords.FindFirst (Format(currdt, "dddd") & "=Yes")
While Not (rsschedulesrecords.NoMatch)
excelsheet.Range("A8", "L8").Insert
tempi = tempi + 1
excelsheet.Cells(8, 2) = rsschedulesrecords.Fields(1).Value
excelsheet.Cells(9, 2) = rsschedulesrecords.Fields(2).Value
excelsheet.Cells(8, 4) = rsschedulesrecords.Fields(17).Value
excelsheet.Cells(8, 5) = rsschedulesrecords.Fields(20).Value
excelsheet.Cells(8, 7) = rsschedulesrecords.Fields(23).Value
excelsheet.Cells(8, 8) = rsschedulesrecords.Fields(27).Value
'excelsheet.Cells(8, 13) = ""
'excelsheet.Rows.Insert

If (Not IsNull(rsschedulesrecords.Fields(28).Value) And _
Len(Trim(rsschedulesrecords.Fields(28).Value)) > 0) Then
excelsheet.Range("A" & tempi, "L" & tempi).Insert
excelsheet.Cells(95, 1) = "Remarks/Airline Name " & rsschedulesrecords.Fields(1).Value & _
" " & rsschedulesrecords.Fields(2).Value & _
" " & rsschedulesrecords.Fields(15).Value & _
" : " & rsschedulesrecords.Fields(28).Value
tempi = tempi + 1
End If
'End With
rsschedulesrecords.FindNext (Format(currdt, "dddd") & "=Yes")
Wend
excelsheet.Range("A8", "L8").Insert
tempi = tempi + 1
excelsheet.Cells(3, 1) = Format(currdt, "dddd mmm dd")
currdt = DateTime.DateAdd("d", -1, currdt)
excelsheet.Cells(1, 4) = DateTime.Now
End If
savefilepath = "\OpsRoster_On-" & CStr(Format(txtStartDate.Value, "mmm-dd-yyyy")) & ".xls"

excelfile.SaveAs CurrentProject.Path & savefilepath
excelapp.ActiveWorkbook.Close True, CurrentProject.Path & savefilepath
excelapp.Quit

Set excelsheet = Nothing
Set excelfile = Nothing
Set excelapp = Nothing

End Sub
Jul 16 '08 #1
1 3036
Stewart Ross
2,545 Expert Mod 2GB
Hi. I think you mean 'record', not 'recordset' - a recordset is the complete set of records which you are traversing, not one of the records within it.

In terms of your code you are not using any form of record counter so for each record found you are just overwriting rows 8 and 9 over and over again.

As you are using two consecutive rows for each record you will need to increment your row counter by two on each pass. I show an indication of how to implement such a counter below.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CreateDailyRoster(rsschedulesrecords As DAO.Recordset)
  2.  
  3. Dim excelapp As New Excel.Application
  4. Dim excelfile As New Excel.Workbook
  5. Dim excelsheet As New Excel.Worksheet
  6. Dim savefilepath As String
  7. Dim tempi As Integer
  8. Dim currdt As Date
  9. Dim rowcount as long
  10.  
  11. currdt = txtStartDate.Value
  12. tempi = 50 ''where remarks begin
  13.  
  14. Set excelfile = excelapp.Workbooks.Open(CurrentProject.Path & "\Template05.xls")
  15. Set excelsheet = excelfile.Worksheets.Item(1)
  16.  
  17. excelsheet.Range("A1") = CDate(txtStartDate.Value)
  18. excelsheet.Range("K4") = "" & Format(txtStartDate.Value, "dddd")
  19. excelsheet.Range("P4") = "" & Format(txtStartDate.Value, "dd")
  20. excelsheet.Range("T4") = "" & Format(txtStartDate.Value, "mmmm")
  21. excelsheet.Range("AB4") = "" & Format(txtStartDate.Value, "yyyy")
  22.  
  23.  
  24. If Not (rsschedulesrecords.EOF) Then
  25. rsschedulesrecords.MoveFirst
  26. excelsheet.Range("A8", "L8").Insert
  27. 'excelsheet.Range(tempr).Insert
  28. tempi = tempi + 1
  29. rsschedulesrecords.FindFirst (Format(currdt, "dddd") & "=Yes")
  30. While Not (rsschedulesrecords.NoMatch)
  31. excelsheet.Range("A8", "L8").Insert
  32. tempi = tempi + 1
  33. excelsheet.Cells(8 + rowcount, 2) = rsschedulesrecords.Fields(1).Value
  34. excelsheet.Cells(9 + rowcount, 2) = rsschedulesrecords.Fields(2).Value
  35. excelsheet.Cells(8+rowcount, 4) = rsschedulesrecords.Fields(17).Value
  36. ...
  37. rsschedulesrecords.FindNext (Format(currdt, "dddd") & "=Yes")
  38. rowcount = rowcount + 2
  39. Wend
  40. ...
  41. End Sub
-Stewart
Jul 20 '08 #2

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

Similar topics

4
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
10
by: Niklas | last post by:
Hi Before I start coding I need to be sure that nobody else has not done it yet and that I can use it. I need an import utility which import data from Excel to a database or some object in...
2
by: bbasberg | last post by:
I have been working hard to clean up my code but I am still wondering why all incoming records go to the "AddNew" part of the IF statement and never to the Edit alternative. I believe that it must be...
16
by: robertmeyer1 | last post by:
Hey, I am working on creating a query which will export the information to excel. I have a specific excel document which has line by line items (corresponds to access query). Here's the...
6
by: jcf378 | last post by:
hello-- i am having trouble figuring out how to export individual records from an Access 2002 Form into a pre-existing Excel spreadsheet, such that the exported record is merely appended to the...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
1
by: afr0ninja | last post by:
Hello! I'm having an issue with the OutputTo function of Access. I have a few queries that I export to excel (by way of the OutputTo function) This works fine. I then take an excel sheet...
2
by: John Bartley K7AAY | last post by:
When I output a table's values to XLS, one value in a very small table, and only one value, is changed. Here are the values in the table, tblLevel. LEVEL H-14 0 1 1.1
5
by: svdoerga | last post by:
Hello! I currently have a button that exports a query, customized by the user on the form, to excel. The data that is exported is compared to older values (from a history table) and depending on...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.