Export Table data into an Excel SpreadSheet (VBA, ACCESS)

hewstone999

Registered User.
Local time
Yesterday, 22:18
Joined
Feb 27, 2008
Messages
37
I have an export function below that will export my table "Test" to an Excel Spreadsheet.

However I want it so i can choose where that data in the "Test" table will go in the Excel Spreadsheet i.e. I want to export all the data in to Cell "B2" of the SpreadSheet - at the moment it will export all the data into "A1"

Any help or ideas?


Private Sub Command3_Click()

'Export function
'EXPORTS TABLE IN ACCESS DATABASE TO EXCEL
'REFERENCE TO DAO IS REQUIRED

Dim strExcelFile As String
Dim strWorksheet As String
Dim strDB As String
Dim strTable As String
Dim objDB As Database

'Change Based on your needs, or use
'as parameters to the sub
strExcelFile = "E:\CSC\LDMS\LDMSDatabaseApp\LDMS_Spec.xls"
strWorksheet = "WorkSheet1"
strDB = "E:\CSC\LDMS\LDMSDatabaseApp\LDMS_IFF_APP.mdb"
strTable = "Test"

Set objDB = OpenDatabase(strDB)

'If excel file already exists, you can delete it here
If Dir(strExcelFile) <> "" Then Kill strExcelFile

objDB.Execute _
"SELECT * INTO [Excel 8.0;DATABASE=" & strExcelFile & _
"].[" & strWorksheet & "] FROM " & "[" & strTable & "]"
objDB.Close
Set objDB = Nothing




End Sub
 
In Access you could create an Excel object in code then create a worksheet then select a location within the worksheet and loop through a recordset to populate the worksheet.

Though it maybe easier to approach it from Excel, if you want to easily specify where in a worksheet the data should go. Eg. in Excel click on B2 then choose from Data menu Import External Data and get your Access data via Import data or New Database query.
Turn on the macro recorder to record those steps then tweak the vba code to suit if you need to automate it for future use
 

Users who are viewing this thread

Back
Top Bottom