The reason you can't do a "fld.SetFocus" is that the SetFocus method takes an explicit field name, and unless you have a field (control) named "FLD", it barfs.
But references to an object's collection of stuff in parenthetical surroundlings, e.g. 'myForm(fld).SetFocus' is not so anal-retentively picky - if Access can't resolve fld to an actual field/control name, it checks to see if there's a variable named fld, and if so what it contains, and if the CONTENTS of the variable fld resolve to a field or control name, everything is hunky-dory.
Steve's technique is very useful when using symbolic substitution for control and object names.
JMH
Me? Ambivalent? Well, yes and no....
Another free Access forum:
As a further qualification to Wildhare's excellent explanation, here are a couple of other related points, which are a logical extention to the thread:
(a) The bracket based referencing described above, applies equally well to a variety of Access "collections"; for example: controls on Forms or Reports, or fields in Tables or Queries.
(b) On a form for example, assunimg we have a control called txtControl, the following three alternatives do the same thing:
(c) If txtControl is the 4th control on the form, then it can also be referenced using its ordinal position in the form's control collection; ie.:
me(3) = "Hello World"
Note the value three above, not four. This is because the first element of the collection has a zero based subscript; you need to get used to this.
(d) The above ordinal position referencing provides a very powerful capability: the ability to move through all of the items in a collection, from start to end, as illustrated below:
for i = 1 to me.count
msgbox me(i-1).name
next i
This will show the names of all controls on a form. Note the (i-1) referencing to allow for the zero based subscripting. An alternative representation could be:
for i = 0 to me.count - 1
msgbox me(i).name
next i
Take your pick.
There are lots of uses for this collection iteration on forms (and also other ways to do it; check out For Each loops for more info). One of my favourite uses is to combine it with usage of the Tag property; eg.
[ff]
Dim F as Form: Set F = me
For i = 0 to F.count - 1
If F(i).tag = "ShowMe" then
F(i).Visible = True
Else
F(i).Visible = False
Endif
Next I
[/ff]
(e) Another example of using the bracket syntax to iterate through a collection of elements of a recordset might be to assign field values to a 'zero based' array elements; for example, assuming a recordset called RS:
For i = 0 to RS.Fields.Count - 1
A(i) = RS(i)
next i
This is obviously a preferable solution to:
A(0) = RS!YourField0
A(1) = RS!YourField1
A(2) = RS!YourField2
... and so on
and becomes the only viable option, if the number of fields in the recordset is large (though I cannot tell you of the number of applications I've had to surf though with hundreds of lines similar to the above, where a simple iteration through the collection would have made life easier for all involved (some developers do have this misguided notion that the longer the program, the cleverer the solution!!).
I'll stop there; hope this helps someone,
Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.