Thursday, November 21, 2013

Storing PDFs in MS Access

Store is as a binary:

Function LoadFileFromDisk(Bestand, Optional FileName As String = "")
  Dim imgByte() As Byte
  If FileName = "" Then FileName = strFileName
  Open FileName For Binary Lock Read As #1
  ReDim imgByte(1 To LOF(1))
  Get #1, , imgByte
  Close #1
  If Not IsEmpty(imgByte) Then Bestand.Value = imgByte
End Function
This text is after the block

Microsoft website solution is quite long:

Amara BLOB example:

'Read the file 'Image-In.jpg' into the field named 'Image'.
FileToBlob "C:\Photos\Image-In.jpg", [Image]

'Extract the file contained in the field named 'Image' to a file named 'Image-Out.jpg'.
BlobToFile "C:\Photos\Image-Out.jpg", [Image]

'Function:  BlobToFile - Extracts the data in a binary field to a disk file.
'Parameter: strFile - Full path and filename of the destination file.
'Parameter: Field - The field containing the blob.
'Return:    The length of the data extracted.
Public Function BlobToFile(strFile As StringByRef Field As ObjectAs Long
    On Error GoTo BlobToFileError

    Dim nFileNum As Integer
    Dim abytData() As Byte
    BlobToFile = 0
    nFileNum = FreeFile
    Open strFile For Binary Access Write As nFileNum
    abytData = Field
    Put #nFileNum, , abytData
    BlobToFile = LOF(nFileNum)

    If nFileNum > 0 Then Close nFileNum
    Exit Function

    MsgBox "Error " & err.Number & ": " & err.Description, vbCritical, _
           "Error writing file in BlobToFile"
    BlobToFile = 0
    Resume BlobToFileExit

End Function

'Function:  FileToBlob - Loads a file into a binary field.
'Parameter: strFile - Full path and filename of the source file.
'Parameter: Field - The binary field into which the file is to be loaded.
Public Function FileToBlob(strFile As StringByRef Field As Object)
    On Error GoTo FileToBlobError

    If Len(Dir(strFile)) > 0 Then
        Dim nFileNum As Integer
        Dim byteData() As Byte

        nFileNum = FreeFile()
        Open strFile For Binary Access Read As nFileNum
        If LOF(nFileNum) > 0 Then
            ReDim byteData(1 To LOF(nFileNum))
            Get #nFileNum, , byteData
            Field = byteData
        End If
        MsgBox "Error: File not found", vbCritical, _
               "Error reading file in FileToBlob"
    End If

    If nFileNum > 0 Then Close nFileNum
    Exit Function

    MsgBox "Error " & err.Number & ": " & err.Description, vbCritical, _
           "Error reading file in FileToBlob"
    Resume FileToBlobExit

End Function

Display BLOB images in MS Access Report

ADO stream object - this may only work with SQL


No comments:

Post a Comment

Solidworks macros eith ChatGPT

 Record a simple using thr Solidworks macro recorder, upload it to ChatGPT, and explain to ChatGPT how you want it changed:  https://youtu.b...