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
9 17876
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: -
Range("A65536").Select
-
Selection.End(xlUp).Select
-
ActiveCell.Offset(1, 0).Select
-
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.
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 - 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.
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): -
Sub AppendToExcel()
-
-
Dim objExcel As Object
-
-
On Error Resume Next
-
-
Set objExcel = GetObject( , "Excel.Application")
-
-
If Err.Number <> 0 Then
-
'Could not get instance, so create a new one
-
Err.Clear
-
On Error GoTo ErrHandler
-
Set objExcel = CreateObject("Excel.Application")
-
With objExcel
-
.Visible = True
-
.Workbooks.Add
-
End With
-
Else
-
'Bound to instance, activate error handling
-
On Error GoTo ErrHandler
-
End If
-
-
'Add some text to the document
-
With objExcel.ActiveWorkbook
-
.Worksheets(1).Range("A65536").Select
-
Selection.End(xlUp).Select
-
ActiveCell.Offset(1, 0).Select
-
ActiveCell.Value = yourFieldValue
-
End With
-
-
ErrHandler:
-
'Release the object and resume normal error handling
-
Set objExcel = Nothing
-
On Error GoTo 0
-
End Sub
-
End Sub
-
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.
- Open the Excel Spreadsheet via Automation.
- Determine the Last Row which contains Data.
- Use the CopyFromRecordset Method to copy the Recordset starting from Range("A<Last Row + 1>).
- If you need further help, let us know.
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): - Dim appExcel As Excel.Application
-
Dim lngLastDataRow As Long
-
Dim RS As DAO.Recordset
-
-
Set appExcel = CreateObject("Excel.Application")
-
Set RS = CurrentDb.OpenRecordset("qryEmployees", dbOpenSnapshot)
-
-
With appExcel
-
.Visible = True
-
.UserControl = True
-
-
With .Workbooks.Open("C:\Stuff\Employees.xls")
-
lngLastDataRow = .Worksheets("Sheet1").Cells.SpecialCells(xlCellTypeLastCell).Row
-
.Worksheets("Sheet1").Range("A" & CStr(lngLastDataRow + 1)).CopyFromRecordset RS
-
End With
-
End With
-
-
RS.Close
-
-
appExcel.WindowState = xlMaximized
-
-
Set RS = Nothing
-
Set appExcel = Nothing
That is very kind, I'll get back to you both tomorrow regarding what has and hasn't worked.
Many Thanks,
NDayave
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: - Dim appExcel As Object
-
'Excel.Application
-
Dim lngLastDataRow As Long
-
-
Set appExcel = CreateObject("Excel.Application")
-
Set RS = CurrentDb.OpenRecordset("qryExport", dbOpenSnapshot)
-
-
With appExcel
-
.Visible = True
-
.UserControl = True
-
-
With .Workbooks.Open(varFileName)
-
lngLastDataRow = .Worksheets("qryExport").Cells.SpecialCells(xlCellTypeLastCell).Row
-
.Worksheets("qryExport").Range("A" & CStr(lngLastDataRow + 1)).CopyFromRecordset RS
-
End With
-
End With
-
-
-
RS.Close
-
-
'appExcel.WindowState = xlMaximized
-
-
Set RS = Nothing
-
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
- Do you have a reference set to the Microsoft Excel X(X).X Object Library?
- appExcel should be Explicitly Declared as Excel.Application (Early Binding) to avoid possible problems with Late Binding utilizing the vague Object Type.
I do now and it is working perfectly.
I'll learn one day.
Cheers,
NDayave
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: thanawala27 |
last post by:
Hi,
Is there any way of APPENDING a Cell in an Excel file?
Any Help is appreciated.
Thanks,
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |