Microsoft KB Archive/141173

= PRB: Default Property of Dynaset Different in VB 4.0 =

Article ID: 141173

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q141173



SYMPTOMS
Code that doesn't refer explicitly to the Name property of the dynaset object, but instead uses the dynaset's old default property, Name, compiles and runs correctly when using Visual Basic 3.0. However, the compilation fails under Visual Basic 4.0, stopping and giving a "Type mismatch" error on lines where the default property is used.



CAUSE
With Visual Basic 4.0, the default value of the dynaset object is no longer the Name property. When you refer to a dynaset object without referring to any properties, Visual Basic 4.0 interprets this to be a reference to the dynaset object itself.



RESOLUTION
Change the code to refer explicitly to all properties. When assigning a string to the RecordSource property of a data control, instead of using Dim ds as Dynaset Data1.RecordSource = ds use the following: Dim ds as Dynaset Data1.RecordSource = ds.Name NOTE: The Dynaset object is provided for backwards compatibility. Use of the new enhanced RecordSet object is recommended.



STATUS
This behavior is by design.



Steps to Reproduce
 Start Visual Basic. On the File menu, click New Project. Double-click the data control button in the Toolbox to add a new data control, named Data1, to the form. Add a command button to the form by clicking the command button icon in the Toolbox.  Insert the following code in the Command1_Click procedure, changing the path with the OpenDatabase statement to refer to where your copy of BIBLIO.MDB is located. In addition, because the code below uses old style syntax, when running under Visual Basic 4.0 you must have "Microsoft DAO 2.5/3.0 Compatibility Layer" checked in the References dialog box. Sub Command1_Click Dim db As Database Dim ds As Dynaset

Set db = OpenDatabase("c:\vb\biblio.mdb") Set ds = db.CreateDynaset("Authors") 'set Data1's RecordSource to the Name of the ds object 'works with Visual Basic 3.0, doesn't work with Visual Basic 4.0 Data1.RecordSource = ds     End Sub </li>  On the Run menu, click Start (ALT, R, S) or press F5. When compiled under Visual Basic 3.0, the above code works as expected and Data1.RecordSource has the value "Authors" when the procedure exits. With Visual Basic 4.0, the program fails to run and flags the last line in the procedure with a 'Type mismatch' error. Changing the last line from Data1.RecordSource = ds to the following Data1.RecordSource = ds.Name resolves the problem. </li></ol>

The RecordSource property of a data control expects a string value, and that is what is returned as the default property of a dynaset in Visual Basic 3.0. In Visual Basic 4.0, referring to a dynaset by its name only is interpreted to mean that you are referring to the recordset object itself. Because RecordSource expects a string, Visual Basic raises the "Type mismatch" error when an attempt is made to assign a recordset object to RecordSource.

NOTE: The Name property of a dynaset or recordset object refers to the name of the table, QueryDef, or SQL statement used to create it (see the online help for Name).

Additional query words: 4.00 vb4win vb4all

Keywords: kbprb KB141173

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.