Microsoft KB Archive/223212

= ACC2000: Unexpected Behavior Referencing Properties and Methods of an Object =

Article ID: 223212

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q223212



Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).



SYMPTOMS
When you run ActiveX Data Objects (ADO) code that references properties or methods of an object, you receive either unexpected results or the following error message:

Run-time error '3265':

ADO could not find the object in the collection corresponding to the name or ordinal reference requested by the application.



CAUSE
The object has not yet been added as a member of the collection.



RESOLUTION
Avoid referencing properties and methods of objects that have not yet been added to their collections.

For example, the "Sample of Unexpected Results" example in the "More Information" section of this article contains the following: 'Count the number of properties of the MyID field BEFORE adding 'MyNewTable to the tables collection. lngCount = tbl.Columns("MyID").Properties.Count

'Display the number of properties found. Debug.Print "Number of properties BEFORE: " & lngCount

'Add the new table to the collection. cat.Tables.Append tbl

'Count the number of properties after adding MyNewTable to collection. lngCount = tbl.Columns("MyID").Properties.Count

'Display the number of properties found. Debug.Print " Number of Properties AFTER: " & lngCount In this example, avoid unexpected results by not referencing the Count property prior to adding the table to the collection. In other words, remove the code: 'Count the number of properties of the MyID field BEFORE adding 'MyNewTable to the tables collection. lngCount = tbl.Columns("MyID").Properties.Count

'Display the number of properties found. Debug.Print "Number of properties BEFORE: " & lngCount

The "Sample of Unexpected Error" example in the "More Information" section of this article contains the following: 'Display the name of the first property for MyID. Debug.Print tbl.Columns("MyID").Properties.Item(0).Name

'Add the new table to the Tables collection. cat.Tables.Append tbl Avoid the error message by not referencing the Name property prior to adding the table to the collection. In other words, remove the code: 'Display the name of the first property for MyID. Debug.Print tbl.Columns("MyID").Properties.Item(0).Name



MORE INFORMATION
If you programmatically reference an object's properties or methods prior to saving that object as a member of a collection, you receive either a run-time error, or the values returned are not accurate. During execution of this procedure, if you reference properties and methods prior to appending the object to its appropriate collection, and then you reference them again after appending the object to its collection, the initial values returned by these properties and methods will be returned again.

Setting a Parent property equal to the appended object or refreshing the collection itself does not resolve the issue.

Steps to Reproduce Behavior
The following two sample code snippets illustrate this behavior. The first sample shows the unexpected results that can occur. The second sample shows the error message that can occur.

Sample of Unexpected Results
 Open a blank new database. In the Database window, click Modules under Objects, and then click New. On the Tools menu, click References. In the Available References box, make sure that the Microsoft ActiveX Data Objects 2.1 Library and the '''Microsoft ADO Ext. 2.1 for DDL and Security''' are selected (checked). Click OK to close the References -  dialog box.</li>  Type the following procedure: Sub Unexpected_Results '''   'Sample procedure to show the unexpected results that can occur when 'referencing properties of an unsaved object. '''   Dim tbl As New ADOX.Table Dim cat As New ADOX.Catalog Dim lngCount As Long

'Establish a connection to the blank new database currently opened. Set cat.ActiveConnection = CurrentProject.Connection 'Specify a new table name, and its field and data-type. tbl.Name = "MyNewTable" tbl.Columns.Append "MyID", adInteger 'Count the number of properties of the MyID field BEFORE adding 'MyNewTable to the tables collection. lngCount = tbl.Columns("MyID").Properties.Count 'Display the number of properties found. Debug.Print "Number of properties BEFORE: " & lngCount

'Add the new table to the collection. cat.Tables.Append tbl 'Count the number of properties after adding MyNewTable to collection. lngCount = tbl.Columns("MyID").Properties.Count

'Display the number of properties found. Debug.Print " Number of Properties AFTER: " & lngCount End Sub </li> On the Debug menu, click Compile .</li> On the View menu, click Immediate Window.</li>  In the Immediate Window, type the following, and then press ENTER: Unexpected_Results </li>  Note the results that are returned:

Number of properties BEFORE: 0

Number of Properties AFTER: 0

NOTE: Adding the following two lines of code just before the final Debug.Print line does not resolve the problem: tbl.ParentCatalog.Tables.Refresh tbl.Columns("MyID").Properties.Refresh </li></ol>

Sample of Unexpected Error
<ol> Open a blank new database.</li> In the Database window, click Modules under Objects, and then click New.</li> On the Tools menu, click References.</li> In the Available References box, click to select the '''Microsoft ADO Ext. 2.1 for DDL and Security''' check box.</li> Click OK to close the References -  dialog box.</li>  Type the following procedure: Sub Unexpected_Error    'Sample procedure to show the unexpected error that occurs when 'referencing properties or methods of an unsaved object.    Dim tbl As New ADOX.Table Dim cat As New ADOX.Catalog 'Establish a connection to the blank new database currently opened. Set cat.ActiveConnection = CurrentProject.Connection 'Specify a new table name, and its field and data-type. tbl.Name = "MyNewTable" tbl.Columns.Append "MyID", adInteger 'Display the name of the first property for MyID. Debug.Print tbl.Columns("MyID").Properties.Item(0).Name 'Add the new table to the Tables collection. cat.Tables.Append tbl End Sub </li> On the Debug menu, click Compile .</li> On the View menu, click Immediate Window.</li>  In the Immediate Window, type the following, and then press ENTER: Unexpected_Error </li> Note that you receive the error message described in the "Symptoms" section of this article.</li></ol>

Additional query words: run time error 3265

Keywords: kbprb kbprogramming KB223212

-

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

© Microsoft Corporation. All rights reserved.