Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
How To Work w/ Microsoft Access QueryDef Parameter Using VB
Article ID: 181782
This article was previously published under Q181782
This article contains examples that illustrate how to call a Microsoft Access parameterized QueryDef and return an ADO recordset using Visual Basic version 6.0. Examples are given for both text and numeric input parameters.
The following examples illustrate ways to return an ADO recordset from a Microsoft Access QueryDef, which expects a parameter in order to return a recordset of one or more rows. The sample application contains three procedures that use Microsoft Access QueryDefs that use parameters.
The first procedure shows an example of passing a numeric parameter while the second procedure shows an example of passing a text parameter. The first two procedures show how to create an ADO parameter collection and define all the parameter properties to have an ADO recordset returned. The third procedure shows how to use the ADO Parameters.Refresh method to return the properties of a parameter.
NOTE: You need to acquire and install the Microsoft Data Access Components (MDAC) for the sample in this article.
If you are using Access 2000 to test, you must use the Jet Engine 4.0 supplied with Microsoft Data Access Components 2.1 and later. You can download the latest version at the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/data/aa937695.aspxThe first requirement is to create the Microsoft Access QueryDefs. This article assumes the reader is familiar with creating new QueryDefs in Microsoft Access. The following Visual Basic code expects two new QueryDefs in the sample Nwind.mdb included with Visual Basic. Create the new QueryDefs with the following properties:
Query Name Table Criteria On Field Datatype ------------------------------------------------------------ ProductsByID Products [ProductID] ProductID Integer CustomerByID Customers [CustomerID] CustomerID Text
Make sure you also set the parameter name and datatype in Microsoft Access by selecting Query, and then choose Parameters.
After the Microsoft Access QueryDef's are created, open a new Visual Basic Project. Form1 is created by default. From the Project menu, choose References then select Microsoft ActiveX Data Objects Library.
Follow these steps:
You may have noticed that another way to get a recordset back from a Microsoft Access QueryDef is to use the Parameter.Refresh method rather than defining the parameter properties in a parameter collection. This actually works although an extra round trip is necessary to the server. However, when passing a text parameter an error occurs because the correct size of a text parameter is not returned by invoking the Parameter.Refresh method. A size property is required when passing a text parameter. This property can be set before creating the ADO recordset to avoid the error.
For additional information on MDAC, please see the following Microsoft Web site:
Microsoft Universal Data Access (MDAC) Web page
Article ID: 181782 - Last Review: July 18, 2012 - Revision: 3.0