Example Database Project
Once all the names and captions have been changed we can now add the code to the final part of the Database.
First we should add code to the Search Button on fmDataBase form:
Private Sub cmSearch_Click ()
Load fmSearch
fmSearch.Show 1
End Sub
Also we must also set the lastrecord again so we can place this in FORM_ONLOAD() procedure in the fmSEARCH form.
LastRecord = FileLen (App.Path + "\" + "Records.rec") \
Len (RecordDB)
Next we will add code to the option explicit section in the General Declarations of the fmSEARCH form.
Option Explicit
Dim LastRecord As Long
Dim RecordDB As RECDAT
Dim Which As Integer
The last variable is so we know which search to do once we have selected a field to search.
The code for the option buttons is as follows:
Private Sub opARTIST_Click()
txtARTIST.Enabled = True
txtRECORD.Enabled = False
txtSTYLE.Enabled = False
Which = 1
End Sub
Private Sub opRECORD_Click()
txtARTIST.Enabled = False
txtRECORD.Enabled = True
txtSTYLE.Enabled = False
Which = 2
End Sub
Private Sub opSTYLE_Click()
txtARTIST.Enabled = False
txtRECORD.Enabled = False
txtSTYLE.Enabled = True
Which = 3
End Sub
All that the above code does is when an option button has been selected, it stops you being able to enter any text in to any other text box. But the which variable we will come to later when I show you the search code, all it does is define which field to search in.
Now here is the code for the search.
Private Sub cmSearch_Click()
Dim i As Integer
If Which < 1 Or Which > 3 Then
Exit Sub
End If
lstMATCHES.Clear
i = 1
Do
Get #1, i, RecordDB
If Which = 1 Then
If Trim(UCase(txtARTIST.text)) =
Trim(UCase(RecordDB.ARTIST)) Then
lstMATCHES.AddItem Trim(RecordDB.ARTIST) _
+ Space(2) + Trim(RecordDB.RECORD) _
+ Space(2) + Trim(RecordDB.STYLE)
End If
ElseIf Which = 2 Then
If Trim(UCase(txtRECORD.text)) =
Trim(UCase(RecordDB.RECORD)) Then
lstMATCHES.AddItem Trim(RecordDB.ARTIST) _
+ Space(2) + Trim(RecordDB.RECORD) _
+ Space(2) + Trim(RecordDB.STYLE)
End If
ElseIf Which = 3 Then
If Trim (UCase(txtSTYLE.text)) =
Trim (UCase(RecordDB.STYLE)) Then
lstMATCHES.AddItem Trim(RecordDB.ARTIST) _
+ Space(2) + Trim(RecordDB.RECORD) _
+ Space(2) + Trim(RecordDB.STYLE)
End If
End If
DoEvents
i = i + 1
Loop While i <= LastRecord
End Sub
What the above code does is it gets the data from the Database file and stores it in the RecordDB template. It then converts the data to uppercase and takes away any leading or trailing space, then it takes the text from the textbox which is selected and trims it down and converts it uppercase. Then the textbox data is compared to the data in the field from the currently selected record. If a match is found the code posts the results in the list box so you can see that it found what you where searching for.
Some final things to do are unload the search form when you click close.
Private Sub cmCLOSE_Click()
Unload fmSearch
End Sub
Also we have to close the file, so no errors occur when we reopen the file. So in the terminate procedure of the fmDataBase form enter the code.
Private Sub Form_Unload(Cancel As Integer)
Close #1
End Sub
Another thing that needs to be done is to stop you over entering text into the text boxes, as this will cause an error when you come to write or search the Database. So on the FORM_LOAD procedure of each form we need to add a maxlen to each of the textboxes.
txtARTIST.Maxlen = Len (RecordDB.ARTIST)
txtRECORD.Maxlen = Len (RecordDB.RECORD)
txtSTYLE.Maxlen = Len (RecordDB.STYLE)
This code above stops you from over entering text in to the text boxes.
The Database we just made is not very useful but it gives you a idea of what can be achieved with Database. As a exercise why don’t, you try and make a booking keeping database, where you can keep records of people who have taken out book’s etc, or expand this example further by adding extra features like a CD collection or Games collection, and prices, dates you paid and bought them.
This is the end of this tutorial on Simple Database’s, I hope you enjoyed it and found it useful, and the topics covered in this tutorial are just a small amount of what Databases are all about. If you have any problems or questions just email: paul@pcbware.co.uk or visit www.pcbware.co.uk and I will be glad to help you.
Paul Kinlan
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
Senin, 09 Februari 2009
Langganan:
Posting Komentar (Atom)
Tidak ada komentar:
Posting Komentar