Used the imagecontrol.picture = pathtoimage in a form_current sub to display linked images. Form works fine except images don't print (wanted to make PDF of all records; image in print out was stuck at the file referenced in the image control properties):banghead:. Solution I used was to save form as report (using File->Save As) then add a Detail_Format event handler to the form which does the same thing as the form_current sub on the form. I put a custom print button at the top of the form which opens the report in print preview mode (acViewPreview). Now displays and prints no problem.
Sample code:
'modLinkedImages: Module and sample code on how to deal with hyperlinked images in MSAccess.
'
'History:
'Date Who Description
'---------- --- --------------
'2013-06-05 JTR Created.
Option Compare Database
Option Explicit
'Updateimg: Update an image control from a linked image file, assuming the file
'is in a subdirectory below the database application (.mdb or accdb) file, e.g. /images/pic.jpg
'and is already linked through a hyperlink in the database.
'Create a form with a text box whose data source is a Hyperlink Field in a table or query, containing
'the relative path to the file (txtHyperLink). Add an image control (img) to the form.
'When you create the image control Access will ask you to set
'the path to the file. Select a file that will be bundled with the app, and select the
'link option, not the embedded option. Set the On Current event to be similar to the following:
'-----------------------------------------------------------------------------------------------
''frmLinkedImages.Form_Current:
'Private Sub Form_Current()
' Dim myerr As Long, errString As String
' myerr = Updateimg(Me.txtHyperLink, Me.img, errString)
' If myerr Then
' MsgBox (errString)
' Exit Sub
' End If
'End Sub
'-----------------------------------------------------------------------------------------------
'You can't print directly from the form - MS Access print does not know to
'call Form_Current before printing each record. If you want to PRINT the form,
'create a report from the form by doing a "save as" of the form and saving it as a report.
'Then add a print button to the form (e.g. in the header) with the following code:
'-----------------------------------------------------------------------------------------------
''frmLinkedImages.cmdPrintReport_Click:
'Private Sub cmdPrintReport_Click()
' Dim stDocName As String
' On Error GoTo Err_cmdPrintReport_Click
' stDocName = "rptLinkedImages"
' DoCmd.OpenReport stDocName, acViewPreview 'Note: acViewPreview lets you select a printer, etc.
'Exit_cmdPrintReport_Click:
'Exit Sub
'Err_cmdPrintReport_Click:
' MsgBox Err.Description
' Resume Exit_cmdPrintReport_Click
'End Sub
'-----------------------------------------------------------------------------------------------
'In the report, move the call to Updateimg to the Detail_format event handler:
'-----------------------------------------------------------------------------------------------
''rptLinkedImages.Detail_Format
'Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
' Dim myerr As Long, errString As String
' myerr = Updateimg(Me.txtHyperLink, Me.img, errString)
' If myerr Then
' MsgBox (errString)
' Exit Sub
' End If
'End Sub
'-----------------------------------------------------------------------------------------------
Function Updateimg(txtHyperLink As TextBox, img As Image, errString As String) As Long
Dim relPath As String, pndSign As Long
Updateimg = 0: errString = "": On Error GoTo HandleError
If Not IsNull(txtHyperLink.Value) Then
relPath = txtHyperLink.Value
pndSign = InStr(relPath, "#")
If pndSign = 0 Then Exit Function
relPath = Right(relPath, Len(relPath) - pndSign)
pndSign = InStr(relPath, "#")
If pndSign = 0 Then Exit Function
relPath = Left(relPath, pndSign - 1)
Debug.Print relPath
On Error Resume Next
img.Picture = CurrentProject.Path & "\" & relPath
If Err Then
If Err = 13 Or Err = 2114 Then 'Notta picture or too big
img.Picture = ""
Exit Function
Else
GoTo HandleError
End If
End If
End If
Exit Function
HandleError:
Updateimg = Err.Number
errString = "Error " & Err.Number & " in Updateimg: " & Err.Description & "."
End Function
Sample code:
'modLinkedImages: Module and sample code on how to deal with hyperlinked images in MSAccess.
'
'History:
'Date Who Description
'---------- --- --------------
'2013-06-05 JTR Created.
Option Compare Database
Option Explicit
'Updateimg: Update an image control from a linked image file, assuming the file
'is in a subdirectory below the database application (.mdb or accdb) file, e.g. /images/pic.jpg
'and is already linked through a hyperlink in the database.
'Create a form with a text box whose data source is a Hyperlink Field in a table or query, containing
'the relative path to the file (txtHyperLink). Add an image control (img) to the form.
'When you create the image control Access will ask you to set
'the path to the file. Select a file that will be bundled with the app, and select the
'link option, not the embedded option. Set the On Current event to be similar to the following:
'-----------------------------------------------------------------------------------------------
''frmLinkedImages.Form_Current:
'Private Sub Form_Current()
' Dim myerr As Long, errString As String
' myerr = Updateimg(Me.txtHyperLink, Me.img, errString)
' If myerr Then
' MsgBox (errString)
' Exit Sub
' End If
'End Sub
'-----------------------------------------------------------------------------------------------
'You can't print directly from the form - MS Access print does not know to
'call Form_Current before printing each record. If you want to PRINT the form,
'create a report from the form by doing a "save as" of the form and saving it as a report.
'Then add a print button to the form (e.g. in the header) with the following code:
'-----------------------------------------------------------------------------------------------
''frmLinkedImages.cmdPrintReport_Click:
'Private Sub cmdPrintReport_Click()
' Dim stDocName As String
' On Error GoTo Err_cmdPrintReport_Click
' stDocName = "rptLinkedImages"
' DoCmd.OpenReport stDocName, acViewPreview 'Note: acViewPreview lets you select a printer, etc.
'Exit_cmdPrintReport_Click:
'Exit Sub
'Err_cmdPrintReport_Click:
' MsgBox Err.Description
' Resume Exit_cmdPrintReport_Click
'End Sub
'-----------------------------------------------------------------------------------------------
'In the report, move the call to Updateimg to the Detail_format event handler:
'-----------------------------------------------------------------------------------------------
''rptLinkedImages.Detail_Format
'Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
' Dim myerr As Long, errString As String
' myerr = Updateimg(Me.txtHyperLink, Me.img, errString)
' If myerr Then
' MsgBox (errString)
' Exit Sub
' End If
'End Sub
'-----------------------------------------------------------------------------------------------
Function Updateimg(txtHyperLink As TextBox, img As Image, errString As String) As Long
Dim relPath As String, pndSign As Long
Updateimg = 0: errString = "": On Error GoTo HandleError
If Not IsNull(txtHyperLink.Value) Then
relPath = txtHyperLink.Value
pndSign = InStr(relPath, "#")
If pndSign = 0 Then Exit Function
relPath = Right(relPath, Len(relPath) - pndSign)
pndSign = InStr(relPath, "#")
If pndSign = 0 Then Exit Function
relPath = Left(relPath, pndSign - 1)
Debug.Print relPath
On Error Resume Next
img.Picture = CurrentProject.Path & "\" & relPath
If Err Then
If Err = 13 Or Err = 2114 Then 'Notta picture or too big
img.Picture = ""
Exit Function
Else
GoTo HandleError
End If
End If
End If
Exit Function
HandleError:
Updateimg = Err.Number
errString = "Error " & Err.Number & " in Updateimg: " & Err.Description & "."
End Function