473,401 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Is there a quick way to loop through all the controls on a form?

Seth Schrock
2,965 Expert 2GB
I'm needing to set the Locked property of 31 of the 32 controls on my form. I was hoping that there would be a way to loop through all of the controls on the form. Inside the loop, I would test to see if the control's name was equal to the 1 control that doesn't need locked. If it was, then leave it unlocked. If not, then lock it. Is this possible?
Jan 17 '13 #1

✓ answered by NeoPa

Seth,

As an example of how to do it without relying on error handling, you could try :
Expand|Select|Wrap|Line Numbers
  1. Dim ctlVar as Control
  2. For Each ctlVar In Me.Controls
  3.     Select Case ctlVar.ControlType
  4.     Case acCheckBox, acComboBox, acTextBox, ...  ' Use all you need here.
  5.         ctlVar.Locked = Not (ctlVar Is Me.ControlName)
  6.     End Select
  7. Next ctlVar
The predefined constants for all the different control types can be found by searching for help on "ControlType" using Context-Sensitive Help.

15 5686
zmbd
5,501 Expert Mod 4TB
yep,
You can do a for...each loop thru the controls group and test for names and types.

I've had to hunt for the types and it's not well documented.

Give me a tad, I may have my diagnostics loop here at the house.
Jan 17 '13 #2
TheSmileyCoder
2,322 Expert Mod 2GB
Yes, its quite simple really
Expand|Select|Wrap|Line Numbers
  1. Dim ctrl as Control
  2. For each ctrl in me.Controls
  3.   ctrl.locked=ctrl.name<>"YOURCONTROLNAME"
  4. next
Jan 17 '13 #3
zmbd
5,501 Expert Mod 4TB
And that was the basically the code I was going to post.
The only difference is that I use a UCase(ctrl.name)<>"YOURCONTROLNAME" as case counts.
Jan 17 '13 #4
Seth Schrock
2,965 Expert 2GB
Just making sure that if I don't specify types, it will do them all. Is this correct? Or do I need to edit out those that don't have a locked property (tab controls)?
Jan 17 '13 #5
zmbd
5,501 Expert Mod 4TB
Using the code TheSmileyCoder has posted, The control "Type" wont matter as the search is based on the control's name.

Now, I do have a generic loop wherein I test for the control type and lock it for several of my forms. I do it this way because there is a ton of them... that might be the way to go given that you have some 30+ ctrls.

In that case you'll need basically same as TheSmileyCoder has posted; however, change the name property to type and then match by the type (ControlType Property [Access 2003 VBA Language Reference]
I don't think these have changed... or at least my forms haven't broken :)

You can put other logic in so that a specfic control or two are not locked based upon their name and so forth - the above link has an example
Jan 17 '13 #6
NeoPa
32,556 Expert Mod 16PB
Z:
The only difference is that I use a UCase(ctrl.name)<>"YOURCONTROLNAME" as case counts.
That's only true if you use :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Binary
Or your nationality is set to one twhere that is the default. I know of none which are. Otherwise case is ignored in VBA string comparisons.
Jan 17 '13 #7
TheSmileyCoder
2,322 Expert Mod 2GB
Zmbd:
The only difference is that I use a UCase(ctrl.name)<>"YOURCONTROLNAME" as case counts.
That is not entirely correct. It will depend on your module settings:

Access 2010 help:
Option Compare Statement

Used at module level to declare the default comparison method to use when string data is compared.

Syntax

Option Compare {Binary | Text | Database}

Remarks

If used, the Option Compare statement must appear in a module before any procedures.

The Option Compare statement specifies the string comparison method (Binary, Text, or Database) for a module. If a module doesn't include an Option Compare statement, the default text comparison method is Binary.

Option Compare Binary results in string comparisons based on a sort order derived from the internal binary representations of the characters. In Microsoft Windows, sort order is determined by the code page. A typical binary sort order is shown in the following example:
The default has always been "Option Compare Database" for me, and any modules I create (Stand-alone or form/report) have always come with that statement. You can use
Expand|Select|Wrap|Line Numbers
  1. strComp 
to force the compare one way or the other.
Jan 17 '13 #8
zmbd
5,501 Expert Mod 4TB
What I know is that the only change I make to the default options is to set the "explicit." Everything else is as out of the box {{so that this is at the top of every standard/class/class_object-module:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
}}


When I do a string comparison as shown in TheSmileyCoder's example when the cases of the letters within a string do not match then the comparison fails...

that is to state: "the" is not equal to "The" which is not equal to "THe"... (etc thru nPr) therefor to ensure that I always have a match against the control name (sometimes I'll miss having the shift-lock on which really messes with the case (Z_Ctrl_Name vs z_cTRL_nAME) (other people use my PC)) is to use that Ucase().
Jan 17 '13 #9
ADezii
8,834 Expert 8TB
  1. Set the 'Tag' Property of the single Control that you do not wish to Lock = NoLock:
  2. Execute the following Code:
    Expand|Select|Wrap|Line Numbers
    1. On Error Resume Next
    2. Dim ctl As Control
    3.  
    4. For Each ctl In Me.Controls
    5.   ctl.Locked = ctl.Tag <> "NoLock"
    6. Next
Jan 17 '13 #10
zmbd
5,501 Expert Mod 4TB
ADezii
That's certainly one option, and one I've used too; however, I use the tag property for a lot of other things so, for me, it's often not available for this use... you also have to be careful of those label controls.
:)
Jan 17 '13 #11
NeoPa
32,556 Expert Mod 16PB
Help:
Option Compare Database can only be used within Microsoft Access. This results in string comparisons based on the sort order determined by the locale ID of the database where the string comparisons occur.
I've never come across a situation before where the default is to compare as Binary. In such a case, of course, you are absolutely correct in your suggested code.

Z:
Using the code TheSmileyCoder has posted, The control "Type" wont matter as the search is based on the control's name.
I'm afraid that isn't accurate. The .Locked property is accessed for all controls, and any that don't support it will throw an error.

This would also be a problem with ADezii's version of course.
{Edit}
Not for ADezii's version as he uses error handling. This does, however, set how errors must be handled for that stretch of code. I generally prefer to code without relying on error handling where possible. It isn't always obvious that it can be.
Jan 17 '13 #12
NeoPa
32,556 Expert Mod 16PB
Seth,

As an example of how to do it without relying on error handling, you could try :
Expand|Select|Wrap|Line Numbers
  1. Dim ctlVar as Control
  2. For Each ctlVar In Me.Controls
  3.     Select Case ctlVar.ControlType
  4.     Case acCheckBox, acComboBox, acTextBox, ...  ' Use all you need here.
  5.         ctlVar.Locked = Not (ctlVar Is Me.ControlName)
  6.     End Select
  7. Next ctlVar
The predefined constants for all the different control types can be found by searching for help on "ControlType" using Context-Sensitive Help.
Jan 17 '13 #13
zmbd
5,501 Expert Mod 4TB
sigh... you're right of course... I'm going to take a nap... these 2am feedings are starting to wear me thin... almost ready to go back to work so I can get a nap in between tests!

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

(ps... the link I posted earlier has the predefined constants listed :-) at least I did that right )
Jan 18 '13 #14
NeoPa
32,556 Expert Mod 16PB
Lol - 2 AM Z? It's just clocked 05:00 here, and I'm struggling with a project I want to release before the users get to work later :-(

As for making mistakes, we all do that. It's nothing to beat yourself up over. Other experts are always picking up my errors. No worries. Better that than they stay there misleading anyone.

Z:
at least I did that right
That and much more. I'm glad you got that bit cause I didn't have a good link for it.
Jan 18 '13 #15
Seth Schrock
2,965 Expert 2GB
That worked perfectly NeoPa.

Thanks to everyone who has helped me with this.
Jan 18 '13 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jeremy Langworthy | last post by:
Hi I have a dynamicly generated form (well the elements are at least) that looks something like this: while( not end of returned records): <input name="plan_id" type="checkbox" id=""...
2
by: Brett | last post by:
The following code will allow me to loop through FORM tags but not the elements in them. I may have five forms on one page. How do I loop through form elements in the forth FORM? private...
10
by: Mohit Gupta | last post by:
Hello Friends I have written a simple HTML page in aspx I have added form elements like "textBox", "Hidden fields", "images", "radio buttons" to the html form I am posting the data back to the...
8
by: AFN | last post by:
I want to have a routine in a page base class that will take all the text fields on a web form, and then HtmlEncode their values. I'm having trouble figuring out if I want to loop controls or...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
1
by: RSH | last post by:
Hi, I have a situation where I have 10 HTMLFileInput controls on my aspx page. They are all named UploadedFile1,UploadedFile2... Now on postback I am trying to construct a loop to check for...
9
by: wreed | last post by:
I have a for loop seen below.... var the_form = document.getElementById(formName); for(var i=0; i<the_form.length; i++) { var temp = the_form.elements.type; if (temp == "radio") { for (x =...
1
yoda
by: yoda | last post by:
Sorry Ben3eeE about that i'm not very good at programming and i'm in high school I just want to know how to loop a form without the message box.
2
by: Trevor2007 | last post by:
I have a subform with a loop to check to make sure 1 of the values in the textbox = "N/A" before saving, but I can't seem to finish it, , well get it just to check for 1 N/A value, here is what I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.