Microsoft KB Archive/110618

From BetaArchive Wiki

Article ID: 110618

Article Last Modified on 12/9/2003



APPLIES TO

  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
  • Microsoft Visual Basic 3.0 Professional Edition



This article was previously published under Q110618

SYMPTOMS

If a Data control references the Recordset property in the Form_Load event or anytime BEFORE the Form_Activate event, you will receive the following error message (Error#91):

Object Variable Not Set

You can also receive the "Object variable not set" error with the Data control, without necessarily being in a Form-Load event, if you assign the Recordsource property to a table name without quotation marks and then do an addnew. For example:

   data1.recordsource = tablename 'where tablename is neither a string
   'variable containing a valid table name, nor a quoted string.
   data1.refresh
   data1.recordset.addnew 'Gives "Object variable not set."
                

CAUSE

The RecordSet property of a Data Control is not resolved until after the form has been loaded -- that is, after the Form_Load event. This means that you cannot issue any method such as MoveNext or FindNext of the Data Control that uses its record set (its internal Dynaset).

RESOLUTION

To prevent the error, force the creation of the internal dynaset in the data control by issuing the Refresh method:

   Data1.Refresh
                

This will validate the RecordSet property allowing you to use methods that require this.

STATUS

This behaviour is by design.

MORE INFORMATION

NOTE: This problem also occurs in Visual Basic for Applications when you define an object and don't use it. In the General Declarations section of your the code, where the variable is dimmed as an Object, as in the following:

   Dim X as Object
                

comment out or delete this.

Also, if "X" is set to something, for example

   Set X = "John Doe"
                

and is not used later in the procedure, then comment out or delete this line as well.

Steps to Reproduce Behavior

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add a data control (Data1) to Form1.
  3. In the Form_Load event of Form1, place the following code:

         ' Data1.Refresh
         Data1.Recordset.MoveLast
                            
  4. Run the application. You should get Error 91 "Object Variable Not Set."
  5. Remove the apostrophe from the Data1.Refresh line.
  6. Run the application again (press the F5 key). Now, you won't get the error.



Additional query words: 3.00

Keywords: kbprb KB110618