Using ADO to Communicate with the Database, Part 1 (Page 1 of 5 )
ADO provides developers with a powerful, logical object model for programmatically accessing, editing, and updating data from a wide variety of data sources. Its primary benefits are ease of use, high speed, low memory overhead, and a small disk footprint.
How to start the Communication with the Database?
To use the objects you must make a reference to them in your project. Go to Project/References and check the highest version of Microsoft ActiveX Data Objects. This could be 2.0 or 2.6.To start the communication with the database, there must be some procedure, which we need to follow. To start with, we need to declare a Connection Object.
What is a Connection?
A Connection object represents a physical connection to a database. When you open the Connection object, you attempt to connect to the database. The State property of the Connection object tells you whether you succeeded or failed. You can send SQL statements or run stored procedures by using the Execute method of the Connection object. If the command you send to the data store returns records, a Recordset object will be created automatically.
Referencing the Connection
Before you proceed, you need to refer the ADO (Microsoft Activex Data Object ver X.XX) in your project. To add the reference you need to go to the Project Menu and then select the Microsoft Activex Data Object X.XX.
Declaring a Connection
Before establishing a session with the database, you need to declare the Connection. Connection can be declared like any of the method below:
Dim objConn As ADODB.Connection
Set objConn = New ADODB.Connection
Or
Dim objConn As New ADODB.Connection
Opening a Connection
To open a Connection, we must make a connection string (Specifying Provider, Path of the database, User ID and Password etc) with the Database. At a minimum, you'll require to specify a Provider/connection string which indicates the type of the database, and a Data Source String, which is simply the path and file name of your database. To separate an Individual setting=value combinations of the connection string are separated with semi-colons.
Dim sConnect As String ‘ Declarign the Connection
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:databasemyaccessfile.mdb"
objConn.Open sConnect ‘ Opening the Connection
Provider for MS-Access 2000 Database file is 'Microsoft.Jet.OLEDB.4.0 Provider for MS-Access 97 Database its 'Microsoft.Jet.OLEDB.3.51.
Once our connection is established, we can read the records (Rows) of the table by using the ADODB Recordset object.
Next: What is a Recordset? >>
More Database Code Articles
More By Pradeep Chaudhary