Microsoft KB Archive/256629

= PRB: "Already Exists" Error Message Occurs When You Use AddNew Method of RecordsetDef with DataSource Control =

Article ID: 256629

Article Last Modified on 8/23/2005

-

APPLIES TO


 * Microsoft Office Web Components
 * Microsoft Office PivotTable Component 9.0 1.0
 * Microsoft Office Chart Component 9.0

-



This article was previously published under Q256629



SYMPTOMS
When you use the AddNew method of the RecordsetDefs collection object with the Office DataSource Control (DSC), you receive the following error message:

Error: An object named "[RecordsetDef Name]" already exists.

You might receive this error message if you use the AddNew method to add a recordset definition with a name that has already been used. The error message occurs even after you delete the recordset definition with the previously used name from the RecordsetDefs collection.



CAUSE
The AddNew method creates a schema row source in addition to a recordset definition. If you specify the Name parameter with AddNew, both a SchemaRowSource object and a RecordsetDef object are created with that name. If either a SchemaRowSource object or a RecordsetDef object with that name already exists, the error message described in the "Symptoms" section occurs.



RESOLUTION
When you use the AddNew method to add a new named recordset definition and schema row source, you must first delete any existing objects from the SchemaRowSources and RecordsetDefs collections that already use that name. This solution is described in more detail in the "More Information" section.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Start Notepad.  Copy the following code, and then paste the code into Notepad: 

  Click Here </BODY>

<SCRIPT language=vbscript>

Dim c

Sub Window_OnLoad

Dim rsd

Set c = DSC.Constants

'Set up the connection for the DataSource control DSC.ConnectionString = _ "provider=microsoft.jet.oledb.4.0; data source=" & _ "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"

Set rsd = DSC.RecordsetDefs.AddNew( _            "Select * from Orders", c.dscCommandText, "MyRSD") End Sub

Sub btnTest_OnClick

Dim rsd

DSC.RecordsetDefs.Delete "MyRSD" Set rsd = DSC.RecordsetDefs.AddNew( _            "Select * from Customers", c.dscCommandText, "MyRSD") End Sub

</SCRIPT>

</HTML> NOTE: The default location for the Microsoft Office files is C:\Progam Files\Microsoft Office. If Office is installed in a different folder on your computer, you must modify the connection string in the code sample to reflect the correct folder for the Northwind.mdb file on your computer.

</li> Save the file as TestDSC.htm.</li> Start Internet Explorer and navigate to the TestDSC.htm file.</li> Click the button on the Web page.

RESULTS: The AddNew method in the OnClick event handler for the button generates a run-time error message, which indicates that the RecordsetDef named "MyRSD" already exists.</li></ol>

To correct the error, modify the OnClick event handler for the button so that you delete both the recordset definition and the schema row source before you call theAddNew method: Sub btnTest_OnClick

Dim rsd

DSC.RecordsetDefs.Delete "MyRSD" DSC.SchemaRowSources.Delete "MyRSD"  '<--- Add this line

Set rsd = DSC.RecordsetDefs.AddNew( _            "Select * from Customers", c.dscCommandText, "MyRSD") End Sub

Additional Notes
All of the Microsoft Office 2000 Web components implement the IPersistHistory interface. Thus, Microsoft Internet Explorer keeps the current state of the components in the history cache when you navigate away from the Web page. If you refresh the Web page or navigate back to that Web page, Internet Explorer restores the component's state from the history cache. For the DataSource control, the state information includes the connection string, the recordset definitions, the schema row sources and so forth.

If you use the AddNew method to initialize a DataSource control in the window's OnLoad event, you should account for situations where the component's state might have been restored from the history cache. To prevent run-time errors that might occur when you attempt to add named recordset definitions that already exist, you can either check the number of recordset definitions:

If DSC.RecorsdetDefs.Count = 0 Then 'Initialize the DataSource Control End If or, check the connection string for the DataSource control:

If Len(DSC.ConnectionString) > 0 Then 'Initialize the DataSource Control End If

<div class="references_section">