Export to excel

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

How can I programatically export data from Access to a selected file and
worksheet? Can do macro or VBA.

(I can do the export but I can't find a way to address the worksheet.)

Thanks.
 
Only way to address the worksheet is to use VBA code that opens a recordset
based on the query/table being exported, opens an EXCEL file, and then
writes the data directly into the cells in the desired worksheet. There is a
VBA method in EXCEL VBA that copies from a recordset (.CopyFromRecordset)
.... I've not used it, but others have and you can find posts about it if you
do a Google.com search on the newsgroups.
 
Depending on what you are trying to do, you can use Data | Get external
data...
from within Excel which uses MS Query to to get a recordset in Excel.

Or you can use VBA to populate the worksheet which requires some programming
knowledge of both Access and Excel. If you use this method you can use the
..CopyFromRecordset method as Ken Snell has correctly pointed out, but this
only works with ADO recordsets not DAO.
 
Here is a fairly complex piece of code I use to create an Excel spread sheet
using ADO. There are extra comments throughout to help you.

HTH,
Bob

Public Function CreateExcelSS()
'-------------------------------------------------------------
' Purpose : Uses ADO to export two record sets to Excel.
' Sheet1 (renamed 'Results') receives cross tab query
' recordset. Sheet2 is created, renamed 'Gene_Ref', &
' receives Gene & Ref recordset values for cross tab output.
' Both sheets columns and headings are formatted:
' Headings are bolded and AutoFit executed on columns.
' Author : Bob Orta, Tuesday, July 08, 2003
'-------------------------------------------------------------
' Revision History
'-------------------------------------------------------------
' Tuesday, July 08, 2003 RO:
' 10/21/03 - Added LocusID to second sheet of worksheet.
'=============================================================
Dim rst As ADODB.Recordset
Dim rst2 As ADODB.Recordset
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xl2Sheet As Excel.Worksheet
Dim i As Integer
Dim FC As Byte ' # fields from crosstab query.

On Error GoTo HandleErr
Set xlApp = New Excel.Application ' Create Excel Application object
Set xlBook = xlApp.Workbooks.Add ' Create a new workbook

xlApp.DisplayAlerts = False
xlApp.DisplayAlerts = True
xlApp.Worksheets.Add ' Adds a worksheet.

' Capture reference to first worksheet
Set xlSheet = xlBook.ActiveSheet
xlSheet.Name = conSheetName ' Change the worksheet name

' Create recordset
Set rst = New ADODB.Recordset
Set rst2 = New ADODB.Recordset
rst.Open _
Source:=conQuery, _
ActiveConnection:=CurrentProject.Connection
FC = rst.Fields.Count
With xlSheet
For i = 1 To FC
' Copy field names to Excel using count of fields, which is
necessary because
' the number of output fields in a crosstab query is not fixed.
' Bold the column headings and insert field names. Starting
position A1. The variable 'i'
' advances the cursor one cell to the right for each additional
field.
With .Cells(1, i)
.Value = rst.Fields(i - 1).Name
.Font.Bold = True
End With
Next
' Copy all the data from the recordset into the spreadsheet.
.Range("A2").CopyFromRecordset rst

' Format the data.
' Causes all columns to autofit cycling throug accordint to field
count (FC).
For i = 1 To FC
.Columns(i).AutoFit
Next
.Columns(2).Delete ' The second column of the crosstab data is not
desired and deleted.
End With
rst.Close
Set rst = Nothing
Set xlSheet = Nothing

' Create 2nd recordset
rst2.Open _
Source:="qryGeneRef", _
ActiveConnection:=CurrentProject.Connection
xlBook.Worksheets("Sheet1").Activate

' Capture reference to second worksheet.
Set xl2Sheet = xlBook.ActiveSheet
xl2Sheet.Name = "Gene_Ref" ' Renames worksheet.
With xl2Sheet
With .Cells(1, 1) ' First cell values
.Value = rst2.Fields(0).Name
.Font.Bold = True
End With
With .Cells(1, 2) ' Second cell values.
.Value = rst2.Fields(1).Name
.Font.Bold = True
End With
With .Cells(1, 3) ' First cell values
.Value = rst2.Fields(2).Name
.Font.Bold = True
End With

.Range("A2").CopyFromRecordset rst2
.Columns("A:B").AutoFit ' Auto fit for two columns.
End With
rst2.Close

'Display the Excel chart
xlApp.Visible = True

Set rst2 = Nothing
Set xl2Sheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing

ExitHere:
Exit Function
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical,
"Form_frmAgExportTestEXCELOutput.CreateExcelSS"
Set rst = Nothing
Set rst2 = Nothing
Set xlSheet = Nothing
Set xl2Sheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing

End Select
End Function
 
Hi Dan,

I am just checking on your progress regarding the information that was sent
you! Have you tried the steps community members provided to you? I wonder
how the testing is going. If you encounter any difficulty, please do not
hesitate to let me know. Please post here and let me know the status of
your issue. Without your further information, it's very hard for me to
continue with the troubleshooting.

Looking forward to hearing from you soon

Sincerely yours,

Mingqing Cheng
Microsoft Online Support
 
Back
Top