Several methods of connecting Access database in VB

Source: Internet
Author: User
Tags odbc access database

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.