|
| Creating database applications in VB |
|
Brief introduction to the usages of Access data bases
|
|
What I think is the most compelling thing about Visual Basic is it's easy way of modifying and accessing databases. This is what I think you should learn next and you will find many applications for this knowledge.Without using a database for data storage,i almost never make the program .
There are many ways to work with the databases in Visual Basic, and I would think you have at least glanced at the Data control and since it is so easy to use and too limited to be interesting for a professional developer,it will not even mention the Data control further in this text. (Ok, there are some exceptions to this.)
What I will teach you to use in this text is DAO (Data Access Objects) and you will get familiar with opening a database and adding/retrieving/deleting/updating records from tables. I will only use an Access Database (*.mdb) in my examples and since this is the most used DBMS (DataBase Management System) for smaller applications made in Visual Basic. We will at the end of this lesson have made a simple and yet functional, phone book application.
|
|
Database Object
|
|
The first thing you must do in your application is to open a database where your tables are stored and you need to declare a variable to hold your database in order to do this. This is done with following statement:
|
|
|
|
This gives you a object/variable that can hold a reference to your database.
To open a simple Access database named "MyDatabase.mdb" and do this:
|
|
Set dbMyDB = OpenDatabase("MyDatabase.mdb")
|
|
|
You should really specify the complete path to the db and if your current directory is the directory where the database is situated, this will work.
So, now you have opened a database and this won't give you any data. What you need to do is open a table in the database itself. You're not limited to open a single table and sometimes you have two or more tables that are related to each other and linked together w ith foreign keys, and there are ways to handle
|
|
RecordSet Object
|
|
To hold your table,Visual Basic uses an object called RecordSet.
To declare such an object and to open the table, write the following statement:
|
Dim rsMyRS As RecordSet
Set rsMyRS = dbMyDB.OpenRecordSet("MyTable", dbOpenDynaset)
|
|
|
What happened there? Well, I declared a RecordSet object and used the Database object's OpenRecordSet
method to open a table of type Dynaset and you can open a RecordSet in several modes. VB's online help file explains the different modes and what they ar e for. The Dynaset mode is the mode I use mostly and it gives you a RecordSet that you can add, delete and modify records in.
|
|
Accessing records
|
|
Now that we have opened a table (referred to as RecordSet from now on) we want to access the records in it and the RecordSet object allows us to move in it by using the methods MoveFirst, MoveNext, MovePrevious, MoveLast (among others). I will use some of these to fill up a list box with records of our RecordSet.
To get this example to work, make a database (with Access) called "MyDatabase.mdb" with the table "MyTable" in it and 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 "P hone" of type Text. Add some records to it and put a list box on a form and call it "lstRecords".
|
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
lstRecords.AddItem rsMyRS!Name
lstRecords.ItemData(lstRecords.NewIndex) = rsMyRS!ID
rsMyRS.MoveNext
Loop
End Sub
|
|
|
Searching the RecordSet
|
|
You might have wondered why I put the value of the field "ID" in the list box's ItemData property and 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". Then copy the following code to the project.
|
Private Sub lstRecords_Click()
rsMyRS.FindFirst "ID=" & Str(lstRecords.ItemData(lstRecords.ListIndex))
txtPhone.Text = rsMyRS!Phone
End Sub
|
|
|
Updating the Database
|
|
When doing database programming,you will probably want to be able to update some value of some field. This is done with Edit and Update and 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.Edit
rsMyRS!Phone = txtPhone.Text
rsMyRS.Update
End Sub
|
|
|
Deleting and Adding records
|
|
Deleting
|
|
Deleting records couldn't be simpler and to delete the current record you just invoke the Delete method of the RecordSet object.
We will put this feature in our little project and 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.Delete
lstRecords.RemoveItem lstRecords.ListIndex
End Sub
|
|
|
Adding
|
|
Adding records is much like updateing, except you use AddNew instead of Edit and let's add one more command button to our application. Let's call it...errh...let me see...yea! "cmdNew" =).
Here is the code which adds a new record.
|
Private Sub cmdNew_Click()
rsMyRS.AddNew
rsMyRS!Name = "A New Person"
lstRecords.AddItem rsMyRS!Name
lstRecords.ItemData(lstRecords.NewIndex) = rsMyRS!ID
rsMyRS!Phone = "Person's Phone Number"
rsMyRS.Update
End Sub
|
|
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
Creating database applications in VB,
visual basic applications,
visual basic database,
how to database,
vb source code,
visual basic vb,
vb ms access,
visual basic net applications,
microsoft access database,
ms access database,
asp net database,
sql server database,
connect to database,
vb asp net,
vb array,
database tutorial,
text creating,
xml database,
asp database,
php database,
c# database
|
|
| HTML Quizes |
|
|
| XML Quizes |
|
|
| Browser Scripting Quizes |
|
|
| Server Scripting Quizes |
|
|
| .NET (dotnet) Quizes |
|
|
| Multimedia Quizes |
|
|
| Web Building Quizes |
|
|
| Java Quizes |
|
|
| Programming Langauges Quizes |
|
|
| Soft Skills Quizes |
|
|
| Database Quizes |
|
|
| Operating System Quizes |
|
|
| Software Testing Quizes |
|
|
| SAP Module Quizes |
|
|
| Networking Programming Quizes |
|
|
| Microsoft Office Quizes |
|
|
| Accounting Quizes |
|
|
| Computer Basics Quizes |
|
|
|