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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An easy one . . maybe 8

Status
Not open for further replies.

leckie

Programmer
Apr 19, 2001
65
0
0
GB
my code

Dim wk As String
wk = Format(Date + 7, "ww")
fld = "[" + wk + "]"

this bit works ok
DoCmd.GoToControl fld

this won't
fld.SetFocus

any help much appreciated . .

regards
Gezro
 
Try:

me(fld).setfocus

if the code is internal to the form, otherwise

Forms!frmYourFormName(fld).setfocus.

Hope this helsp,



Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
What a star . . worked straight away

 
ahh.. but do we understand why?

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:
More Access stuff at
 
Wow. Thanks to Steve101 for the info and WildHare for the explanation. I didn't even realize this feature was available.
 
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:

me!txtControl = "Hello World"
me("txtControl") = "Hello World"

someVariable = "txtControl"
me(SomeVariable) = "Hello World"

(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)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top