PDA

View Full Version : Solved: iterate through Control names



Imdabaum
04-26-2010, 02:16 PM
I've got a loop that will go through contorls and based on what type (textbox, label, dropdown) I can cause an event to happen.

Is it possible to check whether the control's name meets a certain criteria?

The autocomplete doesn't give me any options to actually see the name of the control.

Something like


Dim ctl as Control

foreach ctl in Me.Controls
' Debug.print show control name?
If ctl.Name = strCriteria Then
ctl.enabled = false
End If
next ctl

c_smithwick
04-26-2010, 03:01 PM
What you have is exactly correct. Autosense isn't working because you are using a For....Each Loop. "ctl" isn't set to anything until the loop begins, so while you are still writing the code, the IDE can't tell what is referenced, unlike the following:



Set x = Currentdb.OpenRecordset("tblSomeTable")

x.AddNew
....

In the case of the above, the IDE "knows" that x is a recordset because you explicitly set it in the code line "Set x = ".

Autosense also "fails" to work in the case of Late Binding. Suppose the following:


Dim objOutlook as Object

Set objOutlook = GetObject(, "Outlook.Application")



Autosense will not work with objOutlook either, because the IDE sees it defined as a generic Object and doesn't know what methods or actions to display

Imdabaum
04-26-2010, 03:02 PM
Oh... that's fancy. Makes complete sense. Thanks.
I'll try it out. I have about 64 fields that the manager wants disabled if the option box is value 1. So looping seems to be the ideal way to handle it.

Imdabaum
04-26-2010, 03:27 PM
Thanks a million for the quick response. That made quick work of this problem.

Imdabaum
05-11-2010, 09:03 AM
Just learned to not ignore the F1 button. The Help files actually do have some helpful information on some topics.


Dim ctl As Control

'If you have button1, button2, button3, button4... etc.
'that you want to hide you can also do this.
For i=1 To n 'where n = the last integer of your named control.
stControlName = "button" and i
Set ctl = Me.Controls(stControlName)
ctl.enabled = False
Next i


A little more precise way of identifying controls that have names that fit a specific pattern.