'saveBinary: save file content on a binary field (BLOB)
'(binary, varbinary, and image data types in Ms SQLServer 2000)
'params:
' sTable : Table Name
' sBinaryField: Column Name that is going to be changed (Binary Field)
' sKeyFld: Key Field Name for indicate the record to update
' KeyValue: Key Field Value
' SFile: Path & File Name that is going to be saved, "" to update with NULL
'
'ie: call saveBinary("pub_info", "logo", "pub_id", "'0736'", "c:\images\foto.jpg")
'saves the file c:\images\foto.jpg on the logo field of pub_info table (pubs database)
'on the record where pub_id = '0736'
'note: the single quotation mark is necesary because pub_id is a varchar field
'
'Author: Roberto Figueroa G.
'Date: 07-Jun-2005
Public Sub saveBinary(ByVal sTable As String, ByVal sBinaryFld As String, _
ByVal sKeyFld As String, ByVal KeyValue As String, Optional ByVal sFile As String = "")
Dim Stm As ADODB.Stream, rs As ADODB.RecordSet
Dim sQry As String
On Error GoTo errHandler
sQry = "SELECT " & sBinaryFld & " FROM " & sTable & " WHERE " & sKeyFld & " = " & KeyValue
Set rs = New ADODB.RecordSet
rs.open strQuery, adoConn, adOpenStatic, adLockOptimistic, adCmdText
If sFile > "" Then
Set Stm = New ADODB.Stream
With Stm
.Type = adTypeBinary
.Open
.LoadFromFile sFile
rs.Fields(0).Value = .Read
End With
Set Stm = Nothing
Else
rs.Fields(0).Value = Null
End If
rs.Update
rs.Close
Set rs = Nothing
Exit Sub
errHandler:
Err.Raise vbObjectError + 6, "saveBinary", "Error on updating field"
End Sub
No comments:
Post a Comment