VBA & Macro Problems

Export a Parameterized Microsoft Access Query to CSV with VBA

Steven ParkerSteven Parker Nov 25, 2026 871 views

Question details

Export filtered route and telephone data from a parameterized Access query to a CSV file named with the route value.

Export a Parameterized Microsoft Access Query to CSV with VBA
Product
Microsoft Access
Device & OS
not provided
Scenario
A user needs to run a saved query with a route parameter and provide its results as a CSV file to another program.
Observed behavior
The desired output is a CSV file with appropriate field formatting and a custom filename based on the selected route.
Before you start

Confirm the saved query name and parameter name in Access Query Design, and identify a writable output folder. The example below expects a text parameter named RouteParam and writes to the folder containing the database.

Solution 1Recommended

Export the parameterized query with a VBA CSV routine

Use a DAO recordset to supply the route parameter, write the query results as CSV, and include the route in the filename.

The example writes field names and encloses every value in quotation marks, doubling any quotation marks within values. It uses the query's field order and converts values to text using the current locale.

1
Check the query parameter

Open the saved query in Design View and confirm that it uses a text parameter named [RouteParam]. Replace qryRouteExport in the code with your saved query name. If your query uses a different parameter name or type, update the parameter assignment and input accordingly.

2
Add the export routine

Press Alt+F11, choose Insert > Module, and paste this code: Option Compare Database Option Explicit Public Sub ExportRouteCSV() Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset Dim route As String, safeRoute As String, outputPath As String Dim line As String, badChar As Variant Dim i As Long, fileNum As Integer Dim fileOpen As Boolean Dim errText As String On Error GoTo Failed route = InputBox("Enter the route to export:") If Len(Trim$(route)) = 0 Then Exit Sub safeRoute = Trim$(route) For Each badChar In Array("\", "/", ":", "*", "?", Chr$(34), "<", ">", "|") safeRoute = Replace(safeRoute, CStr(badChar), "_") Next badChar outputPath = CurrentProject.Path & "\route_" & safeRoute & ".csv" Set db = CurrentDb Set qdf = db.QueryDefs("qryRouteExport") qdf.Parameters("RouteParam").Value = route Set rs = qdf.OpenRecordset(dbOpenSnapshot) fileNum = FreeFile Open outputPath For Output As #fileNum fileOpen = True line = "" For i = 0 To rs.Fields.Count - 1 If i > 0 Then line = line & "," line = line & CSVValue(rs.Fields(i).Name) Next i Print #fileNum, line Do While Not rs.EOF line = "" For i = 0 To rs.Fields.Count - 1 If i > 0 Then line = line & "," line = line & CSVValue(rs.Fields(i).Value) Next i Print #fileNum, line rs.MoveNext Loop Close #fileNum fileOpen = False rs.Close MsgBox "Exported to: " & outputPath, vbInformation Exit Sub Failed: errText = "Error " & Err.Number & ": " & Err.Description On Error Resume Next If fileOpen Then Close #fileNum If Not rs Is Nothing Then rs.Close MsgBox errText, vbExclamation End Sub Private Function CSVValue(ByVal value As Variant) As String Dim text As String, quote As String quote = Chr$(34) If IsNull(value) Then text = "" Else text = CStr(value) End If CSVValue = quote & Replace(text, quote, quote & quote) & quote End Function

3
Run and verify the export

In the VBA editor, press Ctrl+G, enter ExportRouteCSV in the Immediate window, and press Enter. Enter a route when prompted, then open the resulting route_<value>.csv file in a text editor to check its headers, delimiters, and quotation marks.

Export the parameterized query with a VBA CSV routine
Check the receiving program's requirements: This routine uses VBA's default text-file encoding and locale-dependent conversion for dates and numbers. If the receiver requires UTF-8 or a particular date, decimal, or telephone format, adjust the export routine and test a sample file. Opening CSV in Excel normally displays field values without their surrounding CSV quotation marks.
Free Microsoft Office alternative

Review exported CSV data with WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and reviewing exported CSV data. Its Microsoft Office format compatibility makes it easy to move existing documents and spreadsheets, although it does not run or fix an Access parameterized query.

Open and review exported route and telephone data in a spreadsheetCompatible with Microsoft Excel XLSX and XLS filesFamiliar spreadsheet interface for a smoother transitionFree, lightweight Office tools for everyday work
microsoft office alternative - wps office

Frequently Asked Questions

Why don't quotation marks appear when I open the CSV in Excel?

Excel interprets quotation marks as CSV delimiters and generally displays only the field values. Open the CSV in a text editor to inspect the actual quotation marks.

How can I use a route selected on an Access form?

Replace the InputBox assignment with a reference to your form control, such as route = Nz(Forms!YourForm!YourRouteControl.Value, ""). Keep the parameter assignment using that route value.

What should I check if Access reports error 3027?

Error 3027 can indicate that the database or underlying data is read-only. Check the database file and folder permissions, linked data sources, and whether the database was opened read-only. Also verify that the CSV destination is writable; file-permission problems may produce a different error.

How do I preserve leading zeros in telephone numbers?

Store or return telephone numbers as text in the Access query, then inspect the CSV in a text editor. When opening the file in a spreadsheet, import the telephone column as text so the spreadsheet does not remove leading zeros.