473,408 Members | 1,754 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,408 software developers and data experts.

Appending to Excel File from Access VBA

92
How do,

I want to be able to append the data from a recordset to the end of an excel file.

Currently I can do this with text files through the .write method, and I can create excel sheets with data in, but not append to the end of them.

I have spent a fair while looking for a solution on this and have come up with literally nothing.

Is there any way to, rather than replace the data in an excel file, append the new data to the end of what is already present?

Thanks in advance,

NDayave
Feb 16 '10 #1
9 17876
beacon
579 512MB
So you do know how to replace existing entries in a spreadsheet? If you do, then appending should be pretty easy. I haven't tested this out from within Access, but I have code for a macro in Excel that I assigned to a command button that appends 10 rows worth of data to the next unused row.

Here's the code I use in Excel, which you should be able to modify to meet your needs from within Access:
Expand|Select|Wrap|Line Numbers
  1. Range("A65536").Select
  2. Selection.End(xlUp).Select
  3. ActiveCell.Offset(1, 0).Select
  4.  
That will highlight the next unused row in your spreadsheet. You can use ActiveCell.Offset(1, 0).Value to insert the actual value into the cell. Then you can adjust the offset to (1, 1) to move to the next column to the right and can increment that number as many times as needed to fill in the appropriate columns.
Feb 16 '10 #2
NDayave
92
No...

What I can do at the moment is create an Excel file from a Query in Access, not replace the existing fields.

This is done via the Access VBA
Expand|Select|Wrap|Line Numbers
  1. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qryExport", varFileName, True
If the filename used already exists, this overwrites the file with the new data.

I'm not sure on how you would run the excel code from within the Access VBA.
Feb 16 '10 #3
beacon
579 512MB
Oh, when you mentioned the .write method, I thought that you were creating an instance of Excel in order to write to files.

You would need to create an instance of Excel first, then you can insert code similar to mine above into that code. Something like the following (I got part of this code from some http://www.excelguru.ca):

Expand|Select|Wrap|Line Numbers
  1. Sub AppendToExcel()
  2.  
  3. Dim objExcel As Object
  4.  
  5. On Error Resume Next
  6.  
  7. Set objExcel = GetObject( , "Excel.Application")
  8.  
  9. If Err.Number <> 0 Then
  10. 'Could not get instance, so create a new one
  11. Err.Clear
  12. On Error GoTo ErrHandler
  13. Set objExcel = CreateObject("Excel.Application")
  14. With objExcel
  15. .Visible = True
  16. .Workbooks.Add
  17. End With
  18. Else
  19. 'Bound to instance, activate error handling
  20. On Error GoTo ErrHandler
  21. End If
  22.  
  23. 'Add some text to the document
  24. With objExcel.ActiveWorkbook
  25. .Worksheets(1).Range("A65536").Select
  26. Selection.End(xlUp).Select 
  27. ActiveCell.Offset(1, 0).Select
  28. ActiveCell.Value = yourFieldValue
  29. End With
  30.  
  31. ErrHandler:
  32. 'Release the object and resume normal error handling
  33. Set objExcel = Nothing
  34. On Error GoTo 0
  35. End Sub
  36. End Sub
  37.  
I may have left out some code to close Excel. Plus, you may want to set the .Visible line of code to False if you don't actually want to see Excel open while you're working in the database.

I haven't tested this out either, but let me know how it works out.
Feb 16 '10 #4
ADezii
8,834 Expert 8TB
  1. Open the Excel Spreadsheet via Automation.
  2. Determine the Last Row which contains Data.
  3. Use the CopyFromRecordset Method to copy the Recordset starting from Range("A<Last Row + 1>).
  4. If you need further help, let us know.
Feb 16 '10 #5
ADezii
8,834 Expert 8TB
Had some spare time, so I wrote the code anyway. Let's suppose that you wanted to Export the Output of qryEmployees to Sheet1 in C:\Stuff\Employees.xls, and that you also wanted to Append the Data to any existing Data (next Row after existing Data on Sheet1):
Expand|Select|Wrap|Line Numbers
  1. Dim appExcel As Excel.Application
  2. Dim lngLastDataRow As Long
  3. Dim RS As DAO.Recordset
  4.  
  5. Set appExcel = CreateObject("Excel.Application")
  6. Set RS = CurrentDb.OpenRecordset("qryEmployees", dbOpenSnapshot)
  7.  
  8. With appExcel
  9.   .Visible = True
  10.   .UserControl = True
  11.  
  12.    With .Workbooks.Open("C:\Stuff\Employees.xls")
  13.      lngLastDataRow = .Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
  14.      .Worksheets("Sheet1").Range("A" & CStr(lngLastDataRow + 1)).CopyFromRecordset RS
  15.    End With
  16. End With
  17.  
  18. RS.Close
  19.  
  20. appExcel.WindowState = xlMaximized
  21.  
  22. Set RS = Nothing
  23. Set appExcel = Nothing
Feb 16 '10 #6
NDayave
92
That is very kind, I'll get back to you both tomorrow regarding what has and hasn't worked.

Many Thanks,

NDayave
Feb 16 '10 #7
NDayave
92
I've had a play with both sets of code. The first doesn't like the line

Selection.End(xlUp).Select

stating that "An Object is required". If I add in .Worksheets("Worksheetname") then it says the Object doesn't support the property or method.

Adezii's code, which looks like this after altering it to fit my needs:
Expand|Select|Wrap|Line Numbers
  1.         Dim appExcel As Object
  2.         'Excel.Application
  3.         Dim lngLastDataRow As Long
  4.  
  5.         Set appExcel = CreateObject("Excel.Application")
  6.         Set RS = CurrentDb.OpenRecordset("qryExport", dbOpenSnapshot)
  7.  
  8.         With appExcel
  9.           .Visible = True
  10.           .UserControl = True
  11.  
  12.            With .Workbooks.Open(varFileName)
  13.              lngLastDataRow = .Worksheets("qryExport").Cells.SpecialCells(xlCellTypeLastCell).Row
  14.              .Worksheets("qryExport").Range("A" & CStr(lngLastDataRow + 1)).CopyFromRecordset RS
  15.            End With
  16.         End With
  17.  
  18.  
  19.         RS.Close
  20.  
  21.         'appExcel.WindowState = xlMaximized
  22.  
  23.         Set RS = Nothing
  24.         Set appExcel = Nothing
You'll note that "Dim appExcel As Excel.Application" has changed to Object as Excel.Application apparently isn't a valid declaration and the xlMaximised is commented out as this refused to work and is for all purposes redundant at this moment in time.

The problem here is on line 13, where it is "Unable to get the SpecialCells property of the Range Class" (Error 1004)

I can't see what is wrong here as I have used similar code within Excel to set the Print area to only those cells that contain data rather than formulas.

Any help is most appreciated,

NDayave
Feb 17 '10 #8
ADezii
8,834 Expert 8TB
  1. Do you have a reference set to the Microsoft Excel X(X).X Object Library?
  2. appExcel should be Explicitly Declared as Excel.Application (Early Binding) to avoid possible problems with Late Binding utilizing the vague Object Type.
Feb 17 '10 #9
NDayave
92
I do now and it is working perfectly.

I'll learn one day.

Cheers,

NDayave
Feb 17 '10 #10

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

Similar topics

1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
2
by: jeffgeorge | last post by:
I'm currently exporting a form to Excel. Because there are controls and totals in the header, I first have a button for users to convert to a datasheet. Then I use the automated quick office...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
9
by: Pete | last post by:
Does anyone have a simple html vbscript or other type of snippet they can share that appends a record to a access database via ADO or DAO? I would like to allow users that don't have Microsoft...
3
by: hari krishna | last post by:
hi, I am generating excel reports through vb.Net. After creating excel.application and the report is generated, each report leaves Excel in memory. I can see them in task manager in Process tab...
9
by: hari krishna | last post by:
hi, I want to send the data from dataset information to Excel through ASP.Net. there will be no XL installed on web server. web server is win 2000 server machine. I am using visual basic code in...
4
by: Jules48 | last post by:
I store comprehensive details of customers' "transactions" in Access (2000). At the moment, I (or my staff) duplicate entry of the information in an Excel spreadsheet which we use to extract stats...
1
by: thanawala27 | last post by:
Hi, Is there any way of APPENDING a Cell in an Excel file? Any Help is appreciated. Thanks,
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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,...
0
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...
0
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...

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.