Microsoft KB Archive/266730

= PRB: Run-Time Error Message '91' Occurs on Attempt to Access Recordset Object of Unbound ADO DataControl =

Article ID: 266730

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q266730



SYMPTOMS
When you attempt to access the Recordset object of an ADO DataControl whose Connection and RecordSource properties have been set, the following error message results when the ADO DataControl does not have any controls bound to it:

Run-time error '91': Object variable or With block variable not set



CAUSE
The Connection and Recordset objects of the ADO DataControl do not get implicitly initialized unless the DataControl is bound to a control on the form that contains it.



RESOLUTION

 * Bind the ADO DataControl to a control on the form before attempting to access its Recordset object. You can then set the visible property of the control to False if you do not want the control to be displayed at run time.


 * Call the ADO DataControl's Refresh method prior to accessing its Recordset object if you do not wish to bind the ADO DataControl to any control on the form. Typically, an ADO DataControl is used when there is a requirement to have data bound controls on a form. Writing Visual Basic code to create and use an ADO Recordset instead of using an ADO DataControl is another option to consider if data bound controls are not required.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Open a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click to select Components, and then add the ADO DataControl to the tool box. Drag an ADO DataControl onto Form1. Set the ConnectionString property of the ADO DataControl to connect to the database of your choice, and also specify the RecordSource property.  Copy and paste the following code in the Form_Load event: Dim rs As ADODB.Recordset Set rs = Adodc1.Recordset MsgBox rs.RecordCount </li> Save the project and run it. The error message in the &quot;Symptoms&quot; section of this article is generated on the line of code that attempts to display the value of the RecordCount property of the Recordset.</li>  Copy and paste the following line of code at the top of the Form_Load event procedure: Adodc1.Refresh </li> Save and run the project. Note that the ADO DataControl's Recordset is initialized and that the RecordCount is displayed as required. The Adodc1.Refresh statement is not required if the ADO DataControl has been bound to at least one control on the form.</li></ol>

Additional query words: run time error 91 object variable or with block variable not set

Keywords: kbprb KB266730

-

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

© Microsoft Corporation. All rights reserved.