Database Learning Practice (III.)--Using data binding

Source: Internet
Author: User
Tags access database

Data binding: Is an allegation that is associated with a specific field in a data source, and the control can automatically display the data source data when the program is running.

The simple understanding is to establish a connection between the binding target and the binding source, and the database can be accessed through binding targets.

This example introduces in detail the binding between the ADO Data control and the source in VB, which makes us realize the convenience of data binding.

Here's a brief introduction to the data Control help point database and a detailed description of how to "replace" the control with code.

One: function Introduction:

Access to the database can be achieved by simply clicking on the 1,2,3,4 four buttons.

1 Click on the first button to access the first record in the database. The second button is the previous record in the current record. The third button is the last record in the current record. The fourth button is the last record in the database.

2 in the use of data control access to the database, we need to set the path to the source, the specific connection in the previous blog "Detailed VB Connection Access database methods" in detail, Then set the username and password datasource and DataField of the text box.

3 The data control is displayed, it is implemented by setting the Caption property of the control.

Second: Profiling the Data control

The data control is so simple to achieve access to the database, then how it is implemented. So how to replace the data control with code, the following detailed:

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 ' fetching information

2) The realization before and after browsing

    If notobjrs.bof Then

        objrs.moveprevious

        

        ifobjrs.bof Then

           objrs.movelast

 

           . Text = Objrs.fields ("User name"). Value

           Txtpassword. Text = objrs.fields ("password"). Value

            exitsub

        Else

            text1.text= objrs.fields ("User name"). Value

           Txtpassword. Text = objrs.fields ("password"). Value

           ' Exit Sub end

 

        if

 


    If notobjrs.eof Then

       objrs.movenext

 

        ifobjrs.eof Then

           objrs.movefirst

            . Text = Objrs.fields ("User name"). Value

           Txtpassword. Text = objrs.fields ("password"). Value

            exitsub

        Else

           txtname. Text = Objrs.fields ("User name"). Value

           Txtpassword. Text = objrs.fields ("password"). Value End

        If

 

        ' Exitsub end

 

    if


3 The entire code process:

Option Explicit Dim objCN as Connection, objRS as Recordset, strSQL asstring Private Sub cmdpre_click () If Notob Jrs.bof Then objrs.moveprevious ifobjrs.bof Then objrs.movelast txt Name. Text = Objrs.fields ("User name"). Value Txtpassword. Text = objrs.fields ("password"). Value exitsub Else txtname. Text = Objrs.fields ("User name"). Value Txtpassword. Text = objrs.fields ("password"). 

       Value ' Exit Sub-end-if end-if End Sub Private Sub cmdNext_Click () If notobjrs.eof Then Objrs.movenext ifobjrs.eof Then Objrs.movefirst txtname. Text = Objrs.fields ("User name"). Value Txtpassword. Text = objrs.fields ("password"). Value exitsub Else txtname. Text = Objrs.fields ("User name"). Value Txtpassword. Text = objrs.fields ("password"). Value End If End If End Sub Private Sub Form_loaD () Set OBJCN =new Connection set objRS =new Recordset objcn.connectionstring = "PROVIDER=MICROSOFT.JET.O ledb.3.51; " & "Data source=" & App.Path & "\ instance 01.mdb" Objcn.open strSQL = "SELECT * from System User" SETOBJR

                                                                                                                                

    s.activeconnection = objCN ' path objrs.openstrsql, OBJCN, 1 ' fetching information Txtname. text= objrs.fields ("User name"). 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 seemingly complex data control is replaced by a few lines of code. Hey.

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.