473,657 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert row excel after each record exported from access to excel

6 New Member
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.Cell s(9, 2) = rsschedulesreco rds.Fields(2).V alue.

Below is my code. Please help?

Private Sub CreateDailyRost er(rsschedulesr ecords As DAO.Recordset)

Dim excelapp As New Excel.Applicati on
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.Va lue
tempi = 50 ''where remarks begin

Set excelfile = excelapp.Workbo oks.Open(Curren tProject.Path & "\Template05.xl s")
Set excelsheet = excelfile.Works heets.Item(1)

excelsheet.Rang e("A1") = CDate(txtStartD ate.Value)
excelsheet.Rang e("K4") = "" & Format(txtStart Date.Value, "dddd")
excelsheet.Rang e("P4") = "" & Format(txtStart Date.Value, "dd")
excelsheet.Rang e("T4") = "" & Format(txtStart Date.Value, "mmmm")
excelsheet.Rang e("AB4") = "" & Format(txtStart Date.Value, "yyyy")


If Not (rsschedulesrec ords.EOF) Then
rsschedulesreco rds.MoveFirst
excelsheet.Rang e("A8", "L8").Inser t
'excelsheet.Ran ge(tempr).Inser t
tempi = tempi + 1
rsschedulesreco rds.FindFirst (Format(currdt, "dddd") & "=Yes")
While Not (rsschedulesrec ords.NoMatch)
excelsheet.Rang e("A8", "L8").Inser t
tempi = tempi + 1
excelsheet.Cell s(8, 2) = rsschedulesreco rds.Fields(1).V alue
excelsheet.Cell s(9, 2) = rsschedulesreco rds.Fields(2).V alue
excelsheet.Cell s(8, 4) = rsschedulesreco rds.Fields(17). Value
excelsheet.Cell s(8, 5) = rsschedulesreco rds.Fields(20). Value
excelsheet.Cell s(8, 7) = rsschedulesreco rds.Fields(23). Value
excelsheet.Cell s(8, 8) = rsschedulesreco rds.Fields(27). Value
'excelsheet.Cel ls(8, 13) = ""
'excelsheet.Row s.Insert

If (Not IsNull(rsschedu lesrecords.Fiel ds(28).Value) And _
Len(Trim(rssche dulesrecords.Fi elds(28).Value) ) > 0) Then
excelsheet.Rang e("A" & tempi, "L" & tempi).Insert
excelsheet.Cell s(95, 1) = "Remarks/Airline Name " & rsschedulesreco rds.Fields(1).V alue & _
" " & rsschedulesreco rds.Fields(2).V alue & _
" " & rsschedulesreco rds.Fields(15). Value & _
" : " & rsschedulesreco rds.Fields(28). Value
tempi = tempi + 1
End If
'End With
rsschedulesreco rds.FindNext (Format(currdt, "dddd") & "=Yes")
Wend
excelsheet.Rang e("A8", "L8").Inser t
tempi = tempi + 1
excelsheet.Cell s(3, 1) = Format(currdt, "dddd mmm dd")
currdt = DateTime.DateAd d("d", -1, currdt)
excelsheet.Cell s(1, 4) = DateTime.Now
End If
savefilepath = "\OpsRoster _On-" & CStr(Format(txt StartDate.Value , "mmm-dd-yyyy")) & ".xls"

excelfile.SaveA s CurrentProject. Path & savefilepath
excelapp.Active Workbook.Close True, CurrentProject. Path & savefilepath
excelapp.Quit

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

End Sub
Jul 16 '08 #1
1 3050
Stewart Ross
2,545 Recognized Expert Moderator Specialist
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
8703
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 within a single workbook with these records. I would end up with around 500 worksheets, 1 for each unique ID value. I was thinking this could be done if I have an exported flag column in the table, and I search for the max (or min) on the ID...
10
11810
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 .NET. The data in Excel is not in structured columns but can exist everywhere in the workbook. For example if I am supposed to import a person and all his/her cars which exist in a workbook I want to be able to create an import protocol and specify...
2
2329
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 because my rs.index never really receives the value from the cell in the spreadsheet that it should use to compare to the corresponding field in the Access DB. I have named the field "rtitle" in Access and made it an index called riskIndex. I am an...
16
10050
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 layout of the excel doc. Each row is a different line item. Each column is a different entity. So there are about 750 entities which will be exported to this excel doc. (1 entity per column). For access, I have the same information as the excel...
6
6307
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 Excel file, rather than completely overwriting it (as occurs with the basic OutputTo function). specifically, I am trying to create a macro that "backs-up" any deleted record. So, when someone goes to delete a record (i have a delete-record macro...
1
9770
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 having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping to export two Tables (or queries...not sure which to use - they both seem to have the same data) in...
1
1751
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 and link some data from these exported sheets with other data from other excel sheets. I can create a link to one of the exported sheets. It will import the information fine, I can manipulate is needed, no issues. However, once I save the excel...
2
1862
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
8955
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 the changes, the cellcolor is changed, so I need to format each cell individually. I have it working, but with many columns and more than 2500 records it can take like 15 minutes to export. Here's what I do: 'fill in data in excel cells intI = 2 Do...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.