Need to write your Access data or query to an Excel file? Here is the how to do it:
Most people are familiar with Excel and know how to use it well (enough), and when you start talking about
Access, they get scared off, and don’t know what to do anymore.
Well, here you are going to find out how write your data to an Excel file and then you could send it out
and then import the results back into your database.
Here is the video on how to write your Access table data to an Excel file:
Our data will be from the “Customers” table, but you could also create a query on multiple tables:
This form only has one button called “Export Customers” with this “Click” event (What happens when you click it).
frmExportCustomer
Private Sub btnExport_Click()
'MsgBox "Click"
Dim blnResult As Boolean
blnResult = ExcelExport()
If blnResult = True Then
MsgBox "Exported"
Else
MsgBox "Export Failed"
End If
End Sub
basExcel
Function ExcelExport() As Boolean
Dim rst As Recordset
Dim lngRow As Long
Dim strSQL As String, strTemplate As String
Dim objExcelApp As Excel.Application
Dim objExcelBook As Excel.Workbook
Dim objExcelSheet As Excel.Worksheet
On Error GoTo errhandler
DoCmd.Hourglass True
Set rst = CurrentDb.OpenRecordset("SELECT [CustomerID],[CompanyName] FROM Customers")
If Not rst.EOF Then
Set objExcelApp = New Excel.Application
strTemplate = CurrentProject.Path & "\file.xlsx"
Set objExcelBook = objExcelApp.Workbooks.Add(strTemplate)
Set objExcelSheet = objExcelBook.Worksheets("Sheet1")
objExcelApp.Visible = True
lngRow = 2
Do Until rst.EOF
objExcelSheet.Range("A" & lngRow) = rst.Fields("CustomerID")
objExcelSheet.Range("B" & lngRow) = rst.Fields("CompanyName")
lngRow = lngRow + 1
rst.MoveNext
Loop
'clean up
rst.Close
Set rst = Nothing
Set objExcelApp = Nothing
Set objExcelBook = Nothing
Set objExcelSheet = Nothing
DoCmd.Hourglass False
ExcelExport = True
Else
MsgBox "No Records"
ExcelExport = False
End If
Exit Function
errhandler:
Select Case Err.Number
Case "1004"
MsgBox "We couldn't find your file.xlsx file. Put it in the same folder as this database.", vbExclamation, "ExcelExport"
DoCmd.Hourglass False
Case Else
MsgBox Err.Number & " " & Err.Description, vbExclamation, "ExcelExport"
DoCmd.Hourglass False
End Select
End Function
Learn Access VBA: Understand Tables, Queries, Forms, and Reports
Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]
How to Fix Run Time Error 1004 in Excel
If you work with Microsoft Excel frequently, chances ar ling for a solution. Fortunately, this error is well-documented, and there are several ways to resolve it. In this article, we’ll explore the causes of run time error 1004, practical steps to fix it, and preventive measures to reduce the chances of it happening again. What […]
How To Escape Apostrophe In SQL Update Query
If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]
How To Parse A Flat File In Excel VBA
In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]
Support these sponsors:
