Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 8

Adding a Record to a Recordset - Visual Basic (VB6)

To add new records to a Recordset programmatically, you can use a combination of the Recordset's AddNew and Update methods.

AddNew appends a temporary record buffer to the cursor's rowset. You can take the following steps in code to add a new record:

  1. If you are providing controls to the user for field editing, set the controls to blank or default values. Next, enable the user to add new data to the controls.

  2. When the user is ready to save the new data, invoke the AddNew method of the Recordset. The cursor is now pointing to a temporary new record.

  3. Assign the desired values to the individual fields of the current record (the temporary blank record). If you have given the user controls to edit field contents, then assign those control contents to the fields.

  4. Call the Recordset's Update method.

The code to implement these four steps might be contained in the Click event procedures for buttons with captions such as Add and Save New. The code in Listing 8.8 provides you with such an example of how to add a new record.

LISTING 8.8
ADDING AND SAVING A NEW RECORD

Private Sub cmdAdd_Click()
cmdSaveNew.Enabled = True
txtLastName = ""
txtSalary = "0"
txtFirstName = ""
End Sub
Private Sub cmdSaveNew_Click()
rsEmployees.AddNew
WriteControlsToData 'our routine to update copy buffer
rsEmployees.Update
cmdSaveNew.Enabled = False
End Sub

To enable the user to cancel adding a record while the user is editing fields, all you need to do is call the routine that refreshes controls from the copy buffer fields.

If you have already updated copy buffer fields programmatically, you will want to call the CancelUpdate method as well.

NOTE - Differences in Calls to AddNew: The timing of your calls to the ADO Recordset's AddNew method will probably differ from the timing of your call to the AddNew method of the Recordset belonging to a Data Environment or to an ADO Data Control when there are bound controls.

Whereas you might call the ADO Data Control or Data Environment Recordset's AddNew method as soon as the user decides to add a record, you probably don't want to call the ADO Recordset's AddNew method until it's time to save the edited data for the new record. You don't want to allocate extra resources before you need to.

See Also


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved