VB%: Finding matching records in a Record set... 
Author Message
 VB%: Finding matching records in a Record set...

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
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)
            If Data1.Recordset.LastName = FindLastName And
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-------------------------------------------

ALL suggestions will be greatly appreciated!
Cliff



Thu, 18 May 2000 03:00:00 GMT  
 VB%: Finding matching records in a Record set...

WIthout going through your code piece by piece, I would offer a quicker
solution. Try using a very simple SQL statement for the recordset. Assuming
you do not want to mess up your existing recordset, you may want to use DAO
objects instead of the data controls for this, but if you can use the data
controls, I think what you would want is...

Data1.Recordsource = "SELECT * from Table where Lastname = '" & FindLastName
& "' and FirstName = '" & FindFirstName & "'"
Data1.Refresh

Now you should simply be able to loop through the recordset. Each item will
have a matching Lastname and Firstname.

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
>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)
>            If Data1.Recordset.LastName = FindLastName And
>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-----------------------------------------
--

>ALL suggestions will be greatly appreciated!
>Cliff



Thu, 18 May 2000 03:00:00 GMT  
 VB%: Finding matching records in a Record set...

You are doing many things wrong. I think you need to do some more reading on databases.
You need to create a recordset and do your searches off of that. If the only thing you want to do is to list all entries with
matching names and don't need the recordset anywhere else, then create the recordset with that query to begin with. Then you can
loop through the entire recordset adding whatever field(s) you want from each record to your listbox.

Private Sub cmdFind_Click()
Dim I as Integer
Dim SearchString as String
Dim MyRecordSet as RecordSet

SearchString = "Select * from MyTable Where [lastname] = '" & txtFields(0) & "' & _
        AND [firstname] = '" & txtFields(1) & "'"
Set MyRecordSet = Data1.OpenRecordSet(SearchString, dbOpenDynaset)
if MyRecordSet.RecordCount > 0 then
        MyRecordSet.MoveLast
        MyRecordSet.MoveFirst
        For I = 0 to MyRecordSet.RecordCount - 1
                frmCEUPrint.lstPrint.AddItem MyRecordSet.fields("fieldname")
                MyRecordSet.MoveNext
        Next I
End If
End Sub

If your recordset is already created from elsewhere in your program, do this.

Private Sub cmdFind_Click()
Dim SearchString as string

SearchString = "[lastname] = '" & txtFields(0) & "' AND " & [firstname] = '" & txtFields(1) & "'"
MyRecordSet.FindFirst searchstring
Do While MyRecordSet.NoMatch = False
        frmCEUPrint.lstPrint.AddItem MyRecordSet.fields("fieldname")
        MyRecordSet.FindNext SearchString
Loop
End Sub

To get values from a recordset you access the recordset's Fields property
(text1.text = MyRecordSet.Fields("nameofthefieldhere")

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
>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)
>            If Data1.Recordset.LastName = FindLastName And
>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-------------------------------------------

>ALL suggestions will be greatly appreciated!
>Cliff



Fri, 19 May 2000 03:00:00 GMT  
 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"



Sat, 20 May 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Checking for matching records in two tables with different record structures

2. Finding records that don't match

3. how do I - find out how many records match abc and how many macth xyz

4. Record Set Guru's - Can't Create Record Set error 429

5. Record Set Guru's - Can't Create Record Set error 429

6. Counting records in a record set

7. Record Count in a Record Set

8. ADO, Record Set Positioning - SQL - Browse Records

9. Recordsets Finding Records and Updating Records

10. Finding Records and Updating Records in More than one Recordsets

11. Finding a set of records.

12. Finding/Seeking/setting the current record when using a data control

 

 
Powered by phpBB® Forum Software