Sie sind auf Seite 1von 10

Lazarus and MS Access DB

Contents
1. Software requirements
2. Database Example Program

Lazarus and MS Access DB on Windows 7 64-bit1 of 10


Christopher W. Wells

Tuesday, 29 July 2014

1 Software requirements
You will need to make sure you have the following software before continuing with the example program:
1. Microsoft Windows 7 (32-bit / 64-bit) http://www.microsoft.com
2. Microsoft Access Database Engine 2010 Redistributable (32-bit) - http://www.microsoft.com/enza/download/details.aspx?id=13255
3. Your Microsoft Access Database file.
4. Lazarus 1.2.0 or newer - http://www.lazarus.freepascal.org/

2 Database Example Program


Below are the steps involved in using the installed software to create your first database application using
both Lazarus and MS Access.
1.
2.
3.
4.

Create a folder on the C: drive called Lazarus_MSAccess


Create a database called MyAccess.accdb
Add information to the database called MyAccess.accdb
Create the Lazarus / Delphi program

The above steps will all be highlighted in screen dumps for you below:

2.1 Create the folder Lazarus_MSAcess


2.1.1 Left click on the Start ( ) button on of Windows 7, navigate to All Programs, navigate to and left click
once on Accessories, left click on Windows Explorer.

Lazarus and MS Access DB on Windows 7 64-bit2 of 10


Christopher W. Wells

Tuesday, 29 July 2014

2.1.2 Left click on the right arrow of Computer, and then left click on your C: drive
N.B. The name of our C: drive is called Local Disk (C:) as shown in the diagram.

Lazarus and MS Access DB on Windows 7 64-bit3 of 10


Christopher W. Wells

Tuesday, 29 July 2014

2.1.3 Left click on the New Folder option from the file menu, rename the New Folder to
Lazarus_MSAccess and press the Enter key on your keyboard.

2.1.4 This ends the creation of the Lazarus_MSAccess folder.

2.2 Create the database MyAccess.accdb


2.2.1

Left click on the Start ( ) button on of Windows 7, navigate to All Programs, navigate to and left click
once on Microsoft Office, left click on Microsoft Access 2010.

2.2.2 A screen similar to the one shown below should now appear, left click on the New button, left click on
Blank database option and then left click on the

Lazarus and MS Access DB on Windows 7 64-bit4 of 10


Christopher W. Wells

Tuesday, 29 July 2014

2.2.3 A screen similar to the one shown below should now appear:

2.2.4 Rename the columns and activate the relevant database restrictions.
N.B. Below is our final changes.

Lazarus and MS Access DB on Windows 7 64-bit5 of 10


Christopher W. Wells

Tuesday, 29 July 2014

Column1 = Student_ID of type NUMBER


Column2 = Fname of type TEXT
Column3 = Sname of type TEXT
Column4 = Subject of type TEXT

2.2.5 Left click on the Close button to have the below prompt appear, left click on Yes to accept the changes:

2.2.6 The below prompt will now appear, rename the table to Student and then left click on OK to accept
the changes:

2.2.7 Your screen should now look similar to the one shown below, left click on the Student table and you
should see the results as shown above.

Lazarus and MS Access DB on Windows 7 64-bit6 of 10


Christopher W. Wells

Tuesday, 29 July 2014

2.2.8 Left click on File, navigate and left click on Save Database As, locate the Lazarus_MSAccess folder and
save the database file as MyAccess.

2.3 Add information to the database called MyAccess.accdb


2.3.1 Left click on the Student table, a similar screen shown below should now be displayed.

2.3.2 Simply fill in the required information for column as you require.
2.3.3 Repeat the above steps to add more rows of data.
2.3.4 Left click on the Save button, when complete, close the database. This now concludes the creation of
the SQLite database sqlite1.db

2.4 Create a Lazarus Project


2.4.1

Left click on the Start ( ) button on of Windows 7, navigate to All Programs, navigate to and left click
once on Lazarus, left click on Lazarus.

Lazarus and MS Access DB on Windows 7 64-bit7 of 10


Christopher W. Wells

Tuesday, 29 July 2014

2.4.2 A screen similar to the one shown below should now appear, left click on the + button.

Lazarus and MS Access DB on Windows 7 64-bit8 of 10


Christopher W. Wells

Tuesday, 29 July 2014

For this project, we will be requiring the following:

TODBCConnection This is under the SQLdb tab


TDataSource This is under the Data Access tab
TSQLQuery - This is under the SQLdb tab
TSQLTransaction - This is under the SQLdb tab
TDBGrid - This is under the Data Controls tab

TSQLQuery

TSQLTransaction

SQLdb tab

Use these
arrows to
expand your tabs

TODBCConnection

2.4.3 Your screen should now look similar to the one shown below after adding all the above controls.

2.4.4 Add a button and a label to your form, remember to rename all your components. I have only
renamed the Form, Button and Label in my example.

Lazarus and MS Access DB on Windows 7 64-bit9 of 10


Christopher W. Wells

Tuesday, 29 July 2014

2.4.5 Add the following code to your button click event and test the program:
//Required specification for MS Access Connection
ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb, *.accdb);
ODBCConnection1.params.add(DBQ=+ExtractFilePath(Application.ExeName) + MyAccess.accdb);
ODBCConnection1.Connected := True;
ODBCConnection1.KeepConnection := True;
//Transaction Part for MS Access Connection
SQLTransaction1.Database:= ODBCConnection1;
//Create the Query for MS Access Connection
SQLQuery1.Database:=ODBCConnection1;
SQLQuery1.UsePrimaryKeyAsKey:=False;
SQLQuery1.SQL.Text:='SELECT * FROM Student';
//Assign the DataSource for MS Access Connection
Datasource1.DataSet:=SQLQuery1;
DBGrid1.DataSource:=DataSource1;
// End of connection string
//This lets you know via the label if you are connected or not
If ODBCConnection1.Connected then
begin
lblStatus.caption:='Connected';
end;
// End of label notification
//This starts the query which was created in the connection string
// This will also pass the information through to the Grid
SQLQuery1.open;
2.4.6 If the above steps have been followed, then you should see the results of the information in your
database displayed in Lazarus. Enjoy.

Lazarus and MS Access DB on Windows 7 64-bit10 of 10


Christopher W. Wells

Tuesday, 29 July 2014

Das könnte Ihnen auch gefallen