473,474 Members | 1,310 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Export from access to excel

3 New Member
i have a couple of books coming on programming access - but they have not arrived as of yet. i am a fluent excel vba programmer, but i am extending out into access. I am starting with a simple export to a code generated excel file.
My database consists of 1 table (tblErrLog) with 8 fields (numEntry, txtVZID, txtAcctNum, txtRegion, txtComments, EZInfo, txtDate, txtTime)

I have the users enter data into an unbound form and submit it thru a simple sql string. At a random time, i will need to export data into a new excel file. the user will specify a data range, datStart and datEnd. then i will need to know how to use vba to have access export all records where txtDate is within the specified date range.

If anyone could shed some code onto the subject, i would greatly appreciate it, thanks!
Feb 14 '08 #1
3 5495
MindBender77
234 New Member
You could make an append query that writes the user selected records to a temp table. To export those records to Excel, you would use "TransferSpreadsheet". Both the append query and TransferSpreadsheet can be add and ran from the same macro.

Hope this points you in the right direction,
Bender
Feb 14 '08 #2
aaronward
3 New Member
I was actually looking for some sort of source code. I am rather confused on the export process. if anyone can provide any assistance, i would greatly appreciate it, thanks!

You could make an append query that writes the user selected records to a temp table. To export those records to Excel, you would use "TransferSpreadsheet". Both the append query and TransferSpreadsheet can be add and ran from the same macro.

Hope this points you in the right direction,
Bender
Feb 14 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
i have a couple of books coming on programming access - but they have not arrived as of yet. i am a fluent excel vba programmer, but i am extending out into access. I am starting with a simple export to a code generated excel file.
My database consists of 1 table (tblErrLog) with 8 fields (numEntry, txtVZID, txtAcctNum, txtRegion, txtComments, EZInfo, txtDate, txtTime)

I have the users enter data into an unbound form and submit it thru a simple sql string. At a random time, i will need to export data into a new excel file. the user will specify a data range, datStart and datEnd. then i will need to know how to use vba to have access export all records where txtDate is within the specified date range.

If anyone could shed some code onto the subject, i would greatly appreciate it, thanks!
VBA provides a specific Excel routine for copying a recordset direct to Excel from Access - CopyFromRecordSet. It does not itself copy the field names, but these are easily transferred from the query itself as shown below.

In the Access VBA code you need to create and initialise an Excel application object. Once that is done you can open a named workbook and transfer the data using the CopyFromRecordSet method.

The advantage of using this approach is that you have full programmed control of what you do with the data thereafter - saving the worksheet, formatting cells, inserting worksheets, copying existing sheets, and so on.

The following skeleton extracts show the general principles. I have not shown all Dims or definitions for the variables.

Expand|Select|Wrap|Line Numbers
  1. Dim ObjExcel as Excel.Application
  2. Dim TheQuery as DAO.Recordset
  3.  
  4. Set objExcel as New Excel.Application
  5.  
  6. objExcel.Workbooks.Add ' if new workbook wanted, or
  7. objExcel.Workbooks.Open (WorkBookPath) ' if existing workbook - WorkBookPath is name and path of workbook to open
  8.  
  9. Set TheQuery = CurrentDb.OpenRecordset(QueryName)
  10. 'QueryName is a string holding the name of the table or query to be copied.
  11.  
  12. TheQuery.MoveLast
  13. R = TheQuery.RecordCount
  14. ' recordcount is not used in this extract, but is useful in other contexts. 
  15. ' the recordcount is not accurate unless you have moved to the end of the 
  16. ' recordset first - to allow Access to count the records properly.
  17. ' If you forget to move back to the first record the RecordSet will be at end of file
  18. ' which will terminate any loop relying on a While Not TheQuery.EOF condition
  19.  
  20. TheQuery.MoveFirst
  21. N = TheQuery.Fields.Count
  22.  
  23. 'Copy the field names to Excel
  24. With objExcel.ActiveSheet
  25.         For I = 0 To N - 1
  26.             .Cells(StartRow, I + StartColumn) = TheQuery.Fields(I).Name
  27.         Next I
  28. End With
  29.  
  30. 'Copy the querydata in the row below the field names
  31.  
  32. objExcel.Cells(StartRowNo+1,StartColumnNo)._
  33. CopyFromRecordset TheQuery 
  34.  
  35. ' StartRowNo and StartColumnNo are numbers - 1 for row 1, 2 for row 2 etc, 1 for column A, 2 for column B etc. Cells (1,1) refers to cell A1.
  36.  
  37. TheQuery.Close
  38.  
  39. objExcel.DisplayAlerts = False ' turn off user dialogue for file saving
  40. objExcel.ActiveWorkbook.SaveAs FileName:=TheFileName ' specify the name of the file
  41. objExcel.DisplayAlerts = True

The skeleton is taken from several different procedures and functions I use within an Excel automation class developed to handle copying of queries from Access to Excel. Normally, these are copied then autofilter applied, column widths fitted, and other processing done from within Access directly on the Excel object.

Hope this helps with ideas for what can be done.

Cheers

Stewart
Feb 14 '08 #4

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

Similar topics

4
by: Gary Wright | last post by:
I have an Access 2K database split into front and back. Quite often the users want to do some data analysis that I have not created a report for so they want to export some subset of the data into...
6
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
14
by: bonehead | last post by:
Greetings, I'm using the DoCmd.TransferText method to export the results of a MS Access query to a csv file. The csv will then be used to load an Oracle table. In other systems such as TOAD...
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...
5
by: Simon | last post by:
Dear reader, With the export command you can export a query to Excel. By activate this command a form pop's up with the following text:
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: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
1
by: Pauline | last post by:
Dear all, I have an enormous database (Access 2003) containing sales information, and an Excel tool to enable end users to do planning and forecasting. Untill now I would create several queries,...
3
by: StevoNZ | last post by:
I've been using Access to export data to Excel all year... this data has been increasing in size as the project nears compleion and now I find that when I export a table (or query) to Excel using the...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.