Upload
jateen-moti
View
226
Download
0
Embed Size (px)
Citation preview
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
1/10
Lazarus and MS Access DB on Windows 7 64-bit1 of 10 Tuesday, 29 July 2014
Christopher W. Wells
Lazarus and MS Access DB
Contents
1. Software requirements
2. Database Example Program
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
2/10
Lazarus and MS Access DB on Windows 7 64-bit2 of 10 Tuesday, 29 July 2014
Christopher W. Wells
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/en-
za/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. Create a folder on the C: drive called Lazarus_MSAccess
2. Create a database called MyAccess.accdb
3. Add information to the database called MyAccess.accdb
4.
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 toAll Programs, navigate to and left click
once onAccessories, left click on Windows Explorer.
http://www.microsoft.com/http://www.microsoft.com/http://www.microsoft.com/http://www.microsoft.com/en-za/download/details.aspx?id=13255http://www.microsoft.com/en-za/download/details.aspx?id=13255http://www.microsoft.com/en-za/download/details.aspx?id=13255http://www.microsoft.com/en-za/download/details.aspx?id=13255http://www.lazarus.freepascal.org/http://www.lazarus.freepascal.org/http://www.lazarus.freepascal.org/http://www.lazarus.freepascal.org/http://www.microsoft.com/en-za/download/details.aspx?id=13255http://www.microsoft.com/en-za/download/details.aspx?id=13255http://www.microsoft.com/8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
3/10
Lazarus and MS Access DB on Windows 7 64-bit3 of 10 Tuesday, 29 July 2014
Christopher W. Wells
.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.
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
4/10
Lazarus and MS Access DB on Windows 7 64-bit4 of 10 Tuesday, 29 July 2014
Christopher W. Wells
2.1.3 Left click on the New Folder option from the file menu, rename the New Folder to
Lazarus_MSAccessand 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 toAll 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 databaseoption and then left click on the
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
5/10
Lazarus and MS Access DB on Windows 7 64-bit5 of 10 Tuesday, 29 July 2014
Christopher W. Wells
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.
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
6/10
Lazarus and MS Access DB on Windows 7 64-bit6 of 10 Tuesday, 29 July 2014
Christopher W. Wells
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 Yesto accept the changes:
2.2.6 The below prompt will now appear, rename the table to Studentand then left click on OKto 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.
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
7/10
Lazarus and MS Access DB on Windows 7 64-bit7 of 10 Tuesday, 29 July 2014
Christopher W. Wells
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 toAll Programs, navigate to and left click
once on Lazarus, left click on Lazarus.
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
8/10
Lazarus and MS Access DB on Windows 7 64-bit8 of 10 Tuesday, 29 July 2014
Christopher W. Wells
2.4.2 A screen similar to the one shown below should now appear, left click on the + button.
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
9/10
Lazarus and MS Access DB on Windows 7 64-bit9 of 10 Tuesday, 29 July 2014
Christopher W. Wells
For this project, we will be requiring the following:
TODBCConnectionThis is under the SQLdb tab
TDataSourceThis 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
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.
TSQLQuery TSQLTransaction TODBCConnection
SQLdb tab
Use these
arrows to
expand your tabs
8/10/2019 Lazarus and MS Access DB on Windows 7 64-bit.pdf
10/10
Lazarus and MS Access DB on Windows 7 64-bit10 of 10 Tuesday, 29 July 2014
Christopher W. Wells
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 GridSQLQuery1.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.