How to Pass Row-Based Parameters to ODBC Queries in Excel VBA
Question details
The user needs to retrieve multiple fields from an external database (like DB2) based on specific cell inputs in Excel, running queries row-by-row without importing the entire dataset.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Entering an identifier (such as an invoice number) in a cell should automatically trigger a database query that fetches related details (like customer name and amount) into adjacent cells.
- Observed behavior
- Fetching external data row-by-row using parameterized ODBC queries via VBA is required because executing a VLOOKUP on an imported table of millions of records is too slow and inefficient.
Ensure that the correct ODBC drivers for your target database (e.g., DB2) are installed on your system and that a valid Data Source Name (DSN) is configured in your Windows ODBC Data Source Administrator.
Use VBA and ADO to Execute Row-by-Row Parameterized Queries
This method uses ActiveX Data Objects (ADO) in VBA to connect to your database, read the cell value, execute a targeted SQL query, and output the results directly into adjacent cells.
Using VBA with ADO allows you to open a single connection to the database and loop through your populated rows. By binding the cell value to the SQL query, you only retrieve the exact records needed, which is significantly faster than loading massive tables into memory.
Open Excel, press ALT + F11 to open the VBA Editor. Go to Tools > References, and check the box for 'Microsoft ActiveX Data Objects x.x Library' (choose the latest version available), then click OK.
Insert a new Module and write a script to establish an ADODB.Connection using your ODBC DSN. For example: `Dim conn As New ADODB.Connection` and `conn.Open "DSN=YourDB2DSN;UID=user;PWD=password;"`.
Write a loop that iterates through your input column (e.g., Column A). For each non-empty cell, construct your SQL query string passing the cell's value: `sql = "SELECT Field1, Field2 FROM YourTable WHERE InvoiceID = '" & cell.Value & "'"`.
Execute the query into a Recordset using `Set rs = conn.Execute(sql)`. Use the `CopyFromRecordset` method or loop through `rs.Fields` to write the fetched data into the adjacent columns (e.g., Columns B and C) of the current row, then close the Recordset.

Set Up Parameterized Queries Using Microsoft Query
If you prefer not to write VBA code, you can use Excel's built-in Microsoft Query tool to create a parameterized query linked to a specific input cell.
Run VBA Macros and Database Queries with WPS Office
WPS Spreadsheets provides comprehensive support for VBA macros and ActiveX Data Objects (ADO), allowing you to seamlessly run external ODBC database queries just like you would in Microsoft Excel.
- 1. Download and Install WPS Office: Visit the official WPS website to download the free suite. Ensure you select the version that includes VBA support during installation.
- 2. Open Your Macro-Enabled Workbook: Launch WPS Spreadsheets and open your .xlsm file containing the ADO database connection scripts.
- 3. Execute the Query Script: Press ALT + F11 to access the WPS Macro Editor. Ensure your ActiveX Data Objects references are active, and run your row-by-row parameterized query directly.

Frequently Asked Questions
Why is an ODBC query faster than VLOOKUP for large databases?
VLOOKUP requires loading the entire dataset (which could be millions of rows) into the spreadsheet's memory. A parameterized ODBC query executes on the database server, retrieving only the specific fields for the exact row needed, saving significant memory and processing time.
Do I need special drivers to query an external database in Excel VBA?
Yes. To connect via ADO or Microsoft Query, you must install the specific ODBC driver provided by your database vendor (e.g., IBM for DB2, Oracle, or SQL Server) and configure a System or User DSN in Windows.
Can I trigger the VBA query automatically when a user types in a cell?
Yes. You can use the Worksheet_Change event in your VBA sheet module. By checking if the modified cell intersects with your input column (e.g., Column A), you can automatically trigger the ADO script to fetch data for that specific row instantly.




