Problem with Access and Images (1 Viewer)

MLUCKHAM

Registered User.
Local time
Today, 21:50
Joined
Jul 23, 2013
Messages
89
I have an issue, I think I have a work around, but I am struggling...

Firstly, I have a SQL 2012 database which has a table holding some images. These images have to be stored in the database (as we are using them in Android devices as well as Access) and so I cannot use paths to the images.

We have an Access 2010 front end which is linking to this SQL 2012 database. The images which are loaded can be displayed in Access, but only one at a time. They can be displayed by putting some code in the Form_Current event: -

Code:
Private Sub Form_Current()

    Image0.PictureData = MealImage
    
End Sub

So far, so good.

However, I need to display a form with multiple images at the same time. I don't want to be a bad coder and have a single form with lots of image controls (as I do not know how many image controls I may need). What I really want to do is to use the built in Continuous Form feature of Access. However, as we all know, the image control only displays different images if you can use an image path (which I don't have).

My work around: -

These are small images - no more than 100kb. When the user clicks to view this particular form they are able to wait a few seconds. My thoughts are: -

1. Create a temporary local table to hold the SQL Data in Access
2. Push the SQL data into the local table.
3. Create "on the fly" the actual image files (based on the image data) and update this local table with the path to these images.
4. Reference the path to these images on the Picture Control and hey presto it will work on a continuous form!

The problem
Although the images are valid, when I dump out the image data to a local file it is not valid and I cannot view the file (in paint etc).

I am not an expert on creating binary image files. I thought it would just be create a file with the same contents and away you go. This is my code: -

Code:
Dim nFileNum As Integer
nFileNum = FreeFile

Open "c:\Test.bmp" For Binary Lock Read Write As #nFileNum

    Put #nFileNum, , MealImage.Value

Close #nFileNum

Am I missing something fundamental here? :banghead:
 

MLUCKHAM

Registered User.
Local time
Today, 21:50
Joined
Jul 23, 2013
Messages
89
I have found some code that works. I thought I would post it so that if anyone has the same problem this workaround could work for you...

This is not my code...:)

Code:
Function SaveBinaryDataTextStream(FileName, ByteArray)
  'Create FileSystemObject object
  Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
  
  'Create text stream object
  Dim TextStream
  Set TextStream = FS.CreateTextFile(FileName)
  
  'Convert binary data To text And write them To the file
  TextStream.Write BinaryToString(ByteArray)
End Function
Function BinaryToString(Binary)
  'Antonin Foller
  'Optimized version of a simple BinaryToString algorithm.
  
  Dim cl1, cl2, cl3, pl1, pl2, pl3
  Dim L
  cl1 = 1
  cl2 = 1
  cl3 = 1
  L = LenB(Binary)
  
  Do While cl1 <= L
    pl3 = pl3 & Chr(AscB(MidB(Binary, cl1, 1)))
    cl1 = cl1 + 1
    cl3 = cl3 + 1
    If cl3 > 300 Then
      pl2 = pl2 & pl3
      pl3 = ""
      cl3 = 1
      cl2 = cl2 + 1
      If cl2 > 200 Then
        pl1 = pl1 & pl2
        pl2 = ""
        cl2 = 1
      End If
    End If
  Loop
  BinaryToString = pl1 & pl2 & pl3
End Function
 

Users who are viewing this thread

Top Bottom