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



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

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.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Open a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. On the Project menu, click to select Components, and then add the ADO DataControl to the tool box.
  3. Drag an ADO DataControl onto Form1.
  4. Set the ConnectionString property of the ADO DataControl to connect to the database of your choice, and also specify the RecordSource property.
  5. Copy and paste the following code in the Form_Load event:
    Dim rs As ADODB.Recordset
    Set rs = Adodc1.Recordset
    MsgBox rs.RecordCount
    					
  6. Save the project and run it. The error message in the "Symptoms" section of this article is generated on the line of code that attempts to display the value of the RecordCount property of the Recordset.
  7. Copy and paste the following line of code at the top of the Form_Load event procedure:
    Adodc1.Refresh
    					
  8. 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.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbprb KB266730