Private Function BinarySearch(strSearchItem As String) As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim lngLastPass As Long
Dim strItem As String
Dim strValue As String
Dim blnDone As Boolean
Open "TDStudList.txt" For Random As 1 Len = Len(StudRec)
Rec = LOF(1) / Len(StudRec)
lngFirst = 1
lngLast = Rec
strItem = UCase$(Trim$(strSearchItem))
'If there is only one record check if it is the desired one.
If lngLast = 1 Then
Get 1, 1, StudRec
If strItem = UCase$(StudRec.RegNO) Then
BinarySearch = 1
Else
BinarySearch = 0
End If
Close 1
Exit Function
End If
'Set the pointer to the middle record.
lngMiddle = ((lngLast - lngFirst) + 1) \ 2
Do Until blnDone
Get 1, lngMiddle, StudRec
strValue = UCase$(StudRec.RegNO)
If strItem = strValue Then
'
'Item Found
'
BinarySearch = lngMiddle
blnDone = True
Exit Do
ElseIf strItem < strValue Then
'
'Direction = Down
'Remove the second half of the records
lngLast = lngMiddle
lngMiddle = lngMiddle - ((lngLast - lngFirst) + 1) \ 2
ElseIf strItem > strValue Then
'
'Direction = UP
'Remove the first half of the records
'
lngFirst = lngMiddle
lngMiddle = lngMiddle - ((lngLast - lngFirst) + 1) \ 2
End If
'
'Check if the records are still divisible
'
If (lngMiddle = lngFirst) Or (lngMiddle = lngLast) Then
lngLastPass = lngLastPass + 1
If lngLastPass = 2 Then
lngLastPass = 0
BinarySearch = 0
blnDone = True
End If
End If
Loop
Close 1
End Function
Last edited by jerry4prince; Sep 2nd, 2010 at 04:28 PM.
1. Use FreeFile to address a file number
2. Where did u get StudRec from?
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
Option Explicit
'
' Student record user defined type.
'
Private Type StudentData
RegNO As String * 9
ContactAddress As String * 40
StudentName As String * 30
Phone As String * 20
State As String * 15
DOB As String * 14
Sex As String * 10
End Type
'
' Array of students
'
Private StudArray() As StudentData
Private StudRec As StudentData
Private Rec As Long
Private Sub pShowFileRecord(lngRecord As Long)
'
' Display a record from the file in the textboxes.
'
txtRegNO = StudRec.RegNO
txtContactAddress = StudRec.ContactAddress
txtStudentName = StudRec.StudentName
txtPhone = StudRec.Phone
txtState = StudRec.State
txtDOB = StudRec.DOB
txtSex = StudRec.Sex
End Sub
Private Sub ClearRecord()
'
' Clear the text boxes
'
MsgBox "Please Enter a valid Registration number", vbCritical, "Invalid Entry or No Record"
txtRegNO.Text = ""
txtContactAddress.Text = ""
txtStudentName.Text = ""
txtPhone.Text = ""
txtState.Text = ""
txtDOB.Text = ""
txtSex.Text = ""
txtNO.SetFocus 'Set the cursor at the input
End Sub
Private Sub cmdAbout_Click()
'
'Show about form
'
frmAbout.Show
End Sub
Private Sub cmdSearch_Click()
Dim lngMatch As Long
'
' Search the file for a record.
'
lngMatch = BinarySearch(txtNO)
'
' If found, display the record.
'
If lngMatch Then
Call pShowFileRecord(lngMatch)
Else
Call ClearRecord
End If
End Sub
Private Sub cmdTD_Click()
frmTD.Show
frmBS.Hide
End Sub
Private Sub Form_Load()
Dim GetStud As StudentData
Dim l As Long
'
'Load an array with data from the file and
'load the listbox with the Reg NO from each record.
'
Open "StudList.dat" For Random As 1 Len = Len(GetStud)
Rec = LOF(1) / Len(GetStud)
ReDim StudArray(1 To Rec)
For l = 1 To Rec
Get 1, l, StudArray(l)
lstRegNO.AddItem StudArray(l).RegNO
Next
Close 1
End Sub
Private Sub lstRegNO_Click()
'
'show listbox items in txtNO
'
txtNO = lstRegNO.Text
End Sub
Private Sub cmdClear_Click()
'
'Clear the whole textboxes
'
txtRegNO.Text = ""
txtContactAddress.Text = ""
txtStudentName.Text = ""
txtPhone.Text = ""
txtState.Text = ""
txtDOB.Text = ""
txtSex.Text = ""
txtNO.Text = ""
End Sub
Private Sub cmdQuit_Click()
'
'Terminate the program
'
End
End Sub
Private Function BinarySearch(strSearchItem As String) As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim lngLastPass As Long
Dim strItem As String
Dim strValue As String
Dim blnDone As Boolean
Open "StudList.dat" For Random As 1 Len = Len(StudRec)
Rec = LOF(1) / Len(StudRec)
'
' Search a file for an item using a binary search.
' The search is not case sensitive.
' Returned is the index of the matching file element.
'
'
' Initialize the pointers to the first
' and last records.
'
lngFirst = 1
lngLast = Rec
strItem = UCase$(Trim$(strSearchItem))
'
' If only one record, see if it is the desired one.
'
If lngLast = 1 Then
Get 1, 1, StudRec
If strItem = UCase$(StudRec.RegNO) Then
BinarySearch = 1
Else
BinarySearch = 0
End If
Close 1
Exit Function
End If
'
' Set the pointer to the middle record.
'
lngMiddle = ((lngLast - lngFirst) + 1) \ 2
'
' Apply the binary search criteria until the
' item is found or the file is exhausted.
'
Do Until blnDone
'
' Read a record from the file.
'
Get 1, lngMiddle, StudRec
strValue = UCase$(StudRec.RegNO)
If strItem = strValue Then
'
' Found it.
'
BinarySearch = lngMiddle
blnDone = True
Exit Do
ElseIf strItem < strValue Then
'
' Direction = down
' Remove the second half of the file.
'
lngLast = lngMiddle
lngMiddle = lngMiddle - ((lngLast - lngFirst) + 1) \ 2
ElseIf strItem > strValue Then
'
' Direction = Up
' Remove the first half of the file.
'
lngFirst = lngMiddle
lngMiddle = lngMiddle + ((lngLast - lngFirst) + 1) \ 2
End If
'
' See if record is still divisible.
'
If (lngMiddle = lngFirst) Or (lngMiddle = lngLast) Then
lngLastPass = lngLastPass + 1
If lngLastPass = 2 Then
lngLastPass = 0
BinarySearch = 0
blnDone = True
End If
End If
Loop
Close 1
End Function
Private Sub txtNO_KeyPress(KeyAscii As Integer)
'
' Convert to upper case.
'
KeyAscii = Asc(UCase$(Chr(KeyAscii)))
End Sub
Private Sub Form_Load()
Dim GetStud As StudentData
Dim l As Long
'
'Load an array with data from the file and
'load the listbox with the Reg NO from each record.
'
If Len(Dir(App.Path & "\Studlist.dat")) = 0 Then
MsgBox "File: 'Studlist.dat' does not exist.", vbCritical, "File Missing"
Unload Me
Exit Sub
End If
Dim fFile As Integer
fFile = FreeFile
Open App.Path & "\StudList.dat" For Random As #fFile Len = Len(GetStud)
Rec = LOF(fFile) / Len(GetStud)
ReDim StudArray(1 To Rec)
For l = 1 To Rec
Get 1, l, StudArray(l)
lstRegNO.AddItem StudArray(l).RegNO
Next
Close #fFile
End Sub
Code:
Private Function BinarySearch(strSearchItem As String) As Long
Dim lngFirst As Long
Dim lngLast As Long
Dim lngMiddle As Long
Dim lngLastPass As Long
Dim strItem As String
Dim strValue As String
Dim blnDone As Boolean
Dim fFile As Integer
fFile = FreeFile
Open "StudList.dat" For Random As #fFile Len = Len(StudRec)
If LOF(fFile) = 0 Then Close #fFile: Exit Function
Rec = LOF(fFile) / Len(StudRec)
'
' Search a file for an item using a binary search.
' The search is not case sensitive.
' Returned is the index of the matching file element.
'
'
' Initialize the pointers to the first
' and last records.
'
lngFirst = 1
lngLast = Rec
strItem = UCase$(Trim$(strSearchItem))
'
' If only one record, see if it is the desired one.
'
If lngLast = 1 Then
Get 1, 1, StudRec
If strItem = UCase$(StudRec.RegNO) Then
BinarySearch = 1
Else
BinarySearch = 0
End If
Close 1
Exit Function
End If
'
' Set the pointer to the middle record.
'
lngMiddle = ((lngLast - lngFirst) + 1) \ 2
'
' Apply the binary search criteria until the
' item is found or the file is exhausted.
'
Do Until blnDone
'
' Read a record from the file.
'
Get fFile, lngMiddle, StudRec
strValue = UCase$(StudRec.RegNO)
If strItem = strValue Then
'
' Found it.
'
BinarySearch = lngMiddle
blnDone = True
Exit Do
ElseIf strItem < strValue Then
'
' Direction = down
' Remove the second half of the file.
'
lngLast = lngMiddle
lngMiddle = lngMiddle - ((lngLast - lngFirst) + 1) \ 2
ElseIf strItem > strValue Then
'
' Direction = Up
' Remove the first half of the file.
'
lngFirst = lngMiddle
lngMiddle = lngMiddle + ((lngLast - lngFirst) + 1) \ 2
End If
'
' See if record is still divisible.
'
If (lngMiddle = lngFirst) Or (lngMiddle = lngLast) Then
lngLastPass = lngLastPass + 1
If lngLastPass = 2 Then
lngLastPass = 0
BinarySearch = 0
blnDone = True
End If
End If
Loop
Close #fFile
End Function
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
Theres something wrong with the PROJECT you uploaded. I can't extract it. Keeps on giving errors.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.