-
Efficient looping
I would like to know everyone’s suggestion for looping through a recordset when you have multiple values you are watching. Here is my example.
Recordset of (LastName, FirstName, Address, State)
Smith Joe 111 Main Street GA
Smith Jim 222 South Street GA
Jones John 333 West Street FL
Jones Sue 333 West Street FL
Jones Bobby 333 West Street FL
Wall Harry 123 First Street FL
Wall Marry 123 First Street FL
Now here is what I would like to do. I want to write the whole recordset to a listbox, but, I want the list to Break on Last Name and I also want to Break if they live at different addresses. This means I need to know when the Last name field changes, as well as when (if) the address changes within the same Last Name. I have seen this done many ways, however, I am interested in the most efficient way of doing it.
Thanks
-
[pseudocode]
dim OldLastName as string = last name from first record
loop
if OldLastName <> CurrentLastName then
"break on last name"
maybe insert a blank line or something like that
else
add record to list
end if
end loop
[/pseudocode]
-
Hi,
Try
[pseudocode]
Dim OldName, NewName, OldHouse, NewHouse, OldStreet, NewStreet as String
For iCount = 1 to RecordsetCount
Loop through recordset placing relative field values into the declared strings.
CheckForChange(OldNAme, NewName, OldHouse, NewHouse, OldStreet, NewStreet)
Next iCount
private sub CheckForChange(OldName as string, NewName as String, OldHouse as string, NewHouse as String, OldStreet as string, NewStreet as String)
CompactStrings(oldname, NewName)
if CompactStrings=False then
InsertBreak
Exit sub
end if
CompactStrings(oldhouse, NewHouse)
if CompactStrings=False then
InsertBreak
Exit sub
end if
CompactStrings(oldStreet, NewStreet)
if CompactStrings=False then InsertBreak
end sub
private function CompactStrings(strOld as String, strNew as String
) As Boolean
strOld=trim(strOld)
strNew=trim(strNew)
if strOld<>strNew OR strOld.Length<>strNew.Length then Return False
end function
Private Sub InsertBreak
Code to insert break
End Sub