VB program how to access the database

Source: Internet
Author: User
Tags define dsn interface



Accessing the database through the "Data" control

(1) Start VB.
(2) The new tab for the new project appears as shown in Figure 10.12.

(3) The VB main interface appears as shown in Figure 10.13.

(4) as shown in Figure 10.14.

(5) The Properties window interface appears as shown in Figure 10.15.

―――――――――――――――――――――――――――――――――――――
Odbc;dsn=graduatedb;uid=scott;pwd=tiger;
―――――――――――――――――――――――――――――――――――――
(6) as shown in Figure 10.16.

(7) Select the TextBox control named Text1 that appears on the Form1 form.
(8) The Properties window of the TextBox control appears as shown in Figure 10.17.
(9) Add the "TextBox" control named Text2 in the same step as TEXT1, and its primary setting is.
(10) Add the "TextBox" control named Text3 in the same step as TEXT2, and its primary setting is.
(11) The design of the form Form1 as shown in Figure 10.18.

(12) In the VB main interface of the "Shortcut button area" Click the Run Shortcut button, as shown in Figure 10.19.

(13) The results of the operation are shown in Figure 10.20.


(14) The reader can find the matching program of the example from the supporting CD of this book.
"See Disc Files": Chapter 10th \ uses the data control to access the database. vbp, the form is form1.frm.
(15) Use a table to represent the setup of the form in the above procedure, as shown in table 10.1.


Accessing the database through the "ADODC" control

(1) Start VB, set up new project.
(2) as shown in Figure 10.21, the VB main interface of the "menu bar" Select the "Project"/"part" option.

(3) The Controls tab of the part shown in Figure 10.22 appears.
(4) as shown in Figure 10.23.

(5) as shown in Figure 10.24.
(6) The General tab of the property page shown in Figure 10.25 appears.

(7) Switch to the Authentication tab of the property page as shown in Figure 10.26.
(8) Switch to the RecordSource tab of the property page as shown in Figure 10.27.

(9) The main property settings are shown in Figure 10.28.

(10) Add a "TextBox" control named Text2 to the Form1 form.
(11) Add a "TextBox" control named Text3 to the Form1 form.
(12) The reader can find the matching program of the example from the supporting CD of this book.
"See Disc Files": Chapter 10th \ uses the ADODC control to access the database. vbp, form is form2.frm.
(13) Use a table to represent the setup of the form in the above procedure, as shown in Table 10.2.


(14) The results of the program operation are shown in Figure 10.29.


Accessing the database through the "ADODB" programming object

1. Refer to the "ADODB" object
To use the "ADODB" object, you must first refer to it.
(1) Start VB, new project.
(2) as shown in Figure 10.30.
(3) The "Reference" interface appears as shown in Figure 10.31.

2. Programming with "ADODB" objects
(1) Design the form Form3 as shown in Figure 10.32.

Use tables to represent the settings for forms in the above procedure, as shown in table 10.3.


(2) The reader can find the matching program of the example from the supporting CD of this book.
"See CD-ROM Files": 10th chapter \ Use ADODB object programming to access the database. vbp, form is form3.frm.
(3) Form FORM3 All VB code is as follows.
―――――――――――――――――――――――――――――――――――――
' Mandatory declaration of variables before using
Option Explicit
' Defines the recordset for RS as a ADODB object
Dim Rs as New ADODB. Recordset
' Defines a connection for a ADODB object conn
Private Conn as ADODB. Connection
' Form runtime initialization process
Private Sub Form_Load ()
' Define CONNSTR as a connection string
Dim ConnStr as String
' defines the command string for cmd ADODB
Dim CMD as New Adodb.command
Set conn = New ADODB. Connection
ConnStr = "Dsn=graduatedb;uid=scott;pwd=tiger;"
Conn. ConnectionString = ConnStr
' Connect the database according to CONNSTR content
Conn. Open
' Query the contents of the datasheet dept
With CMD
. ActiveConnection = conn
. CommandType = adCmdTable
. CommandText = "Dept"
End With
' Send the contents of a datasheet dept to the recordset RS
With Rs
. CursorLocation = adUseClient
. CursorType = adOpenStatic
. LockType = adLockPessimistic
. Open CMD
End With
' Defines 3 text boxes with the data source of RS
Set Text1.datasource = Rs
Set Text2.datasource = Rs
Set Text3.datasource = Rs
' Define the fields displayed by the 3 text boxes
Text1.datafield = "DEPTNO"
Text2.datafield = "Dname"
Text3.datafield = "LOC"
End Sub
' Click the run process for the first record button
Private Sub Cmdfirst_click ()
Rs.movefirst
Rs.update
End Sub
' Click on the ' record ' button to perform the procedure
Private Sub cmdPrevious_Click ()
Rs.moveprevious
If Rs.bof Then
Rs.movefirst
Rs.update
End If
End Sub
' Click the Down Record ' button to perform the procedure
Private Sub cmdNext_Click ()
Rs.movenext
If rs.eof Then
Rs.movelast
Rs.update
End If
End Sub
' Click the ' End Record ' button to perform the procedure
Private Sub cmdLast_Click ()
Rs.movelast
Rs.update
End Sub
' Click the ' Exit ' button to perform the procedure
Private Sub Cmdexit_click ()
Conn. Close
Set conn = Nothing
Unload Me
End Sub
―――――――――――――――――――――――――――――――――――――
(4) The results of the form Form3 are shown in Figure 10.33.



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.