Lesson 18 Database Management
Lesson 18 Database Management
DATA
MANAGEMENT
CONTENTS 01. DEFINITION OF
DATABASE
02. ADVANTAGE AND
DISADVANTAGE OF
USING DATABASE
03. PROCESS OF
CREATING
DATABASE
WHAT IS
DATABASE?
SetdbMyDB = OpenDatabase(“Database_File_name.mdb”)
DATABASE
OBJECT
Example:
SetdbMyDB = OpenDatabase(“MyDatabase.mdb”)
Set dbMyDB =
OpenDatabase(“MyDatabase.mdb”)
Set rsMyRS = dbMyDB.OpenRecordSet
(“MyTable”, dbOpenDynaset)
End Sub
ACCESSING RECORDS
This will make the list box fill up with your records
when the form loads. We have already covered the first
parts where we open the table. The line that says If Not
rsMyRS.EOF. Then rsMyRS.M OveFirst tells the
program to move to the first record in case there are any
records at all. If all EOF is a Boolean Property that is
true if the current record is the last. It is also true that
there are no records in the RecordSet
ACCESSING RECORDS
End Sub
SEARCHING THE
RECORDSET
This will display the phone number off the selected person when
clicking in the list box. It uses the FindFirst method of the RecordSet
object. This takes a string parameter that is like what is after
WHERE in a SQL expression. You state the field that you want to
search in(here “ID”) then the evaluation criteria (here “=”) and
last the value to search for (here the ITemData of the selected item
in the list box). So what we did was to search for the record with
the “ID” field value that was the same as the ItemData property of
the selected item in the list box. Then we show the value of the
“Phone” field in the text box.
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 “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.EDIT
RSMYRS!PHONE = TXTPHONE.TEXT
RSMYRS.UPDATE
UPDATING THE
DATABASE
Could it be that simple? Yes. This changes
the phone number 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. The Edit
method takes the lid off all 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 RECORDS
rsMyRS.Delete
1stRecords.RemoveItem
1stRecords.ListIndex
End Sub
ADDING RECORDS
End Sub
ADDING RECORDS
We will use the box analogy to explain this.
The AddNew method takes a set of new boxes
and adds them to our RecordSet. We then put
some new values in them and close the lids
with Udpate. As you can see we never stated
any value for “ID”, but as you remember, this
is a field of type “Counter” which
automatically gets a unique value. The code
also adds this new record to the list box so
that we will be able to change the phone
number of this person. I leave it up to you to
ad the feature of changing the name.
THANK YOU!