Solved

How do  I obtain the value of an XML column in a SQL table from VBA using ActiveX Data Objects 2.8

Posted on 2008-10-29
10
381 Views
Last Modified: 2012-05-05
I have an xml column in a SQL Express 2008 table that I want to obtain through an ADO 2.8 call in VBA.  It always comes back as an empty string.  In the snippet below,  GetSCFText is a stored procedure that executes the following select statement:

     SELECT TOP (1) SCF FROM TestXMLTable

SCF is an XML column.  If I use a TEXT column in the above select statement the code snippet below works.  However the code below always fails if an XML column is used.

My guess is that the problem is one of the following:

- ADO 2.8 has no support capability for XML column types
- The SELECT statement needs to be revised

Would anyone be able to shed light on this issue?
Dim oADOConn As ADODB.Connection           ' ADO Connection string
Dim cConnString As String                  ' Connection string
Dim oADOCommand As ADODB.Command           ' ADO Command
Dim oADORecSet As ADODB.Recordset          ' ADO Record Set
Dim cXML As String
 
' Connection string
oConnString = ManageVBAItems("SimDBConnString", "", "GET") 
    
If (IsEmpty(oConnString)) Then             ' If no connection string
   Exit Function
End If
        
Set oADOConn = CreateObject("ADODB.Connection")
oADOConn.ConnectionString = oConnString
oADOConn.Open    
Set oADOCommand = CreateObject("ADODB.Command")
    
oADOCommand.ActiveConnection = oADOConn
oADOCommand.CommandType = adCmdStoredProc
oADOCommand.NamedParameters = False
oADOCommand.CommandText = "GetSCFText"    
    
Set oADORecSet = oADOCommand.Execute
cXML = oADORecSet.fields("SCF").Value
    oADOConn.Close                                                        
Set oADOConn = Nothing                                                
Set oADOCommand = Nothing

Open in new window

0
Comment
Question by:MDKIMZEY
  • 7
  • 3
10 Comments
 

Author Comment

by:MDKIMZEY
ID: 22834939
In the attached code snippet, cXML is always an empty string whenever trying to return the value of an xml file in a SQL Express 2008 table - works fine for a TEXT column...
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22839979
If you execute the stored procedure GetSCFText in SSMS, does the field SCF show the expected value?  On the first row?
0
 

Author Comment

by:MDKIMZEY
ID: 22840085
Daniel,

Yes - the XML is in the first row.  GetSCFText executes as I would expect.

-Doug
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:MDKIMZEY
ID: 22842565
Does ADO 2.8 require a different connection string for xml columns?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22843511
I don' think there's anything different in terms of connection strings for XML.  that wouldn't seem to make sense ...

0
 

Author Comment

by:MDKIMZEY
ID: 22844177
Do I need to use SQLXMLOLEDB as the provider?
0
 

Author Comment

by:MDKIMZEY
ID: 22844676
Hi Daniel,

Do you have a snippet that actually works with an XML column type and ADO 2.8?  I'm wondering if this is an issue with the definition of the SQL XML column - what I have noticed is that the oADORecSet.fields("SCF").Value - is always Empty.  The same code executes without problem for any other data type (for example - if I get a text field value - no problem what-so-ever).  

- If others run this and it works - there must be an issue with one of the following:

  1. XML column spec

   2. ActiveX Data Objects 2.8 / SQL Express 2008 / VBA Issue

   3. Connection String

      "Driver={SQL Native Client};Server=DbServer\SQLEXPRESS;Database=MyDb;Trusted_Connection=yes;"

   4. ADO Property Settings

If this simply does not work in SQL Express 2008 - I will simply move the XML to a text column - and come up with a way to deal with the xsd.

0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 22845185
>>Do you have a snippet

No, I haven't actually tried this scenario ... sorry!  Just thought I'd try to help since no one else had yet!
0
 

Author Comment

by:MDKIMZEY
ID: 22849911
Daniel - your help is much appreciated.  The only documentation I have found on this is around the SQLXMLOLEDB provider which only allows you to access data using a stream.
Most documentation is centered around accessing XML columns from .net. Some possible approaches are:

1. Use SQLXMLOLEDB (4.0) with a ADODB Stream

2. Put XML content in a SQL text column - prefix XML with DTD and set the ADO prohibitDTD flag to false.  (This will allow me to use XPath in the XMLDOM to process the XML) - I have tried this and this works - but is a poor workaround since it cuts me out of alot of good functionality.

3. Explore some potential T-SQL procedures to retreive data from the XML column
0
 

Accepted Solution

by:
MDKIMZEY earned 0 total points
ID: 22867162
The best approaches I have found (using ADO 2.8 in VBA) are:

1.  Write a set of stored procedures to shred through the XML with XQuery.  Call the stored procedures
     as you would normally.

2.  Use SQLXMLOLEDB 4.0 with an ADODB Steam
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question