VBA & Macro Problems

Fix a File Path in an Access VBA Function Declaration

Amos GikundaAmos Gikunda Nov 27, 2026 869 views

Question details

Correct an Access VBA function declaration and pass a Windows file path as an argument.

Fix a File Path in an Access VBA Function Declaration
Product
Microsoft Access VBA
Device & OS
not provided
Scenario
A function receives a query name, a worksheet name, and the path to an Excel workbook.
Observed behavior
VBA reports a compile error when a literal file path is placed where the function declaration expects a parameter name.
Before you start

Open the Access database and locate the function and the code that calls it. Check the workbook path and confirm that the mapped drive is available if you plan to run the function.

Solution 1Recommended

Declare a file path parameter and pass the path when calling the function

Use this approach when a literal path in the function declaration causes a VBA compile error.

1
Open the VBA editor

In Access, press Alt+F11 and open the module containing SendTQ2XLWbSheet.

2
Correct the declaration

Replace the literal values in the function declaration with parameter names and types: Public Function SendTQ2XLWbSheet(strTQName As String, strSheetName As String, strFilePath As String). Keep the existing function body and End Function statement.

3
Use the parameter in the function

In the function body, use strFilePath wherever the workbook-opening code needs the file path. Use strTQName and strSheetName for the query and worksheet names as appropriate.

4
Pass the values in the call

In the calling procedure, use quoted strings: Call SendTQ2XLWbSheet("qryExport", "Invoice", "Z:\Payroll\Private\Invoice.xls"). Replace the example values with your own.

5
Compile the project

In the VBA editor, select Debug > Compile to check the declaration and call for compile errors.

Declare a file path parameter and pass the path when calling the function
Check workbook access: Correcting the declaration fixes the syntax issue, but the workbook must still exist and be accessible at the supplied path when the function runs.
Free Microsoft Office alternative

Work with Excel Workbooks in WPS Office

WPS Office is a free, lightweight alternative for everyday document and spreadsheet work. Its familiar interface and Microsoft Office format compatibility make it easy to move existing files. It does not fix Access VBA or replace the Access database in this example.

Open and edit Excel workbooks, including the workbook referenced by the Access function.Compatible with Microsoft Excel XLS and XLSX formats.Familiar spreadsheet interface for a smoother transition from Microsoft Office.Free, lightweight tools for common office tasks.
microsoft office alternative - wps office

Frequently Asked Questions

Why can't I put the file path directly in the function declaration?

A VBA function declaration expects parameter names, such as strFilePath As String, rather than literal values. Pass the quoted path in the code that calls the function.

Do I need to escape backslashes in a VBA file path?

No. In a VBA string literal, use the path inside double quotes with ordinary backslashes, such as "Z:\Payroll\Private\Invoice.xls".

How can I check whether the syntax error is fixed?

In the VBA editor, select Debug > Compile. If another compile error appears, follow the highlighted code and check it separately.

What if the code compiles but cannot open the workbook?

Check that the file exists at the supplied path and that the mapped drive is available to the user running Access. A correct function declaration does not guarantee that the file can be accessed.