
VB%: Finding matching records in a Record set...
Quote:
>I am trying to loop thru a record set to find ALL the matching
>files
>and I keep coming up empty handed! here is what I am doing
>(wrong):
>-----------------------------snip------------------------------------------
>Private Sub cmdFind_Click()
>Dim FindLastName As String
>Dim FindFirstName As String
>Dim UserResp As String
>Dim L As Integer
>FindLastName = txtFields(0)
>FindFirstName = txtFields(1)
>L = Data1.Recordset.RecordCount
This will only give you the right number of records if the Recordset Type is
Table type
Quote:
>frmCEUInput.Hide
>frmCEUPrint.Show
>Do
>With Data1.Recordset
> ' Populate recordset.
> .MoveLast
> ' Find first record satisfying search string. Exit
> ' loop if no such record exists.
> .FindFirst (FindLastName)
This should be:
.FindFirst "LastNameField = '" & FindLastName & "'"
'You have to use the name of the field where you want to search
'Notice the use of ' to enclose the string you want to search by
Quote:
> If Data1.Recordset.LastName = FindLastName And
If you want to search by two fields use (instead of two lines):
.FindFirst "LastNameField = '" & FindLastName & "' AND
FirstNameField = '" & FindFirstName & "'"
Quote:
>Data1.Recordset.FirstName = FindFirstName Then
> frmCEUPrint.lstPrint.AddItem
>(Data1.Recordset.RecordIndex(L))
> L = L + 1
> Else
> L = L + 1
> UserResp = MsgBox("No records found with " &
>FindLastName & ".")
> Exit Do
> End If
>End With
>Loop
>End Sub
>------------------------------snip-----------------------------------------
--
--
If I were you, I would use a Table Type Recordset and use indexes.
Suppose your table has two fields called FIRSTNAME and LASTNAME.
Name the index NAMEIDX and include those two fields on the index - firts the
LastName and second the FirstName.
Now use this statement to sort the recordset:
Data1.Recordset.Index = "NAMEIDX"
And use this to find the first matching record
Data1.recordset.Seek "=", "'" & FindLastName & "'", "'" &
FindFirstName & "'"
If Data1.Recordset.Nomatch Then Exit Sub
Do
frmCEUPrint.lstPrint.AddItem (Data1.Recordset.RecordIndex(L))
L = L + 1
Data1.Recordset.MoveNext
If (Data1.Recordset![LASTNAME] <> FindLastName) Or
(Data1.Recordset![FIRSTNAME] <> FindFirstName) Thne
UserResp = MsgBox("No records found with " & FindLastName &
".")
Exit Do
End If
Loop Until Data1.Recordset.EOF
_____________________
JOEL PAULA
"why he did not crane out to see
what lay beneath was perhaps because
the window was not made to open
or because he could or would not open it."
- Samuel Beckett - "stirrings still"