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
380 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
 

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
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: 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now