PRB: Error or GP Fault When Pass Data Control as Control

Last reviewed: June 21, 1995
Article ID: Q105540
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic

  programming system for Windows, version 3.0

SYMPTOMS

Trying to reference the properties of a Recordset or Database using a data control that was passed as Control to a Sub or Function results in this error message:

   Invalid object reference

In addition, passing a data control into a Sub procedure as Control may result in a general protection (GP) fault, as it does in this example:

   Sub subtest (crtname As Control)
      crtname.UpdateControls
      ' Simply having this line in the code causes the GP fault.
      ' If you turn the line into a comment, the GP fault does not occur.
   End Sub

CAUSE

Recordset and Database specific properties are not available to objects passed as Control. You should replace the "As Control" with "As Data."

NOTE: the Data object type is not easy to find in the Visual Basic documentation. It is, however, listed with the other possible object data types in the "Object Variables" popup on the Help menu topic, "Database Objects." Object types include CheckBox, ComboBox, CommandButton, CommonDialog, Control, Data, DirListBox, DriveListBox, FileListBox, Form, Frame, Grid, HScrollBar, Image, Label, ListBox, MDIForm, OptionButton, PictureBox, TextBox, Timer, and VScrollBar. The Professional Edition adds the following additional object types: Database, Dynaset, Field, Fields, Index, Indexes, QueryDef, Snapshot, Table, TableDef, and TableDefs.

WORKAROUND

Pass the data control as Data. For example, use the following code instead of the code shown below in the MORE INFORMATION section:

   Sub MySub(d As Data)
      Debug.Print d.Recordset.EOF

MORE INFORMATION

The following code example gives the error message:

   Sub MySub(d As Control)
      Debug.Print d.Recordset.EOF    ' an error appears on this line
                             '^^^      with EOF highlighted


Additional reference words: 3.00 gpf
KBCategory: kbprg kbcode 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: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.