• June 13, 2021


Bring Technology Ideas with you

 How to save image from Form into Table using SQL in Microsoft Access

Microsoft Access is one of the standard alone database use for storing files, data, and so on. To insert image in Microsoft Access’ table, we need to create table which consists of one field as OLE Object (An object linking and embedding). This kind of data type is using for storing image, files, etc. in your database table. Here is the trick for storing file in Microsoft Access table,

Create a table as an example below:

    There are 4 fields in the table (ID,F_Name,Password,User_Image) then go to Create, More Forms–> Modal Dialogs

      Then using some control such as 4 Textbox for ID, F_Name, Password, Path and one image include two Buttons as image below:

        Rename your control as your wishes then in Option Compare Database and Form Load event, please use this code:

        Option Compare Database
        Dim db As DAO.Database
        Private Sub Form_Load()
        Set db = CurrentDb
        End Sub

        In Add Image button, it uses to brows image from your local directory in your computer and write the code as below:

        Private Sub cmdAdd_Click()
        Dim fimage As FileDialog
        Set fimage = Application.FileDialog(msoFileDialogOpen)
        With fimage
        .AllowMultiSelect = False
        .Filters.Add "Please select your image", "*.*", 1
        .Filters.Add "Allow image only.jpg,png", "*.jpg;*.png", 2
        .FilterIndex = 2
        If .Show Then
          Image5.Picture = .SelectedItems.Item(1)
          Me.txtpath.Value = .SelectedItems.Item(1)
        End If
        End With
        End Sub

        In your Save Image Button, it uses SQL statement Insert to add image into your table which was create already. Please write the code:

        Private Sub cmdSave_Click()
        CurrentDb.Execute "Insert into Tbl_Student  values('" & Me.txtid.Value & "','" & Me.txtname.Value & "','" & Me.txtpassword.Value & "','" & Me.txtpath.Value & "')"
        MsgBox ("Save successfully"), vbInformation
        End Sub

        Then your form will look like:

          After you click Save, the it should be:

            Take a look to table, you will see the data as below:

              Related post