Data Binding: it refers to the association between a control and a specific field of the data source. When a program is running, the control automatically displays data from the data source.
A simple understanding is to establish a connection between the binding target and the binding source, and access the database by binding the target.
This example details the binding between the ADO Data Control and the data source in VB, which makes it easier for us to bind data.
The following describes how to use the data control to help the database and how to use the code to "replace" the control.
I. features:
You can access the database by clicking the buttons 1, 2, 3, and 4.
1) Click the first button to access the first record in the database. The second button is the previous record of the current record. The third button is the last record of the current record. The fourth button is the last record in the database.
2) When using the Data Control to access the database, we need to set the data source path, the specific connection is described in detail in the previous blog "several methods for connecting to the ACCESS database in VB", and then set the user name and password to the datasource and datafield in the text box.
You can.
3) display of the data control is achieved by setting the caption attribute of the control.
Ii. Data Control Analysis
The data control can be used to access the database in such a simple way. How can it be implemented. How to replace the Data Control with code is described as follows:
1. Interface
2. Code
1) database binding
Set objcn = new connection set objrs = new recordset objcn. connectionstring = "provider = Microsoft. jet. oledb.3.51; "&" Data Source = "& App. path & "\ instance 01.mdb" objcn. open strsql = "select * from system user" setobjrs. activeconnection = objcn 'path objrs. openstrsql, objcn, 1' Retrieve Information
2) Browser
If notobjrs. bof then objrs. moveprevious ifobjrs. bof then objrs. movelast txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value exitsub else text1.text = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value 'exit sub end if
If notobjrs. EOF then objrs. movenext ifobjrs. EOF then objrs. movefirst txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value exitsub else txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value end if 'exitsub end if
3) The entire code process:
Option explicitdim objcn as connection, objrs as recordset, strsql asstring private sub into pre_click () If notobjrs. bof then objrs. moveprevious ifobjrs. bof then objrs. movelast txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value exitsub else txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value 'exit sub end if end ifend sub private sub partition next_click () If notobjrs. EOF then objrs. movenext ifobjrs. EOF then objrs. movefirst txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value exitsub else txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value end if end sub private sub form_load () set objcn = new connection set objrs = new recordset objcn. connectionstring = "provider = Microsoft. jet. oledb.3.51; "&" Data Source = "& App. path & "\ instance 01.mdb" objcn. open strsql = "select * from system user" setobjrs. activeconnection = objcn 'path objrs. openstrsql, objcn, 1' Retrieve Information txtname. TEXT = objrs. fields ("username "). value txtpassword. TEXT = objrs. fields ("password "). value end sub private sub form_unload (cancel as integer) objcn. close set objcn = nothing set objrs = nothing end sub
The complex data control is replaced by several lines of code. Hey.