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?
Seth,
As an example of how to do it without relying on error handling, you could try : - Dim ctlVar as Control
-
For Each ctlVar In Me.Controls
-
Select Case ctlVar.ControlType
-
Case acCheckBox, acComboBox, acTextBox, ... ' Use all you need here.
-
ctlVar.Locked = Not (ctlVar Is Me.ControlName)
-
End Select
-
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.
Yes, its quite simple really - Dim ctrl as Control
-
For each ctrl in me.Controls
-
ctrl.locked=ctrl.name<>"YOURCONTROLNAME"
-
next
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.
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)?
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
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 :
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.
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
to force the compare one way or the other.
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: - Option Compare Database
-
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().
- Set the 'Tag' Property of the single Control that you do not wish to Lock = NoLock:
- Execute the following Code:
-
On Error Resume Next
-
Dim ctl As Control
-
-
For Each ctl In Me.Controls
-
ctl.Locked = ctl.Tag <> "NoLock"
-
Next
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.
:)
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.
NeoPa 32,556
Expert Mod 16PB
Seth,
As an example of how to do it without relying on error handling, you could try : - Dim ctlVar as Control
-
For Each ctlVar In Me.Controls
-
Select Case ctlVar.ControlType
-
Case acCheckBox, acComboBox, acTextBox, ... ' Use all you need here.
-
ctlVar.Locked = Not (ctlVar Is Me.ControlName)
-
End Select
-
Next ctlVar
The predefined constants for all the different control types can be found by searching for help on "ControlType" using Context-Sensitive Help.
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 )
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.
That worked perfectly NeoPa.
Thanks to everyone who has helped me with this.
Sign in to post your reply or Sign up for a free account.
Similar topics
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=""...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |