DEV Community

Fix Microsoft Access
Fix Microsoft Access

Posted on • Edited on

How To Insert Image In Access Database?

Have you ever tried to insert image in your Access database? if not then do it as by doing this you can make your database more attractive and informative. Well, this tutorial is written with the main perspective of providing you with helpful information on how to insert images in your Access database. So, let’s get started….!

Is It Possible To Insert Image In Microsoft Access?

Yes, it is possible to insert image in Access database. This task is possible by using the attachments that store several of your different file types in a single field.

In Access previous version the technique which is used is named Object Linking and Embedding (OLE). It stores documents, images but by default for each of your inserted documents and images, OLE creates a bitmap.

These bitmaps are quite bigger in size around 10 times greater than original file. So, when user approach to view their inserted image or document in their database. OLE only displays the bitmap image hiding the original one.

On the other hand, MS Access attachments help in storing attached images or file in their original formats. So, to view images or file inserted through MS Access attachments you don’t have install any additional software.

How To Insert Image In MS Access Database Table Through Attachments?

Let’s know how to insert images in MS Access database. So, for this just follow down the below-mentioned steps of this section.
At first, you have to add an attachment field in one of your tables in the Access database. Well in MS Access you can add attachments in your table in the following two ways i.e in Design view or Datasheet view. In this section, we will learn how to add an attachment to the datasheet view.

Add An Attachment Field In Datasheet View

• First of all you need to open your access database table in Datasheet view.
• After then tap on the first blank column. For fetching a blank column, on the column header just search for the words “Add New Field”.
• Tap to the Datasheet tab and then go to the Data Type & Formatting group.
• Hit the down arrow available next to the Data Type and then hit on the Attachment option.

Access sets the data type for the attachment field and puts an icon across the field’s header row.

In the shown figure you can see a new Attachment field. And can also see a paper clip icon in the header row of the field. Text insertion is not allowed in the attachment field’s header row.
• Save all the changes which you have performed.

Add An Attachment To A Table

• In the above section you successfully added an attachment field in your Access database table.

• Now double tap to the attachment fields. This opens a dialog box of attachments. As shown in the figure.
• Tap to add button and this will open the Choose File dialog box. From this, you can browse the documents or images you want to attach.
• You can also use the Look in list to search the files you need to insert in the MS Access record. After choosing the file hit on the Open tab.
• Now In your opened Attachments dialog box, hit on the OK option to add files or images into your table.

Perform the same step each time you need to add files in your current fields or any of your fields within the Access table.

How To Insert Image In Access By VBA code?

Within your access database create a memo field to store the image. For this, you just need to make use of the following code for the insertion of the image within the Access database through VBA coding:

Dim FN As Long
Dim FB

FN = FreeFile
Open "C:\Pict1.bmp" For Binary Access Read As FN
FileBinary = Space(LOF(FN))
Get #FN, , FB
Close #FN

'Open RecordSet RS
rs.AddNew
rs("ImgName") = "Picture1"
rs("ImgMem") = FB
rs.Update

For fetching image file From Access:

Dim MPict
Dim FN As Long
'Place an Image Control(Image1) on the Form
'Open Recordset RS here

MPict = RS("ImgMem")
If Dir("C:\Temp.bmp") <> "" Then
Kill "C:\Temp.bmp"
End If

FN = FreeFile
Open "C:\Temp.bmp" For Binary Access Write As #FN
Put #FN, , MPict
Close #FN

Image1.Picture = LoadPicture("C:\Temp.bmp")

You may also like: MS Access Database Repair – Fix Corrupted ACCDB & MDB Files

How to Insert Images into MS Access/SQL table using Java?

For inserting pictures into MS Access/SQL table through Java programming. To do this, firstly you need to create a table in your Access database. So, do it as instructed below?
Column Definition:
Column Name Data Type
ID AutoNumber
Images OLE Object

Table Definitions:

Table Name : Img[InsertPicture.mdb]
Primary Key: Nil
Foreign Key: Nil

Table Description:

Column Name Description
ID Create AutoNumber for image id
Image Image type "OLE Object"

After the table creation, you need to create the DSN. So, for this just make use of the below-mentioned source code of InsertPicture.java.

*/
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class InsertPicture

{

public static void main(String[] args) throws Exception, IOException

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url="jdbc:odbc:MyDsn";

Connection conn=DriverManager.getConnection(url);

String INSERT_PICTURE = "insert into Img(ID,Image) values (?, ?)";

FileInputStream fis = null;

PreparedStatement ps = null;

try

{

conn.setAutoCommit(false);

File file = new File("laura.jpg");

fis = new FileInputStream(file);

ps = conn.prepareStatement(INSERT_PICTURE);

ps.setString(1, "001");

ps.setBinaryStream(2, fis, (int) file.length());

ps.executeUpdate();
conn.commit();

}

finally

{

ps.close();

fis.close();

}

}

}

It’s time to compile the above code by making use of javac compiler. At last run the program.

C:\jdk1.4\bin javac InsertPicture.java
C:\jdk1.4\bin java InsertPicture

By using the above code you can easily insert picture under the img table.

Wrap up:

Try all the methods instructed above to insert image in Access database. I have tried my best to share every pinch of information on this topic with you all.

Good luck guys…!

source:

Top comments (0)