Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I save jpg photos in sql server using microsoft access 2003?

Status
Not open for further replies.

mondo3a

Programmer
Oct 22, 2004
19
CA
I'm using access 2003 as the front end, linked to SQL Server 2005 for the back end. I would like to store scanned photos (jpg's); I do not want to just store the path name in the record, as the scanned photos are confidential (I dont want residing in any network folder). Has anyone got a code sample of this?
 
Something on these lines may suit:

Code:
Dim strSQL As String
Dim strCN As String
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection
Dim mstream As ADODB.Stream

strCN = "ODBC;Description=TEST;DRIVER=SQL Server;" _
& "SERVER=MyServer\Instance;Trusted_Connection=Yes;DATABASE=test"

cn.Open strCN

strSQL = "SELECT ImageCol FROM dbo.ImageTable"
rs.Open strSQL, cn, adOpenDynamic, adLockOptimistic

Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile "c:\docs\image.jpg" 'FileNameToLoadWithFullPath

rs.AddNew
rs.Fields("ImageCol").Value = mstream.Read
rs.Update

rs.Close

Or possibly a pass-through query, but I have not tried this from Access:

Code:
INSERT INTO ImageTable(ImageCol) 
SELECT * 
FROM OPENROWSET(BULK N'C:\docs\image.jpg', SINGLE_BLOB) as ImageCol

 
Thanks for the help.
I tried the code (modified a bit), but i get an error.
Here's the code:
Code:
Dim strSQL As String
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection
Dim mstream As ADODB.Stream

Set cn = CurrentProject.Connection

strSQL = "SELECT testid, photo, notes FROM tbltest where testid=1"

Set rs = New ADODB.Recordset
rs.ActiveConnection = CurrentProject.Connection
rs.CursorLocation = adUseServer
rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic

If rs.EOF = False Then
    Set mstream = New ADODB.Stream
    mstream.Type = adTypeBinary
    mstream.Open
    mstream.LoadFromFile "c:\photo2.jpg"  'FileNameToLoadWithFullPath
    
    rs.Fields("photo").Value = mstream.Read
    rs.Fields("Notes").Value = "Test"
    
    rs.Update
Else
    MsgBox "No records"
End If
rs.Close
If I remove the line:
Code:
rs.Fields("photo").Value = mstream.Read
the code runs ok, but when it's in place, i get an error:
odbc -call failed.
I have set the "photo" field in sql server as varbinary 8000.

any ideas?
 
I just realized that we're using sql server standard edition (2000?) and not 2005 like I said above. discovered this when i unsuccessfully tried to change field from 8000 to varbinary(max). Is there a different field type I can/should be using?
 
ok...I changed the field type to Image, and it seemed to save it. Hate to ask, but what would be the code to retrieve it and place it into an image box?
 
I did not research placing it in an image box in Access, but you can write the file to disk, whence it is easy enough to display.

Code:
Dim strSQL As String
Dim strCN As String
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection
Dim mstream As New ADODB.Stream
 
strCN = "ODBC;Description=Description;DRIVER=SQL Server;" _
& "SERVER=Server\Instance;Trusted_Connection=Yes;DATABASE=DB"
cn.Open strCN
 
strSQL = "SELECT ImageCol FROM ImageTable"
rs.Open strSQL, cn, adOpenDynamic, adLockOptimistic
 
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs!ImageCol
mstream.SaveToFile "c:\docs\imageout.jpg", adSaveCreateOverWrite

 
Thank...that seemed to work.
I appreciate all of your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top