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



The information in this article applies to:

  • Microsoft Office XP 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.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Notepad.
  2. Copy the following code, and then paste the code into Notepad:
    <HTML>
    
    <BODY>
    <object id=DSC classid=CLSID:0002E530-0000-0000-C000-000000000046></object>
    <BUTTON ID=btnTest>Click Here</BUTTON><BR/>
    </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.

  3. Save the file as TestDSC.htm.
  4. Start Internet Explorer and navigate to the TestDSC.htm file.
  5. 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.
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
				

REFERENCES

Programming Microsoft Office 2000 Web Components by Dave Stearns, ISBN 0-7356-0794-X

For more information about the Microsoft Office Web Components, please visit the following Web site:

For sample code that demonstrates solutions using the Office 2000 Web Components with VBScript and ASP, see the following article in the Microsoft Knowledge Base:

258187 OWebComp.exe Contains Scripting Samples for the Office Web Components


Modification Type:MinorLast Reviewed:8/23/2005
Keywords:kbCodeSnippet kbieObj kbOfficeWebChart kbOfficeWebPivot kbPivotTable kbprb KB256629