473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delete lines in Excel through Access??

122 New Member
My database must produces a .csv file with a header line that is different from the detail lines. This .csv file contains a monthly report to an outside agency and is uploaded online so it must follow specific guidelines.

As usual, I think I have made the processing way more convoluted than necessary. Here is what I have currently:

1) Macro #1 runs query #1 to create (or overwrite) a table with the current month's header information. This comes from a table.

2) Macro #2 runs query #2 to create (or overwrite) a table that contains the detail lines. This comes from a table as well.

3) Each of these newly created tables is then used to create a subreport which contains text boxes with the data in the order required for the .csv file. There are no labels included in these subreports.

4) The 2 subreports are then brought together into a single report with one header line in the report header section, and all the detail lines in the report detail section.

5) This final report is then programmatically output to an Excel file.

The probem is, the Excel file has extra rows that I don't want. Row 1 has the names for each field in the header line, row 3 is a blank line, and row 4 has names for each field in the detail lines.

The user then has to manually delete these lines and save the file in .csv format. If they delete the wrong line(s) the file will not upload correctly.

Is there a way to programmatically delete these lines before the database gives control back to the user? Or, have I overthought this whole process and if so, can you help me find an easier way to get to the desired result?
Jun 27 '08 #1
6 5830
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Way overcomplicated as you thought, Annalyser.

Much simpler to use VBA to

1. Open an instance of Excel as an automation object
2. Use the Excel CopyFromRecordset method to copy the headings query recordset contents to the worksheet
3. Move the current activecell to the next blank row then use the same CopyFromRecordset method to copy the body data to the Excel workbook
4. Save the workbook as a CSV file
5. Close the Excel automation object.

CopyFromRecordset does not include field headers - which for your application is ideal.

A skeleton for how this is done in VBA is included below, with placeholders for your header and body query names and the filename. You would place the code in the on-click sub of a command button on a form somewhere, replacing your current use of macros.

I assume that the header is the first row (hence the use of Cells(1,1) to refer to row 1 column1 - Cell A1) and that the data is on the second and subsequent rows (hence cells(2, 1) for the second row column 1 - Cell A2).
Expand|Select|Wrap|Line Numbers
  1. Dim objExcel As Excel.Application
  2. Dim HeaderRS As DAO.Recordset
  3. Dim BodyRS As DAO.Recordset
  4. Set objExcel = New Excel.Application
  5. Set HeaderRS = CurrentDb.OpenRecordset("headerqueryname")
  6. Set BodyRS = CurrentDb.OpenRecordset("bodyqueryname")
  7. objExcel.Workbooks.Add
  8. With objExcel.ActiveSheet
  9.     .Cells(1, 1).CopyFromRecordset HeaderRS
  10.     .Cells(2, 1).CopyFromRecordset BodyRS
  11. End With
  12. objExcel.DisplayAlerts = False
  13. objExcel.ActiveWorkbook.SaveAs Filename:="yourfilename.csv", FileFormat:=xlCSV
  14. objExcel.Quit
  15. objExcel.DisplayAlerts = True
  16. HeaderRS.Close
  17. BodyRS.Close
  18. Set objExcel = Nothing
  19.  
You will need references to the DAO object library and the Excel object library to be set before this code will compile. From the VB editor select Tools, References and ensure that Microsoft DAO 3.6 (or later) is ticked, and Microsoft Excel 11 (or later).

-Stewart
Jun 27 '08 #2
Annalyzer
122 New Member
Thank you muchly, Stewart. I haven't worked with recordsets before, but I suppose it's time I learned. I'll give this a try - it does sound easier.
Jun 28 '08 #3
Annalyzer
122 New Member
Quick question, do the DAO object library and Excel object library go with the database when I split it and move it to a server/client setup? Or do I have to set them on each machine that gets a copy of the front end?
Jun 28 '08 #4
Annalyzer
122 New Member
Yowza! It worked! I just created one function that does the job of 2 macros, 2 queries, 2 tables, 2 subreports, 1 report and a partridge in a pear tree! Okay, maybe not the partridge, but I am tickled!! I did some reading - this recordset thing is powerful stuff! I see so many possibilities. Thank you!

Still wondering if I need to set the reference for object libraries on each machine the database is installed on or if these references are stored in the database? I'll go rtm, but in case I don't find it, an answer is always appreciated. =)
Jun 28 '08 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Glad to be of assistance, Annalyser. As for the references, these normally go with the DB itself - as long as the DLLs referenced are installed on the target machine. Since in this case DAO is part of Access and Excel is part of Office you should have no problems with the references once you distribute the database to your users.
-Stewart
Jun 28 '08 #6
Annalyzer
122 New Member
Great! Thanks Stewart.
Jun 28 '08 #7

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

Similar topics

7
5373
by: Smitty | last post by:
I have a function that imports an Excel file into an Access table using SQL. I then close the OleDataReader and the OleDbConnection, then dispose the OleDbCommand, then OleDbConnection. The calling...
8
2675
by: DavidB | last post by:
I'm trying to delete a file with File.Delete("c:\Documents and Settings\%username%\Application Data\Microsoft\Excel\excel*.xlb") On doing this I get an Exception "Illegal characters in path" -...
1
2335
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender,...
3
15261
by: Rene | last post by:
Hi Proggies, i try to delete an excel worksheet but nothing happens (work on vb.net 2005) ..... xlSheet = xlApp.Sheets("Master") xlSheet.select xlSheet.delete also try
2
3181
by: Jan Lorenz | last post by:
Hi, I have a ADO.NET - Connection to an Excel file. UPDATE and INSERT works fine. Now I want to DELETE lines, but it doesn't works (I get an error message). Microsoft says, that there is no way...
1
2314
by: SteveBark | last post by:
Hello all I am currently trying to develop a script that will take a value from an Excel spreadsheet cell and use that to run a query against an Access table to delete all rows that match the...
24
7506
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
3
3587
by: MarcGA | last post by:
(Excel 2003, Access 2003, XP, novice user here) I can't get Access to accept multiple Excel files to the same table. I can import the spreadsheets to a new table, but I need to import 23...
0
7037
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
7076
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...
0
5324
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,...
1
4768
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
4472
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...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
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...

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.