How To Add Images To tblBinary

I have another post which tells you how you can extract your images from tblBinary (for the custom ribbon), but how do you get them in there in the first place?

When you click “Add file to data base” this happens:

Private Sub AddFile_Click()

   Dim strFile As String

    strFile = "file.jpg"
   
    If strFile <> "" Then
        If AddBinFile(strFile) = True Then
            MsgBox "The file was saved in binary format in table ""tblBinary"" .", vbInformation, "Safe File in data base"
        End If
    End If

End Sub

This is the additional “AddBinFile” code that I got from another German example database:

'Funktion 'AddBinFile' fügt der Tabelle tblBinary die Datei sFileName hinzu.
' Falls die Tabelle nicht existiert wird sie neu angelegt.
' Ergebnis der Funktion ist True bei Erfolg
'***************************************************************************
Function AddBinFile(sFileName As String) As Boolean
Dim f As Integer
Dim arrBin() As Byte
Dim rs As DAO.Recordset
 
    On Error GoTo Errr
 
    'Fehlertests...
    If Not tblBinExists(True) Then Err.Raise vbObjectError + 1, "mdlBinary", _
                                    "Binärtabelle konnte nicht erstellt werden!"
    If Dir(sFileName) = "" Then Err.Raise vbObjectError + 2, "mdlBinary", _
                                    "Datei " & sFileName & "existiert nicht!"
    'Datei einlesen in Byte-Array...
    f = FreeFile
    Open sFileName For Binary As #f
    ReDim arrBin(LOF(f))
    Get #f, , arrBin()
    Close #f
 
    'Byte-Array in Tabelle in Binärfeld abspeichern (> .AppendChunk!)
    Set rs = DBEngine(0)(0).OpenRecordset("tblBinary", dbOpenDynaset)
    rs.AddNew
    rs("FileName") = ExtractFileName(sFileName)
    rs("binary").AppendChunk arrBin()
    rs.Update
    rs.Close
    AddBinFile = True
 
fExit:
    Reset
    Erase arrBin
    Set rs = Nothing
    Exit Function
Errr:
    MsgBox Err.Description
    Resume fExit
End Function

'Hilfsfunktion 'tblBinExists':
'Überprüfen, ob Tabelle "tblBinary" existiert; falls ja, dann Rückgabe: True
'Falls Create=True wird sie erstellt, wenn sie noch nicht existiert
Public Function tblBinExists(Optional Create As Boolean = False) As Boolean
Dim S As String
    On Error Resume Next
    DBEngine(0)(0).TableDefs.Refresh
    S = DBEngine(0)(0).TableDefs("tblBinary").Name
    tblBinExists = (Err.Number = 0)
    If Create And Not tblBinExists Then tblBinExists = CreateBinTable
End Function

'Hilfsfunktion 'ExtractFileName':
'Gibt nur den Dateinamen aus dem vollständige Pfad zurück
Function ExtractFileName(sFilePath As String) As String
Dim n As Long
 
    For n = Len(sFilePath) To 1 Step -1
        If Mid(sFilePath, n, 1) = "\" Then Exit For
    Next n
    ExtractFileName = Mid(sFilePath, n + 1)
 
    'Ab A2000 reicht allein diese Zeile (!):
    'ExtractFileName = Split(sFilePath, "\")(UBound(Split(sFilePath, "\")))
 
End Function

Hope this helps.

Click here to see how to get the existing images out of tblBinary.




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How can I interact with other Office applications (Excel) using VBA in Access?

Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]

How To Generate A XML File With Access VBA

XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]

Mouse Click Counter On Access Form

This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]

Shared Access Database Management

Here is a handy setup for those who need to manage a shared access database. Either you can create this from scratch as per the example or add the new tables to your existing database. This setup will track: 1. the users currently using the database 2. what time the user and computer name currently […]

Previous Post

MS Access VBA Form Filter Example

Next Post

How to get the images out of tblBinary