m:
On Jan 27, 2:26 pm, "David W. Fenton"
Is it a programing requirement of VBA that the objects in a
collection (mcol in this instance) are variant? Is that why you
cast them into Control?
Yes, so far as I'm aware. I can't recall at this point exactly where
I got that information, but I certainly wouldn't be using Set inside
the loop if it were unnecessary.
Well, crap. I just tested it, and this seems to work:
For Each ctl In mcol
If ctl.Visible = True Then ctl.Locked = Not bolSwitch
Next ctl
This may be a change between earlier versions of Access and later,
as this is code I developed originally in A97.
Well, just tested in the A97 version of the app I'd just tested, and
it works just fine.
I'm pretty sure that I got this from the ADH97, but in five minutes
spent perusing it, can't find anything.
This I'm not clear - from my understanding, this seems counter-
intuitive, which I'm sure you would not code it it the way I
understand it.
If you send "True" to the function, I understand that it sets the
property to "NOT Locked"?
The code is to lock or unlock controls for editing. The function is
called "EnableEdit", which means that when I pass EnableEdit True,
it's going to UNLOCK the controls, i.e., making them editable.
The test for .Visible is because the form this comes from has
different record types, and different controls are displayed in
different contexts.
Is this merely to destroy the ctl object when you're done with the
function?
Yes. That line is one of excessive caution. By design, VBA should
release the reference when the ctl variable goes out of scope (i.e.,
when the subroutine finishes running). But because VBA uses
reference counting to do this, it sometimes screws up and doesn't
release the reference. Since the loop refers to each control in
turn, at the end of the loop, there's an outstanding implicit
reference to the last control in the loop. Setting the variable to
Nothing insures that there's no possibility that this reference will
remain unreleased when the code finishes.
And that line of code is there not because I'm smart enough to
figure that out, but because I'm smart enough to listen to brilliant
Access programmers -- in this case, it was Michael Kaplan who
recommended this for all loops through object collections.
An aside: You may not be very experienced with VBA, but you ask
*very* good questions!