PRB: Object Variable Not Set When Referencing Data Control

Last reviewed: November 17, 1995
Article ID: Q110618
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 3.0

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 reference words: 3.00
KBCategory: kbprg kbprb
KBSubCategory: APrgDataOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 17, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.