I'm using an Access (2010) ADP as a front end to a SLQ 2008 db back end. I need the end user to be able to click a button on the ADP form, which will open a FileDialog for them to select a file that will insert the image into the db. Note: I can do this successfully (function and sub below).
My real challenge is viewing the image in an "Image Viewer" form and the worst part is that there isn't a lot out there on the topic.
I created a simple form directly off the table and the "Image" for the record is blank, even though there is "Long Binary" stored in the table.
Any help would be greatly appreciated.
'Function to Store Image File into a field of a recordset
Public Function LoadFieldFromFile(rsf As ADODB.Field, str_full_path As String)
On Error GoTo Er
Dim fds As ADODB.Stream
LoadFieldFromFile = False
Set fds = New ADODB.Stream
'Make it a binary type
fds.Type = adTypeBinary
'Open the stream
'*** Read the binary file into the stream buffer ***
' save binary data into Field of current record
rsf = fds.Read
LoadFieldFromFile = True
Set fds = Nothing
Select Case Err.Number
MsgBox "Could not read file (" & str_full_path & ") , check the path or the file may be in use."
'Unexpected, fail with message box
MsgBox "Error # " & Err.Number & "--" & Error, vbCritical, "LoadFieldFromFile()"
'Sub in Form
Private Sub cmdAttPic_Click()
MsgBox "Note: You can only add one picture at a time.", vbInformation
Dim str_full_path As String
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
str_full_path = vrtSelectedItem
Set fd = Nothing
Dim intFKForm As Integer
intFKForm = DLookup("PK_Form", "ztblForm", "Form_Name='" & Me.FormName & "'")
Dim rs As ADODB.Recordset
Dim Sql As String
Dim FileName As String
Set rs = New ADODB.Recordset
Sql = "SELECT * FROM tblImage WHERE PK_Image = 0;"
rs.Open Sql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic, Options:=adCmdText
rs.Fields("FK_Form") = intFKForm
rs.Fields("FK_ID") = Me.PK_HACCP
rs.Fields("Act_Image_Date") = Date
LoadFieldFromFile rs.Fields("Act_Image"), str_full_path
rs.Fields("Act_Image_File_Name").Value = str_full_path
Dim intPicCnt As Integer
intPicCnt = DCount("[PK_Image]", "tblImage", "[FK_Form]=" & intFKForm & " AND [FK_ID]=" & Me.PK_HACCP)
Me.txtPicCnt = intPicCnt