Solved

What is the best way to get a varchar(max) from ADO

Posted on 2008-10-16
7
638 Views
Last Modified: 2012-05-05
I have a SQL Express column of type varchar(max).  What is the best way to extract the column value using ADO in VBA?  Do I  need to declare and use the Windows API Function: WideCharToMultiByte to use the value as text?

A value of correct length is returned but not as a recognizable or usable string.
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
   
vSCFText = oADORecSet.fields("SCFText").value
        
    oADOConn.Close                                                        ' Close the ADO Connection
    
    Set oADOConn = Nothing                                                ' Release the ADO Connection
    Set oADOCommand = Nothing                                             ' Release the ADO Command

Open in new window

0
Comment
Question by:MDKIMZEY
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:JonMny
ID: 22732091
String variable type will work fine
dim  vSCFText as string
 
0
 

Author Comment

by:MDKIMZEY
ID: 22733459
Actually the string comes through as binary.
0
 
LVL 9

Expert Comment

by:JonMny
ID: 22733479
I tested it on my system and works fine, what is stored in your field? How large is it?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MDKIMZEY
ID: 22733651
JonMny, Thanks much for looking at this and trying it on your system - the column SCFText is declared as SCFTEXT varchar(max)  in SQL Express 2005.

What version of SQL and MDAC are you using?
0
 
LVL 9

Accepted Solution

by:
JonMny earned 125 total points
ID: 22733761
I have sql server 2008 not sure what version of mdac, I used excel 2007 to test it.
Sub sdefla()
Set oADOConn = CreateObject("ADODB.Connection")
oADOConn.ConnectionString = "server=.\dev;Integrated Security=SSPI;database=test;provider=sqloledb"
 
 
 
oADOConn.Open
 
Set oADOCommand = CreateObject("ADODB.Command")
oADOCommand.ActiveConnection = oADOConn
oADOCommand.CommandType = 4
oADOCommand.NamedParameters = False
oADOCommand.CommandText = "gettest"
Set oADORecSet = oADOCommand.Execute
   
vSCFText = oADORecSet.fields("test").Value
        
    oADOConn.Close                                                        ' Close the ADO Connection
    
    Set oADOConn = Nothing                                                ' Release the ADO Connection
    Set oADOCommand = Nothing                                             ' Release
End Sub

Open in new window

0
 

Author Closing Comment

by:MDKIMZEY
ID: 31506745
I appreciate your looking at this - I changed the column definition from varchar(max) to text - this did the trick...
0
 

Author Comment

by:MDKIMZEY
ID: 22734059
This may be an issue with SQL Express 2005 and ADO 2.8 - the solution I used was to change the varchar(max) column to a text column and add a SET TEXTSIZE statement to the stored procedure.  

I did try using a call to:

Private Declare Function WideCharToMultiByte Lib "kernel32" _
    (ByVal codepage As Long, ByVal dwFlags As Long, _
    lpWideCharStr As Any, ByVal cchWideChar As Long, _
    lpMultiByteStr As Any, ByVal cchMultiByte As Long, _
    ByVal lpDefaultChar As String, _
    ByVal lpUsedDefaultChar As Long) As Long

on the binary data in the Value property of the ADO Field.  This had no affect.


0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

696 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