Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through records 1

Status
Not open for further replies.

Moss100

Technical User
Aug 10, 2004
586
GB
Hello I am just trying to learn how to loop through records.

I am trying to do a very basic task to start with.

I have a form and would like to loop through the records and show a message box with the ID for each record.

My code seems to loop through the records, but the ID stays the same throughout - I'm not sure why.

In addition should I be trying to loop through records on the form or by using a recordset? Many thanks for any pointers, Mark

Code:
Set rst = Me.RecordsetClone ' I'm guessing this copies the recordset

    rst.MoveFirst  ' I'm guessing this moves to the first record of the recordset
    
    Do Until rst.EOF   ' I'm guessing says run the code until there are no more records
     
     MsgBox "Here is the property number:" & [Prop_ID]
     
    rst.MoveNext   ' I'm guessing says go to the next record

 Loop
 
MsgBox "Here is the property number:" & rst![Prop_ID]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Your guesses are correct. You may look at your record set as an array of Fields and Records (similar to columns and rows in Excel. You also need to know that your record set looks like:[tt]

rst.BOF
rst records
...
rst.EOF[/tt]

With your logic as it is you may error on [tt]rst.MoveFirst[/tt] when you have an empty recordset: [tt]rst.BOF = rst.EOF[/tt] so just to be on the safe side:

Code:
Set rst = Me.RecordsetClone 
If rst.BOF <> rst.EOF Then[green]
'or you may say:
'If NOT rst.EOF Then[/green]
    rst.MoveFirst      
    Do Until rst.EOF 
       MsgBox "Here is the property number:" & rst![Prop_ID]
    rst.MoveNext
 Loop  
End If

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top