In VB, there are three ways to connect an Access database, including the following
using the ADO object to access the database by writing code Connection Objects ODBC Data source using the ADO Data control to quickly create a database connection There are three ways to connect Data Link File use ODBC data source name Using connection Strings To create a database connection by using the Data Environment designer Add an environment reference first. then locate the data source to connect
Either connection is preceded by a path that leads to the data source to be connected, and the data source is established in the connection. Just like from Beijing to Shanghai, you can make cars, you can do the train, you can do the plane, the car is high-speed, the train to take the railway, the plane route, the process is not the same, but the result is the same, are to reach Shanghai destination.
When you operate these three connection modes, you will find that there are duplicate actions.
In the data control connection, the connection between the ODBC data source and the connection using the string and the direct use of the ODBC data source connection are different from using the ODBC data source directly after setting the path, then connecting with the Connection object, and the data control does not use the Connection object. There are controls, set a good path, directly connected to the.
Both the Data Environment Designer and the ADO object use the connection connection. The difference is that in the Data Environment Designer, the connection connection path is the same as the concatenation of the strings in a data control.
Summary of different: ADO object connection needs to write code, others do not have to write code.
The detailed steps for each of the connection methods are detailed below:
1, using the ADO Connection object
First, you create an ADO reference, define the connection class, and then instantiate the object.
The code completes as follows:
Dim objCN as New Connection, objRS as new Recordset
objcn.connectionstring = "provider=microsoft.jet.oledb.3.51;" &am P "Data source=" & App.Path & "\ instance 01.mdb"
Objcn.open
2, using an ODBC data source
First, open the ODBC data source on your computer and establish a relationship.
The steps are as follows:
1. Open the ODBC data source on your computer
As shown in the following illustration:
2) Click Add
3) naming
4 and the database source to establish a relationship
5) Connect with code
Dim objcn As Connection
Set objcn = New Connection
objcn.open "dsn= instance 2DSN"
3,ado the Data control to create a connection
1) Right-click the data control and select the Adodc property
2 Select one of the three connection resources.
Using the data Link file: Click Browse directly to find the. udl file that contains the connection string.
Using an ODBC data source connection, click New
Select the user database, followed by the same steps as in 2.
Use connection string: Click Build
Test connection
4, creating a database connection using the Data Environment designer
Adding references
Right-click to create a connection, right-click the property you want to connect to, and then follow the action in the data control that uses the string.