Referencing a previous record

davesmith202

Employee of Access World
Local time
Today, 22:29
Joined
Jul 20, 2001
Messages
522
I have a loop where I cycle through a recordset:

Code:
rs.MoveFirst
Do While Not rs.EOF
rs.MoveNext
Loop

What I need is some code that says If the City field in the previous record is the same as this one, then msgbox "Same City". i.e. as it cycles through, it checks the current record-1.

Make sense? Any ideas?

Thanks,

Dave
 
What you need to do is to create an array and populate the array with your Do Whille.

Code:
Dim MyArray() as string

Dim nIndex As Integer

Dim Rs As DAO.Recordset
Set Rs = CurrentDb.Openrecordset("TableName")

If not Rs.EOF then
    Rs.MoveLast
    ReDim MyArray(Rs.Recordcount -1)
    Rs.MoveFirst
    Do Until Rs.EOF
        MyArray(nIndex) = Rs("FieldName")
        Rs.MoveNext
    Loop
End If

At this point we have populated an array with all the field contents

Next you can perform another loop based on the array

Code:
For x = 0 to Rs.RecordCount -1
     If MyArray(x) = MyArray(x+1) Then
        'The next record is different froom this one
     End If
        
Next

Rs.Close
Set Rs = nothing

The above is aircode and has not been tested

CodeMaster::cool:
 
CodeMaster, I can see how that could work. What about doing something like:

Code:
strPreviousCity=""
rs.MoveFirst
Do While Not rs.EOF
   If rs!CurrentCity=strPreviousCity then blah blah
   rs.MoveNext
   strPreviousCity=rs!CurrentCity
Loop

Would that be more efficient? Would it even work? lol
 
There is only one way to find out.... try it....
 

Users who are viewing this thread

Back
Top Bottom