How To Export Access Data To Excel Using VBA
How To Export Access Data To Excel Using VBA
Preface
For many of the databases I develop I include an export-to-Excel capability. This is for clients who want the means of dumping data so that they can do their own thing without risk to the primary data.
Quite often an export in itself is all that’s required, instead of a carefully created Access report included in the database. Also, on occasions, the export serves as a useful check that the assembled records contain exactly what the client needs, prior to creation of a full-blown Access
report.
This article describes a VBA procedure for dumping data from Access into Excel, exploiting the DoCmd.TransferSpreadsheet method to take you beyond the explanation of the workings of this method that you read in many websites.
Initial Considerations
Compounded from client requirements over the years, I've identified these are the main features of an export-to-Excel capability:
All DMW databases supplied to clients are split — FE (Front End) and BE (Back End). FEs contain queries, forms, reports, macros and modules, and, as appropriate, a table or two, as I'll explain below. BEs contain tables only.
Were users to be allowed to tinker, it would be an impractical and time-consuming task for me providing upgrades and on-gong support to the client. The client would not be happy with the bill.
https://www.consultdmw.com/export-access-data-to-excel.html 1/8
01/07/2019 How to Export Access Data to Excel using VBA
Sub dmwExport
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DoCmd.TransferSpreadsheet _
TransferType:=acExport, _
SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=query$ ,_
FileName:=path$, _
HasFieldNames:=True
End Sub
dmwExport("qsResults", "S:\Reports\Results.XLSX"), for example, exports the contents of the query qsResults to the folder S:\Reports\ as an Excel file named Results.XLSX.
Error Handling
At this point we will include error handling and make sure that the procedure releases any connection with Excel once it has presented the workbook:
DoCmd.TransferSpreadsheet _
TransferType:=acExport, _
SpreadsheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:=query$, _
FileName:=path$, _
HasFieldNames:=True
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
Set wkbk = .Workbooks.Open(path$)
End With
procDone:
Set wkbk = Nothing
Set xlApp = Nothing
Exit Sub
errHandler:
msg$ = Err.Description
https://www.consultdmw.com/export-access-data-to-excel.html 2/8
01/07/2019 How to Export Access Data to Excel using VBA
"S:\Reports\Results.XLSX" This works satisfactorily — the Workbook named Results.XLSX is directed to the S:\Reports\ folder.
"S:\Reports\Results" This works satisfactorily too — the procedure attaches the .XLSX extension to the workbook's name so that Results.XLSX is directed to the S:\Reports\ folder.
"S:\Reports\Results\" Here the problem arises that the final \ causes the procedure to treat S:\Reports\Results\ as a folder, without specifying any workbook at all, with resulting error conditions, eg —
If there’s call for it (let me know), I shall add some code to contend with the issue. The full-blown export program described below incudes such code.
Over the years, I’ve tried a number of ways of providing for this, currently settling with a method that uses an addition to the two main FE and BE files. This third file I name KEY.ini, which is a simple text file, the content of which is this:
Important
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This file to be placed in same folder as USER file
Edit ExportPath to correspond to your folder structure
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DEFAULT]
ExportPath = "S:\Reports\"
Now we need to accommodate KEY.ini into the export process. The process must pick out the export path from KEY.ini.
In tune with good practice we are going to structure our programming by separating our code into a number of sub-routines. Each of these will perform a distinct operation, one of which will be the picking from KEY.ini.
From here on in dmwExport will become one of the sub-routines the running sequences of which will be determined by an overall controlling procedure.
dmwGetPath This VBA-function sub-routine will look for KEY.ini and get the export path from it. If dmwGetPath cannot locate KEY.ini, or is unable the find the information about the export path, then it will return an error message to dmwExportToXL.
dmwCheckPath This VBA-function sub-routine will seek to confirm the existence of the folder to which dumps are to be directed. If dmwCheckPath cannot locate that folder it will it report is as missing to dmwExportToXL.
dmwExport This will complete the export program passing data from Access and into Excel, the opening Excel to display the data, and the formatting of the worksheet.
Sub dmwExportToXL()
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim msg$, bln As Boolean
msg$ = dmwGetPathFromKEY()
bln = dmwCheckPath()
Call dmwExport()
End Function
https://www.consultdmw.com/export-access-data-to-excel.html 3/8
01/07/2019 How to Export Access Data to Excel using VBA
procDone:
dmwGetPathFromKEY = path$
Exit Function
errHandler:
path$ = "Error"
Resume procDone
End Function
If it's unable to return the whereabouts of DATA, then dmwGetPathFromKEY warnings from which dmwStartUp composes messages to the user.
procDone:
dmwCheckPath = msg$
Exit Function
errHandler:
https://www.consultdmw.com/export-access-data-to-excel.html 4/8
01/07/2019 How to Export Access Data to Excel using VBA
msg$ = Err.Description
Resume procDone
End Function
dmwCheckPath returns a null string if the folder is in place, or a message if that folder is missing or that the program cannot find it at the anticipated location.
Function dmwExport( _
query$, path$, _
fileName$, wksName$, _
colsCurrency$, colsDate$ _
) As String
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Error GoTo errHandler
Dim xlApp As Object, wkbk As Object, wks As Object
Dim file$
Dim formatCur$, formatDate$, intColor&
Dim arrayCols() As String, col$, n%, i%, w!
Dim cell As Range
Dim msg$
With .Columns(arrayCols(i))
.NumberFormat = formatCur$
End With
Next i
' Filters
With .Range("A1")
.Select
.autofilter
End With
With xlApp.ActiveWindow
.SplitColumn = 0
.SplitRow = 1
.FreezePanes = True
End With
msg$ = vbNullString
procDone:
Set wks = Nothing
Set wkbk = Nothing
Set xlApp = Nothing
dmwExport = msg$
Exit Function
errHandler:
msg$ = _
Err.Number & ": " & Err.Description
Resume procDone
End Function
Function dmwExportToXL(query$, _
fileName$, wksName$, _
colsCurrency$, colsDate$)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://www.consultdmw.com/export-access-data-to-excel.html 6/8
01/07/2019 How to Export Access Data to Excel using VBA
path$ = Left(CurrentProject.FullName, _
InStrRev(CurrentProject.FullName, "\"))
path$ = path$ & "KEY.ini"
procDone:
If msg$ <> vbNullString Then
MsgBox msg$, vbExclamation, "Excel Export"
End If
Exit Function
errHandler:
msg$ = Err.Number & ": " & Err.Description
Resume procDone
End Function
This is how you might provide values for the arguments of dmwExportToXL:
Each argument value must be enclosed in the quotation marks as shown above.
https://www.consultdmw.com/export-access-data-to-excel.html 7/8
01/07/2019 How to Export Access Data to Excel using VBA
Invoice
Thanks, in anticipation.
The cause of the error was lack of permissions assigned to all the users. To avoid the error, make sure that each user is given full admission to the database BE, and to the folder and drives in which it is located.
Monty Python
☰A T
https://www.consultdmw.com/export-access-data-to-excel.html 8/8