Binary Search Data1.Recordset ... 1 Line Of Code Needed

2Pac
04-16-2004, 11:13 AM
HELP!! Ive tried to do a binary search on the following Access Database through Data1.recordset.
I think that only one line of code needs to be changed: how to specify a Long Variable type to equal the first record in the database.

Its highlighted in red below, I think this is all that needs to be changed, but am not sure.....

Option Explicit

Dim db As Database
Dim rs As Recordset

Private Sub Command1_Click()
Dim hit As Boolean, fTop As Long
Dim fBott As Long, fTmp As Long

If txtSearch.Text = "" Then
MsgBox "Nothing to do."
txtSearch.SetFocus
Exit Sub
End If
hit = False
fTop = Data1.Recordset.RecordCount - 1
fBott = 0
fTmp = (fTop + fBott) / 2

Do While (Not hit) And (fTop >= fBott)
Data1.Recordset. = fTmp
'WHAT DO I PUT IN HERE TO MAKE fTmp = FIRST RECORD ??

If Data1.Recordset("Staff Number") = txtSearch.Text Then
hit = True
ElseIf Text1.Text < Data1.Recordset("Staff Number") Then
fTop = fTmp - 1
Else
fBott = fTmp + 1
End If
fTmp = (fTop + fBott) / 2
If Not hit Then
MsgBox "No match"
Else
txtSurname.Text = Data1.Recordset("Surname")

End If
Loop
txtSearch.SetFocus
End Sub

Shurik12
04-16-2004, 02:02 PM
Hi,

I have a slight difficulty to understand this part of your code:


...
fTop = Data1.Recordset.RecordCount - 1
fBott = 0
fTmp = (fTop + fBott) / 2

Do While (Not hit) And (fTop >= fBott)
Data1.Recordset. = fTmp
...


Just a note. If you drop Data control and do 'all in code' (and better yet switch to ADO) a recordset opened with the following sql will return the needed result:

"Select * From Table1 Where [Staff Number] = '" & txtSearch.Text &"'"

Shurik.

rprewitt
04-16-2004, 02:55 PM
If that line of code would work then you could also put it in the recordsource value of the data control ;)

MKoslof
04-16-2004, 06:07 PM
I don't understand what you are trying to do with the recordset. You aren't looping within the recordset, so I assume you are always on the first value. And, then you never close the recordset or update it. And what is Data1? I assume this is some data control, but you also DIM a recordset and database at the top of your code piece (db and rs) but you never use these variables anywhere.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum