Use a different control on each pass through a loop

B

Brian Scheele

Suppose I have 5 fields on a form, f1, f2, f3, f4, and f5, and I want to pull
values in the field, x, in the recordset, rst.

My code is like this (pseudocode):


rst.movefirst
f1 = x
rst.movenext
f2 = x
rst.movenext
f3 = x
rst.movenext
f4 = x
rst.movenext
f5 = x


I would rather code it like this:

i=1
dim F as field
rst.movefirst
while not rst.EOF
field = "f" & i
field = x
i = i + 1
rst.movenext
wend

The field = x would make f1 = x on the first pass, then f2 = x on the
second, etc.








--


Brian Scheele
IT Manager
Clark Filter
3649 Hempland Road
Lancaster, PA 17601-1323
 
G

George Nicholson

dim i as integer
rst.movefirst
while not rst.EOF
For i = 1 to 5
rst.Fields("f" & i) = x
Next i
rst.movenext
wend
 
A

Allen Browne

This example shows how to loop through each field of each record in a form:

Dim rs As DAO.Recordset
Dim fld As DAO.Field

Set rs = Me.RecordsetClone
If rs.RecordCount > 0 Then
rs.MoveFirst
Do While Not rs.EOF
For Each fld in rs.Fields
Debug.Print fld.Name, fld.Value
Next
rs.MoveNext
Loop
End If

Set fld = Nothing
Set rs = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top