Logicwurks Home Page

Links To Excel Code Examples

Range/Wkb/Wks Variables
Add Grand Totals Using Ranges
Using Range Offset Property
Using Range Find Method
Union Of Ranges
Delete Duplicate Rows
Delete Rows And Columns
Worksheet Variables
Loop Through Worksheets
Add Worksheets Dynamically
Find Last Row Or Column
Copy And Paste Special
Copy To Specific Cell Types
Open An Excel File
Open An Excel File w/Params
Open An Excel File On Web
Sort Methods 2003 - 2010
Sort Alpha/Numeric In ASCII
Search Using Match Function
Search Using Vlookup Function
Remove String Non-Printables
Auto_Open And Auto_Close
Initialize Form At Open
Load Combo And List Boxes
Excel Events
Worksheet Change Events
Binary Search Of Array
Typecast Constants
Excel Error Handling
Handling Optional Parameters
Data Validation Drop Downs
Read A Text Fiile w/Handle
Read A Text Fiile w/Script
Dynamically Load Images
Test For Exists Or Open
Loop Through Pictures
Loop Through Form Objects
Splash Screen
Dynamically Load Formulas
Date Examples
Date Find Same Days
Convert Month To Number
Initialize Arrays
Redim An Array
Reassign Button Action
Timer Functions
Legacy Calendar Control
Excel 2010 Date Picker
Paste Pictures Into Excel
Generate Multiple Worksheets
Read Access Data Into Excel

Links To Access Code Examples

Create Recordset With AddNew
Update Field(s) In A Recordset
Import A Tab Delimited File
Export Excel From Query
Import Tab Delim w/WinAPI
Initialize Global Variables
Access Error Handling
Loop Through Form Controls
Insert A Calendar Control
Create A Filtered Recordset
Populate Combo Boxes
Passing Form Objects
Create VBA SQL Statements
Create Dynamic Queries
Display File Images On A Form
Manipulate Files via VBA
Manipulate Files via Scripting
Number Subform Records
Parse Delimited Fields
Parameterized Queries (VBA)
Manipulating QueryDefs In VBA
FindFirst On Combined Keys
Execute SQL Delete Records
Commit Form To Table
Report With No Data
Reference Form Objects

 

Executing Parameterized Queries From VBA

When running Parameterized queries from the Access interface, the user is asked to enter the parameter. To accomplish this from VBA, parameter values need to be supplied before the query is executed. There are two distinctly different methods to accomplish this:

A parameterized query appears as follows:

The code to demonstrate how to supply [Param1] from VBA (For Both Types) follows, and it uses the QueryDefs object.

Program Code

Option Compare Database
Option Explicit

Public Sub TestParamQuery()
' ***********************************************************************
' Using Parameters in an Append, Update, Delete or Make Table Query
' ***********************************************************************
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset

' ***********************************************************************
' Set qd To The Value of an Existing QueryDef
' ***********************************************************************
Set qd = CurrentDb.QueryDefs("qryTestParams")

' ***********************************************************************
' Assign A Value to the Query's Parameter Param1
' ***********************************************************************
qd!Param1 = "A"
    
' ***********************************************************************
' Execute The Query With a Parameter Value
' ***********************************************************************
qd.Execute dbFailOnError

End Sub

Public Function TestSelectQuery()
' ***********************************************************************
' Using Parameters in a SELECT Query
' ***********************************************************************
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef

' ***********************************************************************
' Set Up DB and Query Definitions
' ***********************************************************************
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryTestSelectWithParams")
qdf!Param1 = "A"

' ***********************************************************************
'Now we'll convert the querydef to a recordset and run it
' ***********************************************************************
Set rst = qdf.OpenRecordset

If rst.EOF Then
    rst.Close
    Exit Function
End If

' ***********************************************************************
' Loop Through All The Records Matching Param1
' ***********************************************************************
Do
    MsgBox ("Value is " & rst!Name)
    rst.MoveNext
Loop Until rst.EOF

rst.Close
qdf.Close

Set rst = Nothing
Set db = Nothing
Set qdf = Nothing

End Function