I am trying to retrieve an oracle field with datatype clob into a VB array without reformating the retrieval SQL string (simply SELECT * FROM my_table). I am using an ADO recordset.
The clob values are lost when I close the connection or do any other action with the recordset. How can I achieve this.
In one of my projects I tried to manipulate BLOBs and CLOBs using ADO, but without any success, until I switched to Oracle Objects 4 OLE. You need to install and configure Oracle client on your machine, the in project references add "Oracle InProc Server 4.0 Type Library".
Here's a sample code I used for BLOBs:
Dim OraSession As OracleInProcServer.OraSessionClass
Dim OraDatabase As OracleInProcServer.OraDatabase
Dim OraDynaset As OracleInProcServer.OraDynaset
Dim srcImg As OraBlob
Set OraSession = CreateObject("OracleInProcServer.XOraSession")
Set OraDatabase = OraSession.DbOpenDatabase("myDBName", "login/password", 0&)
Set OraDynaset = OraDatabase.DbCreateDynaset("SELECT src_id, src_image from Sources where src_file_name like '%.png'", 0&)
With OraDynaset
If .RecordCount > 0 Then
.MoveFirst
While (Not .EOF Or .BOF)
Set srcImg = .Fields("src_image").Value
srcImg.CopyToFile ("C:\sources\" & srcID & ".png")
.MoveNext
Wend
End If
End With
Set OraDynaset = Nothing
Set OraDatabase = Nothing
Set OraSession = Nothing
Related
I want to import data from Oracle database to Excel using VBA. I've tried several options I found in here or in the official manual but none of them seem to work for me - I always get the same unspecified runtime error on line with
rs.Open strSQL1, con
or
Set rs = con.Execute(strSQL1)
depending on which one of these two methods I use obviously. Here is the whole code:
Sub data_distribution()
'Setting up the database connection
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL1, strInput, strCon As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=XXX)(PORT=XXX))" & _
"(CONNECT_DATA=(SERVICE_NAME=XXX))); uid=XXX; pwd=XXX;"
'--- Open the above connection string.
con.ConnectionString = strCon
con.Open
'con.Open strCon
'--- Now connection is open and you can use queries to execute them.
'--- It will be open till you close the connection
'Definition of parameter
strInput = InputBox("Insert car_id")
strSQL1 = "select * from car where car_id = " & strInput & ""
'Executing the query
'rs.activeconnection = con
'rs.Open strSQL1, con
Set rs = con.Execute(strSQL1)
rs.Open strSQL1, con
Worksheets("Data").Range("A2").CopyFromRecordset rs
'Closing connection
rs.Close
con.Close
Set con = Nothing
Set rs = Nothing
I was thinking it might be an issue of connecting to the database but if I import/query data manual, it works just fine. For example using this manual
http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
I had to download and configure ODAC for Windows from Oracle site to make it work. I use tsnames.ora to set the path. Wasn't sure if I configured it right but it works so I guess there isn't a problem with the connection in vba either, or is it? (The variables are, of course, set to real values, not "XXX")
The query itself is correct and returns valid results from the database. Libraries necessary to use ADOBD are linked as well. (Microsoft ActiveX Data Objects 2.0 Library)
There is an existing question with the same error but it's unresolved:
Unspecified run time error while executing vba script
I'm guessing it's station/interface specific (I use Excel 2010, SQL developer).
Even though it's been some time I asked the question, here, it's still unresolved so I will answer myself.
The problem was in the car table where one of the attributes was type CLOB (CHARACTER LARGE OBJECT) with lenght over 2000 characters. Excel was unable to cope with that type and the query caused the unspecified error. If I listed all attributes but this one, it all went well.
In VBA macro in Excel I have an issue where the exact same query with the exact same database credentials are returning different results depending on what computer the script is being executed. Some pull all of the correct results, others have results missing.
If we execute the query using Toad or any other tool that can execute oracle it has worked correctly on every machine tested.
So far we have found out that the following things are not causing the issue:
Oracle Driver version (instant client vs full both tested no correlation found)
Excel versions (everyone at my company has the same build of excel)
VBA project references are the same.
Confirmed all machines are pointing to the correct database.
ODBC Settings are the same
All machines tested are running Excel 2010 on windows 7, The Database is Oracle 11,
Here is the script
Dim cn
Set cn = CreateObject("ADODB.Connection")
Dim rs
Set rs = CreateObject("ADODB.Recordset")
cn.ConnectionString = "DSN=#####;UID=####;PWD=#####;DBQ=######;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000;TLO=O;MLD=0;ODA=F"`
`cn.Open`
`Set rs = cn.Execute(SQLfinal)`
Dim iCol As Integer
Dim iRow As Integer
fldCount = rs.Fields.Count
For iCol = 1 To fldCount
Sheets("8. Data").Cells(12, iCol).Value = rs.Fields(iCol - 1).Name
Next
Sheets("8. ####").Cells(13, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Rows("12:12").Select
Selection.AutoFilter
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
I upsized an Access 2007 database to SQL Server 2008 R2. The images are in SQL Server as image type. Access has link to the table containing the image. When I try to display from within Access, it won't do it. It still has the OLE Object wrapper.
How can I get that image and display it on my forms in Access? I do not have the option, at the moment, to remove the images, put them in a directory and point to them (the best way I know but not an option). I need to read the image / blob file directly from SQL Server and display it on an Access form.
Thank you for any ideas.
I saw this but it did not help:
How to display image from sql server in ms access
http://access.bukrek.net/documentation looks like the file in folder method
Since Access 2010, you can use the PictureData property to store and display images from SQL Server. You will need a bound control for an SQL Server data type varbinary(max), which can be hidden, and an unbound Image control in MS Access. You can now simply say:
Private Sub Form_Current()
Me.MSAccessImageControl.PictureData = Me.SQLServerImage
End Sub
And vice versa. You will need to add some error management to that, but very little else.
Below is a function I have successfully used called BlobToFile. And I also posted the code that I use to test it. The picture gets dumped to a so-called temp file but its not truly temp because it isn't in the temp directory. You can manually delete the image file or else you'll have to write it to your temp folder instead. Then I have an image control where I display the picture.
Private Sub Command1_Click()
Dim r As DAO.Recordset, sSQL As String, sTempPicture As String
sSQL = "SELECT ID, PictureBlobField FROM MyTable"
Set r = CurrentDb.OpenRecordset(sSQL, dbSeeChanges)
If Not (r.EOF And r.BOF) Then
sTempPicture = "C:\MyTempPicture.jpg"
Call BlobToFile(sTempPicture, r("PictureBlobField"))
If Dir(sTempPicture) <> "" Then
Me.imagecontrol1.Picture = sTempPicture
End If
End If
r.Close
Set r = Nothing
End Sub
'Function: BlobToFile - Extracts the data in a binary field to a disk file.
'Parameter: strFile - Full path and filename of the destination file.
'Parameter: Field - The field containing the blob.
'Return: The length of the data extracted.
Public Function BlobToFile(strFile As String, ByRef Field As Object) As Long
On Error GoTo BlobToFileError
Dim nFileNum As Integer
Dim abytData() As Byte
BlobToFile = 0
nFileNum = FreeFile
Open strFile For Binary Access Write As nFileNum
abytData = Field
Put #nFileNum, , abytData
BlobToFile = LOF(nFileNum)
BlobToFileExit:
If nFileNum > 0 Then Close nFileNum
Exit Function
BlobToFileError:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
"Error writing file in BlobToFile"
BlobToFile = 0
Resume BlobToFileExit
End Function
In existing Microsoft Access DataBase we have table that inputvalue mapped from oracle DB,I hope with help of macros its getting purged into the access DB from Oracle DB. here my question is there is anyway to identify the existing mapping between oracle DB Field to Access DB Field? Please help on this.
Using VBA:
Dim fld As DAO.Field
Dim tdf As TableDef
Dim db As Database
Set db = CurrentDb
For Each tdf In db.TableDefs
''Linked table
If Len(tdf.Connect) > 0 Then
Debug.Print tdf.Connect
''Local name
Debug.Print tdf.Name
''Source name
Debug.Print tdf.SourceTableName
For Each fld In tdf.Fields
''Local name
Debug.Print fld.Name
''Source name
Debug.Print fld.SourceField
Next
End If
Next
We have very old VB6 applications connecting to a Sybase database. Today they are running fine using the Open Client 12 drivers via ODBC connecting to a Sybase ASE 15 servers.
There is a plan on upgrading to the OpenClient 15 drivers and with that version I'm getting this error:
Run-time error '-2147467259(80004005)'
Transaction cannot have multiple recordsets with this cursor type.
Change the cursor type, commit the transaction, or close one of the recordsets.
As I understand searching the internet, the problem is in the Cursors.
In the old drivers in the ODBC configuration manager there was a Performance Tab and we have the Select Method in cursor, but with the new drivers this tab is gone and all we got about cursors is a section in the General tab named Cursor Behavior, in there we have the option Use Cursors checked.
Here is a little code snippet where I can reproduce the problem. All help is appreciated.
Dim conObj As ADODB.Connection
Dim objRs As ADODB.Recordset
Dim objRs2 As ADODB.Recordset
Set conObj = New ADODB.Connection
conObj.ConnectionTimeout = 10
conObj.CommandTimeout = 5
conObj.Provider = "MSDASQL"
conObj.Open "DSN=cdbur32;UID=***;PWD=***;Database=dbsait;WSID=Test;APP=Test"
conObj.CursorLocation = adUseClient
conObj.BeginTrans
Set objRs = New ADODB.Recordset
Set objRs.ActiveConnection = conObj
objRs.Source = "select id_estatus_aplicacion from dbo.cat_sait_estatus_aplicaciones"
objRs.CursorType = ADODB.CursorTypeEnum.adOpenForwardOnly
objRs.LockType = ADODB.LockTypeEnum.adLockReadOnly
objRs.Open
objRs.MoveNext
Debug.Print objRs("id_estatus_aplicacion")
Set objRs2 = New ADODB.Recordset
Set objRs2.ActiveConnection = conObj
objRs2.Source = "select * from dbo.cat_sait_estatus_aplicaciones"
objRs2.CursorType = ADODB.CursorTypeEnum.adOpenForwardOnly
objRs2.LockType = ADODB.LockTypeEnum.adLockReadOnly
objRs2.Open 'error here
objRs.MoveNext
Debug.Print objRs("id_estatus_aplicacion")
conObj.RollbackTrans
objRs.Close
objRs2.Close
Set conObj = Nothing
Does changing lines
objRs.CursorType = ADODB.CursorTypeEnum.adOpenForwardOnly
and
objRs2.CursorType = ADODB.CursorTypeEnum.adOpenForwardOnly
help?
in the end the Sybase guys send us a new drivers and that fixed the problem