LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Export Access Table to Excel

Hi,

I'm trying to use code that I found in a previous post to export an
Access table to Excel.

The code is as follows:

Private Sub Command1_Click()

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim Excel As Object
Dim wb As Object
Dim ws As Object
Dim recArray As Variant
Dim tableName As String
Dim strSQL As String
Dim RecCount As Long
Dim iCols As Integer, iRows As Integer, fldCount As Integer


'Create object variable referring to the open database
Set db = CurrentDb
'Create object variable referring to the SalesData table
tableName = "tblInventories"


strSQL = "Select * from " & tableName 'Recordset to copy


Set Excel = CreateObject("Excel.Application")
Set rst = CurrentDb.OpenRecordset(strSQL)
'Open new workbook and create object variable reference to it
Set wb =
Excel.Workbooks.Open("\\Druma701va16100\forestry\T ools\Databases\Forested
NRMU'S\Inventories.xls")
'Create object variable refering to worksheet
Set ws = wb.Worksheets("New")


'Show Excel and maximise its window
Excel.Visible = True
ws.Activate
'Transfer field names to worksheet
fldCount = rst.Fields.Count
For iCols = 0 To fldCount - 1
ws.Cells(1, iCols + 1).Value = rst.Fields(iCols).Name
Next
'Find number of records
With rst
.MoveLast
RecCount = .RecordCount
.MoveFirst
End With


'Transfer data
recArray = rst.GetRows(RecCount)
ws.Range(ws.Cells(2, 1), _
ws.Cells(RecCount + 1, fldCount)).Value =
Excel.Transpose(recArray)
''Format worksheet
With ws.Range(ws.Cells(1, 1), ws.Cells(1, fldCount))
.Font.Bold = True
.EntireColumn.AutoFit
End With
'The following code could be used to save the workbook and Quit
Excel
wb.Close SaveChanges:=True
Excel.Quit
'Release object variables
Set db = Nothing
Set rst = Nothing
Set Excel = Nothing
Set wb = Nothing
Set ws = Nothing


End Sub

It is exporting the field names to Excel properly. However, at the
following line, I get a Type Mismatch error:

ws.Range(ws.Cells(2, 1), _
ws.Cells(RecCount + 1, fldCount)).Value =
Excel.Transpose(recArray)


Can anybody help? I would sincerely appreciate any input.

Thanks in advance,
Don

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
excel worksheet export into access table H. Chudhary Excel Worksheet Functions 0 January 23rd 08 02:26 PM
Can you export a pivot table into Access? Tracy Excel Discussion (Misc queries) 3 June 27th 07 12:41 AM
Is there any way to export a .xls table to Access .mdb without Acc Mr. Analogy Excel Discussion (Misc queries) 0 February 1st 07 12:05 AM
export spreadsheet from excel to access table Rick[_26_] Excel Programming 2 September 14th 04 10:58 AM
Export Data to Access Table Pete T[_2_] Excel Programming 1 October 10th 03 11:47 PM


All times are GMT +1. The time now is 08:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"