After numerous cups of coffee I finally found a solution for my problem. It works and I don't know how to do it otherwise.
Background: I have a couple of tables in Sql Server, one of the tables stores images. All tables are linked in an Access database which also contains native Access tables.
My client has developed a report based on these table and wanted the images to be included.
The report is built on a query which has the name of the image. I placed an image control on the report, imgLogo.
In the report's OnOpen event I create all the images that are connected to the query. Like this:
Private Sub Report_Open(Cancel As Integer)
Dim strRecSource As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSql As String
strRecSource = Me.RecordSource
Set db = CurrentDb
strSql = "SELECT * FROM " & strRecSource
Set rs = db.OpenRecordset(strSql)
Do While Not rs.EOF
CreateImageOnDisk rs("id"), rs("file_name")
rs.MoveNext
Loop
End Sub
My CreateImageOnDisk function looks like this:
Public Sub CreateImageOnDisk(intID As Integer, strFileName As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSql As String
Dim mStream As ADODB.Stream
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;data Source=localhost;Initial Catalog=procdb;User Id=sa;Password="
Set rs = New ADODB.Recordset
strSql = "SELECT * FROM prod_files WHERE ID = " & intID & " AND file_name = '" & strFileName & "'"
rs.Open strSql, cn, adOpenForwardOnly, adLockOptimistic
Set mStream = New ADODB.Stream
mStream.Type = adTypeBinary
mStream.Open
mStream.Write rs("file_binary")
mStream.SaveToFile Application.CurrentProject.Path & "\" & strFileName, adSaveCreateOverWrite
End Sub
And in the OnFormat event I used the code which I found in the great thread about images in report.
Me.imgLogo.Properties("Picture") = Application.CurrentProject.Path & "\" & Me.file_name
To save diskspace I delete all the images from the computer when exiting.
Anyone comments on this? I'd be glad to hear if you find an easier way of doing it.
Background: I have a couple of tables in Sql Server, one of the tables stores images. All tables are linked in an Access database which also contains native Access tables.
My client has developed a report based on these table and wanted the images to be included.
The report is built on a query which has the name of the image. I placed an image control on the report, imgLogo.
In the report's OnOpen event I create all the images that are connected to the query. Like this:
Private Sub Report_Open(Cancel As Integer)
Dim strRecSource As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSql As String
strRecSource = Me.RecordSource
Set db = CurrentDb
strSql = "SELECT * FROM " & strRecSource
Set rs = db.OpenRecordset(strSql)
Do While Not rs.EOF
CreateImageOnDisk rs("id"), rs("file_name")
rs.MoveNext
Loop
End Sub
My CreateImageOnDisk function looks like this:
Public Sub CreateImageOnDisk(intID As Integer, strFileName As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSql As String
Dim mStream As ADODB.Stream
Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB;data Source=localhost;Initial Catalog=procdb;User Id=sa;Password="
Set rs = New ADODB.Recordset
strSql = "SELECT * FROM prod_files WHERE ID = " & intID & " AND file_name = '" & strFileName & "'"
rs.Open strSql, cn, adOpenForwardOnly, adLockOptimistic
Set mStream = New ADODB.Stream
mStream.Type = adTypeBinary
mStream.Open
mStream.Write rs("file_binary")
mStream.SaveToFile Application.CurrentProject.Path & "\" & strFileName, adSaveCreateOverWrite
End Sub
And in the OnFormat event I used the code which I found in the great thread about images in report.
Me.imgLogo.Properties("Picture") = Application.CurrentProject.Path & "\" & Me.file_name
To save diskspace I delete all the images from the computer when exiting.
Anyone comments on this? I'd be glad to hear if you find an easier way of doing it.