Looping through controls

G

Guest

is there a way I can loop through all the controls in all the subforms in an
Access form? I want to call a function in each control, so I have something
like:

Dim frmLoop As Form
Dim ctrLoop As Control


For Each frmLoop In Forms
For Each ctrLoop In frmLoop
Me.frmLoop.Controls.ctrLoop.OnUndo = "myProcedure"

Next ctrLoop
Next frmLoop

Am I on the right track? I know my syntax is bad. That's why it's not
working? Thanks for any help
 
D

Douglas J. Steele

Try something like:

Dim ctlSubform As Control
Dim ctrLoop As Control

For Each ctlSubform In Me.Controls
If TypeOf ctlSubform Is Subform Then
For Each ctrLoop In ctlSubform.Form.Controls
' ctrLoop.OnUndo = "myProcedure"
Next ctrLoop
End If
Next frmLoop

However, I'm not aware of an Undo event (I only have Access 97 on this
machine, and it definitely doesn't have that event) If there is an Undo
event, the property would likely be called Undo, not OnUndo, and not every
control would have one (for example, since you can't type in a label or
line, there wouldn't be an Undo event for them). What you might want to do
is add a function like the following:

Function HasEvent(ControlObject As Control, EventName As String) As Boolean
On Error Resume Next

Dim strCurrProc As String

strCurrProc = ControlObject.Properties(EventName)
HasEvent = (Err.Number = 0)

End Function

You could then use

Dim ctlSubform As Control
Dim ctrLoop As Control

For Each ctlSubform In Me.Controls
If TypeOf ctlSubform Is Subform Then
For Each ctrLoop In ctlSubform.Form.Controls
If HasEvent(ctlLoop, "Undo") Then
ctrLoop.Undo = "myProcedure"
End If
Next ctrLoop
End If
Next frmLoop
 
G

Guest

Doug:

As an FYI, there is an Undo event in A2K3 (don't know about 2002 as I don't
have that one) and it isn't in 2000.
--
Bob Larson
Access World Forums Super Moderator
Utter Access VIP
Tutorials at http://www.btabdevelopment.com
__________________________________
If my post was helpful to you, please rate the post.
 
G

Guest

The informations is helpful. I haven't gotten it to work, but I've made
progress. Some things I found:

I can test for the type of control, so I don't have to test to see if it has
that particular event. Instead of using your HasEvent() procedure I just use

if typeOf ctlText is TextBox Then

and from what I've gathered, undo is the event, OnUndo is the property of
the control that allows me to assign a procedure to that event.

Thanks again for your help.
 
D

Douglas J. Steele

I forgot to mention that if you're trying to assign your code to run for the
occurrence of the event on all the text boxes, MyProcedure needs to be a
function (even if it doesn't return any value), and you'll need to prefix it
with an equal sign:

ctrLoop.OnUndo = "=MyProcedure()"
 

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