[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

MS Access 2010 ADP - Images...

Posted on 2011-04-21
6
Medium Priority
?
784 Views
Last Modified: 2012-05-11
Hey there!!!

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
  fds.Open
  '
  '*** Read the binary file into the stream buffer ***
  '
  fds.LoadFromFile str_full_path
  ' save binary data into Field of current record
  rsf = fds.Read
  
  LoadFieldFromFile = True
    
Done:
  fds.Close
  Set fds = Nothing
  Exit Function

Er:
  Select Case Err.Number
    Case 3002
      MsgBox "Could not read file (" & str_full_path & ") , check the path or the file may be in use."
    Case Else:
      'Unexpected, fail with message box
     MsgBox "Error # " & Err.Number & "--" & Error, vbCritical, "LoadFieldFromFile()"
  End Select
  Resume Done
End Function


'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

    With fd

        If .Show = -1 Then

            For Each vrtSelectedItem In .SelectedItems

                str_full_path = vrtSelectedItem

            Next vrtSelectedItem
        Else
        End If
    End With

    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.AddNew
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
rs.Update

Dim intPicCnt As Integer
    intPicCnt = DCount("[PK_Image]", "tblImage", "[FK_Form]=" & intFKForm & " AND [FK_ID]=" & Me.PK_HACCP)
Me.txtPicCnt = intPicCnt

Open in new window

0
Comment
Question by:NEVAEHSIN
6 Comments
 
LVL 2

Expert Comment

by:SQLSergentMike
ID: 35440766
0
 
LVL 1

Author Comment

by:NEVAEHSIN
ID: 35441025
This may be a dumb question but need to ask before I try to work this in my code...  I've read in other post that the file type is important when decoding it.  I can easily get the file type but it can and probably will not always be the same.  This link refers to .gif images.  Do you think the code will work with other file types such as .jpg, .pdf, etc.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35447162
>>I've read in other post that the file type is important when decoding it.<<
There are only two types binary and text, so in your case it is alway binary.  You will have to save somewhere else the actual file type in order to open it correctly.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 2000 total points
ID: 35460919
Here is what I use.

...don't know if it is adaptable to a ADP
Access-BasicLinkFormReportImages.mdb
0
 
LVL 1

Author Closing Comment

by:NEVAEHSIN
ID: 35460946
Finally a working example!  And great job adding in the report!
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35464082
;-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

830 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