0% found this document useful (0 votes)
42 views34 pages

Lesson 18 Database Management

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views34 pages

Lesson 18 Database Management

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

LESSON 18:

DATA
MANAGEMENT
CONTENTS 01. DEFINITION OF
DATABASE
02. ADVANTAGE AND
DISADVANTAGE OF
USING DATABASE

03. PROCESS OF
CREATING
DATABASE
WHAT IS
DATABASE?

A database is a collection of records,


that can be sorted, removed,
searched, etc.
WHAT IS
DATABASE?

A Database allows you to store multiple


pieces of information in one file, instead of
using several files for each piece of data. A
Database normally contains Many fields of
data.
EXAMPLE OF
DATABASE:

For example, you are keeping information about your record


collection, so you have a field called “ARTIST”, a field called
“RECORD” and a field called “TYPE”, now when you put
information into the three fields they are stored in that order in
the file. The information ais also retrieved in that order when
you wish to obtain the data. Storing the data in fields means that
you can easily search the data if you know what field to search.
Additional
ADVANTAGE
OF information
USING can be derived
DATABASE
from the same
data.
ADVANTAGE
OF Improves
security.
USING
DATABASE
ADVANTAGE
OF Cost efficient.
USING
DATABASE
Multiuser
DISADVANTAGE
Database
OF
Management
USING
Systems can DATABASE
be expensive.
Performance
can vary, DISADVANTAGE
depending on OF
the machine USING
DATABASE
using.
Damage to
database
DISADVANTAGE
affects
OF
virtually all
USING
applications DATABASE
programs.
PROCESS
OF
CREATING
DATABASE
DATABASE
OBJECT
In order to create a database object, you must open
a database where your tables are stored. You need
to declare a variable to hold your database in order
to do this.

To do this, you must type

Dim dbMyDB as Database


DATABASE
OBJECT

This gives you a variable or object that can hold


a reference to your database. To open a simple
access database named “MyDatabase.mdb”, do
this:”

SetdbMyDB = OpenDatabase(“Database_File_name.mdb”)
DATABASE
OBJECT
Example:

SetdbMyDB = OpenDatabase(“MyDatabase.mdb”)

Note: You should really specify the complete path of


the database, but if your current directory is the
directory where the database is situated, this will work.
DATABASE
OBJECT

Now that you have opened a


database, this won’t give you any
data. What you need to do is open a
table in the Database.
RECORDSET
OBJECT

Visual Basic uses an object called RecordSet to


hold your table. TO declare such an object and
to open the table, do this:

Dim rsMyRS As RecordSet


Set rsMyhRS = dbMyDB.OpenRecordSet(“MyTable, dbOpenDynaset”)
RECORDSET
OBJECT

Using that, we have declared a


RecordSet object and used the Database
Object’s OpenRecordSet method to open
a table of type Dynaset.
ACCESSING RECORDS

Now that we have opened a table (Referred to as


RecordSet from now on) we want to access the
records in it. The RecordSet object allows us to
move in it by using the methods MoveFirst,
MoveNext, MovePrevious, MoveLast (Among
others). We will use some of these to fill up a list
box with the records of our RecordSet
ACCESSING RECORDS

To get this example to work, make a database


(With Access) called “MyDatabase.mdb” with the
table “MyTable” in it. This table should have the
fields “ID” of type “Counter” that you set to be the
primary key, the field “Name” of type text and a
field “Phone” of type Text. Add some records to it.
Put a list box on a form and call it “IstRecords.”
Dim dbMyDB as Database
Dim rsMyRS as RecordSet

Private Sub Form_Load()

Set dbMyDB =
OpenDatabase(“MyDatabase.mdb”)
Set rsMyRS = dbMyDB.OpenRecordSet
(“MyTable”, dbOpenDynaset)

If not rsMyRS.EOF Then rsMyRS.MoveFirst


Do while Not rsMyRS.EOF
1stRecords.AddItem rsMyRS! Name
1stRecords.ItemData(1stRecords.NewIndex)=
rsMyhRS!ID rsMYRS.MoveNext
Loop

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

Then we make the program add the “Name” field of


all records to the list box by adding the current
records field “Name” and moving to the next record.
You ask for a field of RecordSet by putting a !
between the name of the RecordSet object an d the
name of the field. While the loop checks to see if
there are more records to add.
SEARCHING THE
RECORDSET
You might have wondered why we didn’t put the
value of the field “ID” in the list box’s ItemData
property. I did this so that we would know the
primary key for all the Records in order to search for
a record. Put a text box somewhere on the form and
call it “txtPhone”. The ncopy the following code to
the project
SEARCHING THE
RECORDSET
Private Sub 1stRecords_Click()

rsMyRS.FindFirst “ID=” &


Str(1stRecords.ItemData(1stRecords.ListIndex))txtP
hone.Text = rsMyRS!Phone

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

Deleting records coudnt 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 following code will
do the work of deleting our currently
selected person.
DELETING RECORDS

Private Sub cmdDelete_Click()

rsMyRS.Delete
1stRecords.RemoveItem
1stRecords.ListIndex

End Sub
ADDING RECORDS

Adding records is much like


updating, except you use
AddNew instead of Edit. Let’s
add one more command button
to our application. Lets call it
“cmdNew” Here is the code
that adds a new record.
ADDING RECORDS

Private Sub cmdNew_click ()


rsMyRS.Addnew
1stRecoerds.AddItem rsMyRS!Name
1stRecords.ItemData(1stRecords.Newindex) =
rsMyRS!ID
rsMyRS!Phone = “Person’s Phone Number”
rsMyRS.Update

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!

You might also like