referencing form fields in a loop?

  • Thread starter Thread starter Jerome
  • Start date Start date
J

Jerome

Hi,

I want to do something really easy but I don't know how to make the
field reference variable??

My form has x text controls: field1, field2, field3, etc ...
and a button to reset them all back to the default value of '$'.

Of course I could write:
Me.field1.value="$"
Me.field2.value="$"
Me.field3.value="$"

etc but I want to write that more elegantly, something like this:

Sub cmdReset
Dim count
For count = 1 To 9
Me.field[count].Value = "$"
Next count
End Sub

But apparently that's not the right way to reference each control ...!
How can I do this?

Any help is greatly appreciated.

Jerome
 
Hi Jerome,

Try this in your subroutine:

Dim ctl As Control

For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
ctl = "$"
End If
Next ctl

HTH -Linda
 
Well, field1, field2, etc aren't exactly elegant (or meaningful) field
names, but if that's what you want to use, you can do your loop like this:
for i=1 to 9
me.controls("field" & i) = "$"
next

HTH
 
Back
Top