|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
ADO: Binding Controls To Record SetsTo bind a control to an ADO Recordset object, you just set that controls DataSource property to that object, and then set whatever other data properties that control needs to have set (see, for example, Table 24.1 in the previous chapter, which lists the data properties of various controls). Lets see an example. In our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), we create a record set, adoRecordset, and open the db.mdb database we created in the last chapter in it. We can bind the fields in that database to the text boxes Text1 and Text2 this way when the adocode main form loads:
Private Sub Form_Load()
Dim db As Connection
Set db = New Connection
db.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data _
Source=C:\vbbb\adocode\db.mdb;"
Set adoRecordset = New Recordset
adoRecordset.Open "select Grade, Name from students", _
db, adOpenStatic, adLockOptimistic
Set Text1.DataSource = adoRecordset
Text1.DataField = "Name"
Set Text2.DataSource = adoRecordset
Text2.DataField = "Grade"
End Sub
Thats all it takesnow weve bound two text boxes to an ADO record set. ADO: Adding A Record To A Record SetTo add a new record to an ADO record set, you use the AddNew method. After youve updated the fields of the current record, you save that record to the database with the Update method. Heres how you use AddNew: recordset.AddNew [ Fields [, Values]] Here are the arguments for this method:
Lets see an example. Here, well add a new record to the record set adoRecordset in our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), when the user clicks the appropriate button:
Private Sub cmdAdd_Click()
On Error GoTo ErrLabel
adoRecordset.AddNew
Text1.Text = ""
Text2.Text = ""
Exit Sub
ErrLabel:
MsgBox Err.Description
End Sub
Note that we also clear the two text boxes that display the field data, Text1 and Text2, so users can enter the data they want in the new record. When done, they press the Update button to update the data source. ADO: Refreshing The Record SetSometimes you want to refresh the data in a record setyou might be dealing with multiply-connected databases, for instance, where other users are making changes as welland you can use the ADO Refresh method for that. Lets see an example. Here, well refresh the record set adoRecordset in our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), when the user clicks the appropriate button:
Private Sub cmdRefresh_Click()
On Error GoTo ErrLabel
adoRecordset.Requery
Exit Sub
ErrLabel:
MsgBox Err.Description
End Sub
And thats all it takes to refresh the record set. ADO: Updating A Record In A Record SetAfter changing the data in a records fields or adding a new record, you update the data source to record the change, using the Update method: recordset.Update Fields, Values Here are the arguments for this method:
Lets see an example. When users want to update records in our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), they click the appropriate button, and well update the data source this way:
Private Sub cmdUpdate_Click()
On Error GoTo ErrLabel
adoRecordset.Update
Exit Sub
ErrLabel:
MsgBox Err.Description
End Sub
Thats all we neednow were ready to update records in an ADO record set. ADO: Moving To The First Record In A Record SetTo move to the first record in an ADO record set, you use the Recordset objects MoveFirst method (this method takes no parameters). Lets see an example. When the user clicks the appropriate button in our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), well move to the first record in our record set, adoRecordset:
Private Sub cmdFirst_Click()
On Error GoTo ErrLabel
adoRecordset.MoveFirst
Exit Sub
ErrLabel:
MsgBox Err.Description
End Sub
And thats all the code we need to move to the first record. ADO: Moving To The Last Record In A Record SetTo move to the last record in an ADO record set, you use the Recordset objects MoveLast method (this method takes no parameters). Lets see an example. When the user clicks the appropriate button in our ADO code example, the adocode example (see A Full-Scale ADO Example in this chapter), well move to the last record in our record set, adoRecordset:
Private Sub cmdLast_Click()
On Error GoTo ErrLabel
adoRecordset.MoveLast
Exit Sub
ErrLabel:
MsgBox Err.Description
End Sub
And thats all the code we need to move to the last record.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |