Updating the Database
You will probably want to be able to update some value of some field when doing database programming. This is done with Edit and Update. We will try to change the value of the "Phone" field by editing the text in the text box and clicking a button.
Put a command button on the form and name it "cmdUpdate". Then copy the following code to the project.Private Sub cmdUpdate_Click()rsMyRS.EditrsMyRS!Phone = txtPhone.TextrsMyRS.UpdateEnd Sub
Could it be that simple? Yes. This changes the phonenumber of our selected person. Or to put it technically: This changes the value of the "Phone" field of our current record. Imagine the current record being a set of boxes, with a field in each box. T he Edit method takes the lid off all of the boxes and Update puts them back on. When we write rsMyRS!Phone = txtPhone.Text we replace the content of the "Phone" box with the content in the text box.
Deleting and Adding records
Deleting
Deleting records couldn't be simpler. To delete the current record you just invoke the Delete method of the RecordSet object. We will put this feature in our little project. Make one more command button named "cmdDelete" and the following code will do the work of deleting our currently selected person.Private Sub cmdDelete_Click()rsMyRS.DeletelstRecords.RemoveItem lstRecords.ListIndexEnd Sub
I won't even bother to explain that in greater detail =). The first statement deletes the record and the second removes the list box entry.
Adding
Adding records is much like updateing, except you use AddNew instead of Edit. Let's add one more command button to our application. Let's call it...errh...let me see...yea! "cmdNew" =). Here is the code that adds a new record. Private Sub cmdNew_Click()rsMyRS.AddNewrsMyRS!Name = "A New Person"lstRecords.AddItem rsMyRS!NamelstRecords.ItemData(lstRecords.NewIndex) = rsMyRS!IDrsMyRS!Phone = "Person's Phone Number"rsMyRS.UpdateEnd Sub
No comments:
Post a Comment