PRB: Default Property of Dynaset Different in VB 4.0

ID: Q141173

3.00 4.00 WINDOWS kbprg kbprb

The information in this article applies to:

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.

MORE INFORMATION

Steps to Reproduce

1. Start Visual Basic.

2. On the File menu, click New Project.

3. Double-click the data control button in the Toolbox to add a new data

   control, named Data1, to the form.

4. Add a command button to the form by clicking the command button icon in
   the Toolbox.

5. 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

6. 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.

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 reference words: 4.00 vb4win vb4all KBCategory: kbprg kbprb KBSubcategory: APrgData

Keywords          : kbDatabase 
Version           : 3.00 4.00
Platform          : WINDOWS

Last Reviewed: April 7, 1996