Steps to Reproduce Behavior
The following Visual Basic code sample demonstrates how to load an MSXML
DOMDocument object directly by using the
Save method of the ADO
Recordset object. This example uses data from the SQL Server Pubs sample database. The preceding error message occurs if this code is run on a computer with a version of MDAC earlier than MDAC 2.5, or by referencing version 2.1 of the ActiveX Data Objects Library.
If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:
305019�
INFO: MSXML 4.0 Specific GUIDs and ProgIds
To run this code, follow these steps:
- Create a new Visual Basic Standard EXE project. Form1 is created by default.
- On the Project menu, click to select References, and then click to add references to Microsoft ActiveX Data Objects version and Microsoft XML version 2.0 or later.
- Paste the following code into the code area of the Form1 module. You must modify the database connection string and the query so that it works for your specific database. Make sure that the User ID has the appropriate permissions to perform this operation on the database.
Private Sub Form_Load()
Dim szConnect As String
Dim SQL As String
szConnect = "Provider=MSDASQL;Data Source=<YOUR ODBC DSN>;User Id=<username>;Password=<strong password>;Initial Catalog=pubs"
SQL = "SELECT * FROM AUTHORS"
Dim oRS As ADODB.Recordset
Dim oCN As ADODB.Connection
Set oCN = New ADODB.Connection
Set oRS = New ADODB.Recordset
With oCN
.CursorLocation = adUseClient
.ConnectionString = szConnect
.ConnectionTimeout = 5
.Open szConnect
End With
oRS.Open SQL, oCN
Dim xmlDoc As DOMDocument
Set xmlDoc = New DOMDocument
' To specify a specific version, use a declaration like the following, with the appropriate version in the ProgID:
' Dim xmlDoc As MSXML2.DOMDocument40
' Set xmlDoc = New MSXML2.DOMDocument40
xmlDoc.async = False
oRS.Save xmlDoc, adPersistXML
If xmlDoc.parseError.errorCode <> 0 Then
MsgBox "Errors During Load" & vbCrLf & xmlDoc.parseError.errorCode & xmlDoc.parseError.reason
Else
MsgBox xmlDoc.xml
End If
oRS.Close
oCN.Close
Set oRS = Nothing
Set oCN = Nothing
End Sub
- Run the project. If a project reference has been set to version 2.5 or later of the ActiveX Data Objects Library, then the XML persisted to the DOMDocument is displayed in a message box indicating that it has successfully loaded into the DOM.
The error message in the "Symptoms" section of this article is raised if:- You run this code on a computer that does not have MDAC 2.5 or later installed.
-or-
- You set a project reference to version 2.1 of the Microsoft ActiveX Data Objects Library.