Microsoft Access connectivity in Delphi with UniDAC
Elevate your Delphi and Lazarus database applications by connecting Microsoft Access using Universal Data Access Components (UniDAC) to make your database interactions more powerful and flexible
UniDAC (Universal Data Access Components), developed by Devart, is a comprehensive library of components designed to provide direct access to various databases from Delphi, C++Builder, and Lazarus.
With UniDAC’s server-independent interface, you can work across different databases and change the client engine for any server type by modifying a single connection setting. This flexibility simplifies the process of switching between database servers in a cross-database environment.
Why Choose UniDAC
Ease of setup and use
UniDAC is straightforward to install and use, any integrations with it can be set up in minutes, which ensures a quick and hassle-free start for developers.
Server-aware providers
UniDAC uses server-aware providers to optimize performance, guaranteeing that data manipulation operations are executed in the most efficient manner.
Cross-platform compatibility and cross-IDE support
UniDAC supports multiple platforms and IDEs, including Delphi, C++Builder, Lazarus, and more. It allows development on Windows, macOS, Linux, iOS, and Android for both x86 and x64 architectures, along with support for the FireMonkey platform.
Server-independent SQL
UniDAC features a powerful macros engine that enables server-independent SQL, allowing dynamic SQL generation and cross-database support within a single application.
Access to cloud services
UniDAC supports integration with popular cloud services such as Salesforce, QuickBooks, Zoho CRM, and more, enabling cloud-based data management in applications.
Secure connection
UniDAC can be integrated with SecureBridge components to embed secure protocols like SSL, SSH, or HTTPS, allowing for secure connections to the database server.
Support for EntityDAC
UniDAC works with EntityDAC, allowing developers to use Object-Relational Mapping (ORM) for Delphi, with support for LINQ, making database queries more intuitive and streamlined.
Connect to Microsoft Access from Delphi via UniDAC
Prerequisites
RAD Studio installed on your system
UniDAC from Devart installed
Microsoft Access installed or access to an .mdb or .accdb file
Configure connection to Microsoft Access via UniDAC
Start your RAD Studio.
Open or create a new project. To do this, navigate to File > New > Windows VCL Application – Delphi.
From the UniDAC category on the Component palette, drag and drop the TUniConnection component onto your form
Double-click the UniConnection1 label on the form, and in the dialog that opens, configure the following TUniConnection properties.
In the Provider dropdown menu, select Access.
In the Database field, enter the path to the Microsoft Access file.
Clear the LoginPrompt checkbox.
On the Options tab, set the Direct property to True.
On the Connect tab, click Connect.
Configure connection to Microsoft Access via UniDAC
You can use UniDAC components like TUniQuery, TUniTable, or TUniStoredProc to execute SQL queries, access tables, or perform other operations with data in your Microsoft Access database. Below is an example of how to configure a connection to an Access database file directly in your code.
procedure TForm1.ConnectToAccess;
begin
UniConnection1.ProviderName := 'Access'; // Set provider to Microsoft Access
UniConnection1.Database := 'C:\path\to\your\database.mdb'; // Specify path to your
Access file
UniConnection1.LoginPrompt := False; // Disable login prompt
UniConnection.SpecificOptions.Values['Direct'] := 'True'; // Enable direct connection
try
UniConnection1.Connect; // Attempt to connect
ShowMessage('Connection successful!');
except
on E: Exception do
ShowMessage('Failed to connect: ' + E.Message);
end;
end;
This code example can be placed within a method that is called when your form is created or triggered by a specific event. It establishes a direct connection to the Access file without requiring Microsoft Access to be installed on the machine.
Retrieve Microsoft Access data via UniDAC
UniDAC allows you to connect directly to Microsoft Access and retrieve data from it. To retrieve data from Microsoft Access:
From the UniDAC on the Component palette, drag and drop the TUniQuery and TUniDataSource components onto your form.
Then, drag and drop the TAccessUniProvider component onto your form from the UniDACProviders category.
Similarly, add the TButton component from the Standard category and TDBGrid from the Data Controls category.
Double-click the UniQuery1 label in the Form Designer and enter a SQL query to select the data from the Microsoft Access database.
For example,
SELECT * FROM emp;
Click OK to save changes and close the dialog.
Click the DBGrid1 label in the Form Designer and set the DataSource property to the UniDataSource1 value in the Object Inspector.
Click the UniDataSource1 label in the Form Designer and set the DataSet property to the UniQuery1 value in the Object Inspector.
Call the Open method in your application to execute the SQL query: double-click the Button1 label, and in the Code Editor that opens, add the following code:
UniQuery1.Open
The complete procedure code should look like this:
procedure TForm2.Button1Click(Sender: TObject);
begin
UniQuery1.Open
end;
end.
Click Run
In the window that opens, click the button to display the result.
View tables in a Microsoft Access database using UniDAC
Steps to view tables in a Microsoft Access database using UniDAC in RAD Studio (Design mode):
From the Component Palette, under the UniDAC category, add the TUniTable component to your form.
Double-click the TUniTable1 label on your form to open the configuration dialog.
In the dialog that appears, open the Table Name dropdown list to view and select tables from your Access database.
The TUniTable component in UniDAC is designed to work with entire database tables in a dataset-like manner. It allows for easy access to table data without writing SQL queries. You can set the TableName property to the desired table, open the connection, and retrieve or modify data.
View the list of database tables using TUniQuery:
In the Form Designer, double-click the UniQuery1 label to open the query editor.
Enter the following SQL query to select the list of tables in a Microsoft Access database:
SELECT Name
FROM MSysObjects
WHERE Type = 1;
To exclude system tables, modify the query as follows:
SELECT Name
FROM MSysObjects
WHERE Type = 1 AND Name NOT LIKE 'MSys%';
Click OK to close the dialog.
Click Run to execute the query.
Manage fields in the Form Designer
Right-click the UniQuery1 label on your form and select Fields Editor from the shortcut menu.
In the Fields Editor window, right-click inside the window and select Add All Fields from the context menu.
Alternatively, you can select New Field to add fields manually.
Manage field properties. After adding fields, you can manage their properties in the Object Inspector. Select each field in the Fields Editor to view and edit its properties, such as DisplayLabel, Visible, ReadOnly, Alignment, etc.
Execute queries
The TUniQuery component can be used not only for selecting data but also for executing any queries supported by the database server. For example, to update records in the emp table using an UPDATE statement, follow these steps:
Double-click the TUniQuery component to open the SQL editor.
Enter your query, such as:
UPDATE emp
SET Field1 = 'ANDERSEN', Field2 = 'ACCOUNTANT'
WHERE ID = 2;
Execute the query at design-time by clicking Run in the editor.
To execute the query at runtime, simply call the Execute method of TUniQuery in your code:
Double-click the TUniQuery component to open the SQL editor.
Enter your query, such as:
UPDATE emp
SET Field1 = 'ANDERSEN', Field2 = 'ACCOUNTANT'
WHERE ID = 2;
Execute the query at design-time by clicking Run in the editor.
To execute the query at runtime, simply call the Execute method of TUniQuery in your code:
UniQuery1.Execute;
Edit data in the Form Designer
To edit data, simply click any cell within the TDBGrid. This automatically invokes the Edit method, enabling you to modify the cell's contents. After making your changes, navigate to another record to save the update—this automatically triggers the Post method. If you want to cancel the changes to the current record, press ESC to revert the data before saving.
Edit data at runtime
You can use the following code to modify data programmatically at runtime:
To add new records, use TDBNavigator, which automatically triggers the Append or Insert method. TDBNavigator enables users to control the dataset when editing or viewing data. When you click a button on the navigator, the corresponding action is performed on the linked dataset. For example, clicking the Insert button adds a new blank record to the dataset.
From the Data Controls category on the Component palette, drag and drop the TDBNavigator component onto your form
Bind TDBNavigator to the Data Source.
2.1 Select the TDBNavigator component on your form.
2.2 In the Object Inspector, set the DataSource property of TDBNavigator to UniDataSource1.
Click Run and use the TDBNavigator buttons to insert new records and write changes to the database.
Delete records
Similarly, you can use TDBNavigator to delete records. The Delete method is called automatically.
To delete records from a Microsoft Access database, bind TDBNavigator to the UniDataSource. Then, run the application, select the record you want to delete, and click the Delete button on the navigator. The selected record will be removed from the database.
Insert or delete records at runtime
Insert records at runtime:
Use the Append method to create a new blank record.
Assign values to the necessary fields using FieldByName.
Use the Post method to save the new record to the database.
procedure TForm1.InsertRecord;
begin
UniQuery1.Append; // Start appending a new record
UniQuery1.FieldByName('Field1').AsString := 'Smith'; // Assign values to fields
UniQuery1.FieldByName('Field2').AsString := 'Manager';
UniQuery1.FieldByName('Field3').AsInteger := 1234;
UniQuery1.FieldByName('Field4').AsDateTime := Now;
UniQuery1.Post; // Save the record to the database
end;
Delete records at runtime:
Locate the record in the dataset.
Call the Delete method to remove the selected record.
procedure TForm1.DeleteRecord;
begin
UniQuery1.Delete; // Delete the current record
end;
Conclusion
UniDAC (Universal Data Access Components) is a powerful toolkit for connecting to various databases, including Microsoft Access, from a Delphi application. This solution enables developers to work with Access databases directly through the familiar Delphi interface. UniDAC significantly simplifies data management tasks, which makes it an essential tool for developers and database administrators who aim to streamline and optimize their interactions with Microsoft Access databases.
See how to install UniDAC on Windows, Linux, and macOS and connect to a database
UniDAC is a specifically designed Delphi Data Access Connector library that allows the development of
multi-platform applications in various environments via providing direct access to various Delphi, Lazarus,
and C++ Builder databases in 32-bit and 64-bit platforms of Windows, Linux, macOS, iOS, and Android.
Universal Data Access Components
Native connectivity to various databases and clouds from Delphi.