Senin, 09 Februari 2009

Example Database Project

Now that we have the basic layout and design we can start to code some of the features in to the program:

First we will add in our type declaration so that we have the template for our Database.

Instructions:

Click "Project" from the VB menu,
Click "Add Module",
Now in the new module type.
Type RECDAT

ARTIST As String * 40

RECORD As String * 40

STYLE As String * 20

End Type

This set’s up our template for the Database, you will notice after the word "string" there is a "* 40" or "* 20" all this means is that the maximum characters allowed in the variable is either 40 or 20 respectively.

Now we can add code to our OnLoad procedure on the form, but first we must add some variables under the option explicit declaration so that they are available to all procedures and functions.

Option explicit

Dim Lastrecord As Long

Dim Currentrecord As Long

Dim RecordDB As RECDAT

The fourth line is the most important here as it declares a variable RecordDB as a type of RECDAT this means to access a variable inside the RECDAT type we can just do RecordDB.varaible.

Now we can add code in to the Form_Load procedure:

Private Sub Form_Load ()

Dim RecLen As Long

Dim NumRec As Long

RecLen = Len (RecordDB)

Open ("Records.rec") For Random As #1 Len = RecLen

NumRec = Filelen ("records.rec") \ RecLen

If NumRec = 0 Then

Lastrecord = 1

Else

Lastrecord = NumRec

End If

End Sub


Now that you have entered the code above, I will tell you what it does. After declaring the variables we are to use in this procedure, you will see that we find the size of the type this is done so that we can find how many records there currently are in the file so that we know what the lastrecord is. Also Visual Basic uses this length when opening a file as RANDOM ACCESS, this is so Visual Basic knows where about to jump to in the file when you ask for a record position.

i.e. Get #1, x, RecordDB (where x is the new position record number)

The next part of the coding which we next need to do is to add functionality to the command buttons.

First we will add code to the txtADD command button.

Private Sub cmADD_Click ()

RecordDB.ARTIST = Trim (txtARTIST.text)

RecordDB.RECORD = Trim (txtRECORD.text)

RecordDB.STYLE = Trim (txtSTYLE.text)

Put #1, Lastrecord, RecordDB

Lastrecord = Lastrecord + 1

fmDataBase.Caption = "Record Collection" + Space(1) & Currentrecord

& "/" & Lastrecord - 1

End Sub

Ok now that you have seen the code for the ADD button, I will explain what the code actually does:

First we obtain the data from the text boxes and place them in the template "RecordDB". Once this is done we are ready to write to the file, this is done by using the "PUT" statement, what the put statement does is it goes to the position in the file you selected and then writes the contents of the variable to the disk. Finally we increment the lastrecord by one and change the "forms" caption to show what record we are on.

Now that we are able to add new items to the Database, you would at least think we should be at least able to navigate through them, so now I will present you with the code that allows you to move forward and backwards through each record in the file.

Private Sub cmBACK_Click()


Currentrecord = Currentrecord - 1


If Currentrecord = 0 Then

Currentrecord = Lastrecord - 1

End If


Get #1, Currentrecord, RecordDB


txtARTIST.text = RecordDB.ARTIST

txtRECORD.text = RecordDB.RECORD

txtSTYLE.text = RecordDB.STYLE


fmDataBase.Caption = "Record Collection" + Space(1) & Currentrecord & "/" & Lastrecord - 1

End Sub

Private Sub cmFOR_Click()

Currentrecord = Currentrecord + 1


If Currentrecord > filelen (App.Path + "\" + "Records.rec") \ Len(RecordDB) Then

Currentrecord = 1

End If


Get #1, Currentrecord, RecordDB


txtARTIST.text = RecordDB.ARTIST

txtRECORD.text = RecordDB.RECORD

txtSTYLE.text = RecordDB.STYLE


fmDataBase.Caption = "Record Collection" + Space(1) & Currentrecord & "/" & Lastrecord - 1

End Sub

What the above code does is increment/decrement the "Currentrecord" by +/- 1 so that you get the next record in the file. Also before it reads the data it checks to see if the Currentrecord is not zero because if this was the case the a error would be reported because you cant have a zero record. Also if you go past the last record you won’t get an error but you won’t be able to read any data because there will be none there so you loop back to the begging of the file.

Now for the last part of the project, the "Search" method. In this section I will discuss a simple way of searching Databases, there are more complex search methods, but that goes beyond the scope of this tutorial.

What we can do is to scan through each of the records and search the specified field for the string we want. Here is how we can accomplish this objective:

NEXT..


source: http://www.vbexplorer.com/VBExplorer/VBExplorer.asp


--------------------------------------------------

Related :

Trik vb.net2008 code
basic io explorer vb2005
checking if files exist vb2005
Connecting passworded server another domain
Connecting reading from excel VBnet
copy files created between date
Copy Picture from Database
list disk drive vb2005
list file in vb2005
list sub forlder vb2005
Load exe file run from memory
move diferrent location vb2005
network connections VisualBasic2005
open multiple image no OpenFileDialog
play audio files VisualBasic2005

Tidak ada komentar:

Posting Komentar