referencing form fields in a loop?

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
 
S

Squirrel

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
 
M

MacDermott

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
 

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