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!

modifying properties of controls 'wholesale' 5

Status
Not open for further replies.

uscitizen

Technical User
Jan 17, 2003
672
0
0
US
let's say there are a host of command buttons on a form and you'd like to disable them easily (w/o enumerating every last one of their names in statements like button1.enabled = false, .......,button1000.enabled = false), not that there really are 1000s of them but more than you'd want to be bothered coding explicitly. can vba handle that? what if there are a few buttons in the footer (as opposed to the details section where the many live) which you would want to leave alone?

“The philosophy of the school room in one generation will be the philosophy of government in the next." --- Abraham Lincoln
 
you can name to controls based on a some sort of scheme...

ctrl1
ctrl2
ctrl3
etc

then have some code run that starts at 1 and goes to whatever...it checks each field and does whatever you need done....

For i = 1 to 100
If Me("ctrl" & i) = "something" then
do something
End If
Next i

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
MCSA, CNA, Net+, A+
w: rljohnso@stewart.com
h: wildmage@tampabay.rr.com
 
hi, nice thought for future database design, but this one was not put together with this requirement in mind, so the looping would work w/o a re-write of the naming convention.

“The philosophy of the school room in one generation will be the philosophy of government in the next." --- Abraham Lincoln
 
i meant to say the looping would NOT work w/o a rewrite of the names and that would probably require as much to do as just writing out the individual commands. i probably misled you to believe that there was something orderly to the names by my example. sorry that was not the intention there.

“The philosophy of the school room in one generation will be the philosophy of government in the next." --- Abraham Lincoln
 
I thought that would be the case and there is another method like

For Each ctrl in Form
' something
Next ctrl

I don't remeber the exact syntax off my head....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+
w: rljohnso@stewart.com
h: wildmage@tampabay.rr.com
 
yes, that's the one (you tell it what sorts of controls would be affected, checkboxes, text, comboboxes etc)but what is the syntax? there's the $64 question

“The philosophy of the school room in one generation will be the philosophy of government in the next." --- Abraham Lincoln
 
Here's from another thread that I just answered thread181-785996 . This isn't ideal, but it works:

Private Sub etc()
Dim i As Integer
For i = 0 To Controls.Count - 1
If TypeOf Controls(i) Is CommandButton Then
MsgBox "do something here"
End If
Next i
End Sub
 
Or another version, if you'd like the possibility of changing some but not all (and without renaming a lot of them), there's a tag property of most controls which can be used for instance thru something like this:

[tt]dim ctl as control
for each control in me.controls
select case ctl.controltype
case acCommandButton
if ctl.tag = <some value/text...> then
ctl.enabled= not ctl.enabled ' toggling property
end if
case acTextBox
' blah blah
end select
next ctl[/tt]

Press F1 on "controltype" and you'll find constants for all controltypes.

Roy-Vidar
 
hi roy vidar,
on my 'Print' button's OnClick event property's vba, what needs doing is turning off the enable property (or even possibly disappearing them from the form, as in setting their visible property off).

dim ctl as control
for each control in me.controls
select case ctl.controltype
case acCommandButton
' if ctl.tag = <some value/text...> then
ctl.enabled= not ctl.enabled ' toggling property
' end if
case acTextBox
' blah blah
end select
next ctl

could that be done with the modified version of your code above; note i've commented out the 'if' and 'end if'; does
'ctl.enabled = not ctl.enabled' mean that if it's enabled then turn it off? does it have the same effect as 'ctl.enabled = false'?




“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
Spot on!

The enabled property, as also the visible property of a control, are booleans, and can be evaluated to either true or false. So using the syntax (here, visible property):

[tt]ctl.visible = not ctl.visible[/tt]

means "toggle the values" and equals:

[tt]if ctl.visible = true then
ctl.visible = false
else
ctl.visible = true
end if[/tt]

Should you need only a specific property setting, for instance false, then use:

[tt]ctl.visible = False[/tt]

Roy-Vidar
 
great stuff, rov-vidar. if my memory serves, i think you could add other control types to the list and they'd all be acted on in the same way as our command buttons were. acCommandButton is a reserved term, yes?

“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
Correct, and as mentioned above:
myself said:
Press F1 on "controltype" and you'll find constants for all controltypes.

- thanx for the kind words and the star!

Roy-Vidar
 
hi, i bear a compiler error "Invalid next control variable reference" which arose when i added the following to my 'Print' button's OnClick event property VBA code:


Dim ctl As Control
For Each Control In Me.Controls
Select Case ctl.ControlType
Case acCommandButton
ctl.Enabled = False
End Select
Next ctl

have i done something goofy?

“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
Don't know. Haven't seen such error when using this. Only thing that I've encountered, is a runtime error stating that you can't alter some of the properties while the control has the focus. That one could be avoided with setting focus to a control that's not going to be manipulated thru this routine prior to executing this loop (i e; Me!SomeOtherControl.SetFocus).

Just some thoughts:
Try removing this part from the from the module again, and then compile.

Try inserting only this in another button click event, and see if it runs there.

None of this works, try creating a new database with only a form and multiple command buttons, and try it there.

- since I'm not able to see anything that strikes me as "wrong" I'm suspecting it might have to do with either the rest of your code, some of the names used here is in use for different purposes somewhere else in your code, or the code might be inserted in a part of your code creating some kind of conflict, or perhaps a form corruption...

I'm stumped, anyone else?

Roy-Vidar
 
hi, it struck me as strange that we say 'for each control' and then end by saying 'next ctl' so i switched 'ctl' for 'control' and at this point the compiler's not bellyacheing.

in the interim, i got to thinking whether there'd be a way to passover certain buttons....there are three in the footer that i would just as soon see uneffected. would there be some way to (in english) tell it something like "for each control in this form, but for the following with names like 'Command238' and 'EnableEditsbtn' set the enabled property of the control to false"? the two controls are always in the footer, and i noticed that there's a property that seems to let you addresst there location, so possibly there'd be some way to exclude them from the dis-abling by their common presence? just a wild guess.


“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
Aaaarrrgggghh - my fault all the way - sorry.

Should have been:

[tt]for each ctl in me.controls
' ....
next ctl[/tt]

I probably need new glasses I'm sure. Using the Control vs ctl is probably a matter of preference (think both works, but the latter is most commonly used in reference books)

As for avoiding, I mentioned something about that previously, theres a tag property of controls (other tab). Set it to someting, for instance "NoChange" on the few controls in question, and use a test for it:

[tt]if ctl.tag <> "NoChange" then
ctl.enabled = false
end if[/tt]

To use the location, one could for instance use:

[tt]for each ctl in me.detail.controls[/tt]
- formfooter, formheader is also available to narrow down where to perform the actions.

Roy-Vidar
 
hi roy-vidar,

i didn't quite understand the part about the tage property of controls, but thanks to your perspicacity you provided what i think's an even better alternative to avoiding the controls to be left alone by focusing just on the ones (in the detail) section which need switching. as an aside, what i found interesting was that when you follow 'me' with a '.' you don't see 'detail' in any look-up list which would tend to discourage me from believing it was even a real option, however, after typing it in manually and following it with a '.' you see that you can pick from a lookup list which has 'controls' amongst its candidate values. is this a 'hidden feature' of some kind or am i imagining things?



“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
In a controls properties, other tab (as mentioned) there's supposed to be a "tag" property. This might be set to some value that might be used when for instance looping thru controls.

The intellisense dropdown does not always show all the methods and properties available (on my version, neither controltype, name, value and other properties are not available, but I know they are there)...

But here it might be that you've altered the name of the section in question (name property for the section in question), then use that in the expression.

[tt]for each ctl in me.MyDetail.controls

or perhaps:

Me.Section("MyDetail").Visible = False[/tt]

- or just try it out

Roy-Vidar
 
it's interesting that there's this 'disconnect' thing in a2k's 'intellisense' documentation which prevents the uninitiated from ever learning there's a trove of valuable functionality available for the asking. were it not for this serendipitous exchange on tek-tips.com i would be count myself among their rank.

may i ask what is "a tag property of controls (other tab)" -- i sort of understand you to be saying that you can have 'tag' after 'ctl.' (much as you might 'visible'or what have you) and that if you make a test to see if, as you loop through your controls, any one of them has its 'tag' set to be other than 'No Change' then you do something. but, what,i ask myself, is a 'tag' property; perhaps then testing for 'No Change' would begin to make sense as well.

“The philosophy of the school room in one generation will be the philosophy of government in the next.&quot; --- Abraham Lincoln
 
Select a control (for instance a command button) in a forms design view. Bring up the property dialog. This dialog/window... has 5 tabs, one of them is the "other" tab. First property on that tab, is the controls "Name" property. The last property (lowest/bottom) is the "Tag" property. The Tag property might be altered either in the design view, or in VBA (and, the helpfiles also says thru a macro - (please excuse a slight shudder whilst writing this last part;-))

<aside>Not entirely precise, but "all" the properties seen in the property dialog is available also at runtime (some of them are read/only at run time) - so one might also perceive the property dialog as an addition to the intellisense dropdown (of course other needed recourses are good reference books, help files, internet...)</aside>

From the help files on the tag property:
Unlike the other properties, the Tag property setting doesn't affect any of an object's attributes.

In other words - a free property we can use for whatever we like, for instance like here, to store extra information about this control.

This is very usefull for the situations where you'd like to perform some actions on all objects "except a selected few". Then you'd just check the tag property for which controls to perform the actions against.

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top