VB4 Parameter Queries and Data ControlsHowever, when I do so, I get an error message of "Type Mismatch". Why?
From: Rod J. Falanga
Email: rfalanga@unm.edu
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.