Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2005

    Unanswered: How to save a pic to access (binary data)?

    How to save a pic to access (binary data)? How to convert a pic to binary data? I am a beginner.


  2. #2
    Join Date
    Nov 2004
    Dim adoPrimaryRs As ADODB.Recordset
    Dim Chunk() As Byte
    Dim strNote As String
    Dim Offset As Long
    Dim SmallChunks As Long
    Dim chunks As Long
    Private Const ChunkSize As Long = 1024
    Dim FileLength As Long, i As Integer
    Dim DataFile As Integer

    ' FUNCTION : WriteImage
    ' Purpose : Writes the image into the database
    ' Inputs : argPhotoFileName - a valid image path,
    ' argMySql - the Query string to open the record set
    ' argFieldValue - the keyField value
    ' Output : True if the Record is written to the table
    ' IMPORTANT The first field should be image field, the next field the key field

    Public Function WriteImage(argPhotoFileName As String, argMySql As String, argFieldValue As Variant) As Boolean
    On Error GoTo MyError

    '----start storing image data
    Dim strPhotoFile As String, strSQL As String
    Dim varFieldval As Variant
    strPhotoFile = argPhotoFileName
    strSQL = argMySql
    varFieldval = argFieldValue
    Set adoPrimaryRs = New ADODB.Recordset
    With adoPrimaryRs
    .Open strSQL, m_cnnGlobal, adOpenKeyset, adLockOptimistic
    If .EOF And .BOF Then
    ' record is not there so add new else modify
    .Fields(1).Value = varFieldval
    .Fields(2).Value = m_UDTUser.bytUserID
    End If
    End With
    If Not Len(strPhotoFile) = 0 Then
    DataFile = 1
    'Open the image file
    Open strPhotoFile For Binary Access Read As DataFile
    FileLength = LOF(DataFile) ' Length of data in file
    'If the imagefile is empty Then Exit
    If FileLength = 0 Then
    Close DataFile
    WriteImage = False
    Set adoPrimaryRs = Nothing
    Exit Function
    End If
    'Calculate the bytes(Chunks)pakages to write
    chunks = FileLength \ ChunkSize
    SmallChunks = FileLength Mod ChunkSize
    'Resize the chunck array to adjust the firts bytes package
    'To be copied
    ReDim Chunk(SmallChunks)
    Get DataFile, , Chunk()
    'Write the bytes to the given database fieldname
    adoPrimaryRs.Fields(0).AppendChunk Chunk()
    'Adjust the chunck array for the rest bytes
    'packages to be copied
    ReDim Chunk(ChunkSize)
    For i = 1 To chunks
    Get DataFile, , Chunk()
    adoPrimaryRs.Fields(0).AppendChunk Chunk()
    Next i
    Close DataFile
    adoPrimaryRs.Fields(2).Value = m_UDTUser.bytUserID
    WriteImage = True
    Set adoPrimaryRs = Nothing
    End If
    '-----end storing image data
    Exit Function
    MsgBox "Error while writing the ImageData" & vbCrLf & Err.Description, vbCritical, "Images"
    Close DataFile
    Set adoPrimaryRs = Nothing
    WriteImage = False
    End Function


    strSql= "Select Design,DesignID From DesignImages Where DesignID=1"

    DesignImages is the table name , Design is the field in which the design is
    inserted and DesignID is the key field



    Courtsey some Programmer who had posted this code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts