Image of Navigational Panel mapped to Contents / Home / Search VB4 Parameter Queries and Data Controls
Tech Support Question

Image of Line Break

Q. I am using VB4, Enterprise and 32-bit. I've got a query in an Access 95 database which accepts parameters. I've also got a data control on the form which I originally assigned to the query. I want to be able to assign values to the parameters and then re-run the query and assign the result to the data control's RecordSource property.

However, when I do so, I get an error message of "Type Mismatch". Why?

From: Rod J. Falanga
Email: rfalanga@unm.edu

A. What exactly your problem is I am not sure, I would be interested to see what the actual code is you're executing. However, regardless of that, what you attempting to do is quite possible. The first point to make is to be clear on the difference between the data control's RecordSet property and its RecordSource property.

The RecordSource property is a string expression that should represent a valid source of records in the database. Whereas the Recordset property is an actual set of records.

The problem is this, you can assign the name of a QueryDef to the RecordSource property, which will work unless the QueryDef has parameters. If the QueryDef has parameters this will not give it sufficient information to resolve into an actual recordset when the data control requests one. This should result in a 'Too few parameters' error.

The best way to go is to use a QueryDef to generate an actual recordset in code by setting its parameters and using the OpenRecordSet method, then you can assign this recordset to the recordset property of the data control. Its RecordSource property is irrelevant in this case as it already has a RecordSet and does not need to construct one.

The following code is from a working example. NB: You must set the Data control's DatabaseName property to the correct database name and path before this code will work as it relies on the database object created from this by the Data control.

   Dim qry As QueryDef
   
   Set qry = Data1.Database.OpenQueryDef("qryGet")
   qry!ParamName = InputBox$("Enter the parameter:")
   Set Data1.Recordset = qry.OpenRecordset(dbOpenSnapshot)
   qry.Close

I hope this helps you to get the effect you are after.


Image of Arrow to Previous Article Image of Arrow to Next Article

[TECH SUPPORT TOC]
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]