473,587 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best method to selectively lock/disable controls on a form.

What I do now is I put a value in the tag property, and using the
form_current event, I run through all controls properties until the ones
with the required tag value are met. Sound OK in theory, but it is ...
slow.

I thought having a table with the name, type of controls and put my 'tag
value' there. At the opening of the form, I would retrieve a recordset from
the table, and perform a subroutine on the current event to lock/unlock
controls witht ehstored value.

But before spending time trying it, I wonder if anybody had to face this
situation and how to deal with it the best way. TIA.

Nov 13 '05 #1
7 9808

"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:t2******** *************@w agner.videotron .net...
What I do now is I put a value in the tag property, and using the
form_current event, I run through all controls properties until the ones
with the required tag value are met. Sound OK in theory, but it is ...
slow.

I thought having a table with the name, type of controls and put my 'tag
value' there. At the opening of the form, I would retrieve a recordset from the table, and perform a subroutine on the current event to lock/unlock
controls witht ehstored value.

But before spending time trying it, I wonder if anybody had to face this
situation and how to deal with it the best way. TIA.

Tags are metadata and are therefore goofy and inefficient. Enable-disable,
hide-unhide, lock-unlock controls from within the target form only.

Most forms are unique in that no two have the same collection of controls.
A universal solution would be therefore very complicated to implement, if
not impossible. Walking a recordset of control states is inefficient and
unnecessary.

Create a global Boolean variable to represent the state of the controls in
the form, whether the controls are locked or unlocked or whether the
controls are hidden or unhidden, etc. gvIsOn = True, or something similar.

Creat TWO procedures to set the controls whose states need to be changed.

***
Public Sub TurnOff()
'Disables or hides form and subform controls.
On Error GoTo ErrorHandler
cmbEID1.SetFocu s
txtIncdntEmp.En abled = False
txtIncdntEmp.Lo cked = False
cmbEID2.Enabled = False
txtSuperEmp.Ena bled = False
txtSuperEmp.Loc ked = False
txtIncdntDate.E nabled = False
txtIncdntTime.E nabled = False
cmbDeptID.Enabl ed = False
cmbOPID.Enabled = False
cmbTask.Enabled = False
cmbZID.Enabled = False
ckbCI.Enabled = False
ckbClosed.Enabl ed = False
optgrpClass.Ena bled = False
bxHS.Visible = False
bxE.Visible = False
bxQ.Visible = False
cmbTypeHS.Enabl ed = False
cmbBodyPart.Ena bled = False
txtLostDay.Enab led = False
txtExpAmt.Enabl ed = False
optgrpExpUnit.E nabled = False
cmbTypeE.Enable d = False
txtInvolve.Enab led = False
txtEMSTrack.Ena bled = False
cmbTypeQ.Enable d = False
txtCustomer.Ena bled = False
txtOrderNum.Ena bled = False
txtProductLot.E nabled = False
sbfIncident1.Vi sible = False
sbfIncident2.Vi sible = False
txtIncdntDescri pt.Enabled = False
txtCostAct.Enab led = False
txtCostCalc.Ena bled = False
txtCause.Enable d = False
txtICAT.Enabled = False
optgrpSeverity. Enabled = False
optgrpProbabili ty.Enabled = False
optgrpCAW.Enabl ed = False
txtFCAT.Enabled = False
txtCAEmp.Enable d = False
txtCAEmp.Locked = False
cmbCAEID.Enable d = False
txtCADate.Enabl ed = False
optgrpExternal. Enabled = False
txtENDate.Enabl ed = False
txtENEmp.Enable d = False
txtENEmp.Locked = False
cmbENEID.Enable d = False
txtRMEmp.Enable d = False
txtRMEmp.Locked = False
cmbRMID.Enabled = False
txtDateReview.E nabled = False
ckbxIncludeCTP. Enabled = False
tpg1.Enabled = False
sbfFormal.Visib le = False
lblStart.Visibl e = True
gvIsOn = False 'Sets global flag.
ExitRoutine:
Exit Sub
ErrorHandler:
MsgBox Err.Description
Resume ExitRoutine
End Sub
***

Public Sub TurnOn()
'Enables or unhides form and subform controls.
On Error GoTo ErrorHandler
txtIncdntEmp.En abled = True
txtIncdntEmp.Lo cked = True
cmbEID2.Enabled = True
txtSuperEmp.Ena bled = True
txtSuperEmp.Loc ked = True
txtIncdntDate.E nabled = True
txtIncdntTime.E nabled = True
cmbDeptID.Enabl ed = True
cmbOPID.Enabled = True
cmbTask.Enabled = True
cmbZID.Enabled = True
ckbCI.Enabled = True
ckbClosed.Enabl ed = True
optgrpClass.Ena bled = True
txtIncdntDescri pt.Enabled = True
txtCostAct.Enab led = True
txtCostCalc.Ena bled = True
txtCause.Enable d = True
txtICAT.Enabled = True
optgrpSeverity. Enabled = True
optgrpProbabili ty.Enabled = True
optgrpCAW.Enabl ed = True
optgrpExternal. Enabled = True
txtRMEmp.Enable d = True
txtRMEmp.Locked = True
cmbRMID.Enabled = True
txtDateReview.E nabled = True
ckbxIncludeCTP. Enabled = True
tpg1.Enabled = True
sbfFormal.Visib le = True
lblStart.Visibl e = False
gvIsOn = True 'Sets global flag.
ExitRoutine:
Exit Sub
ErrorHandler:
MsgBox Err.Description
Resume ExitRoutine
End Sub

***

In the On Current event of the form, apply the criteria you want to use to
activate or deactivate the form's specific controls. In the example below,
the form is mostly deactivated when a new record is about to be started.
But if the form is displaying an existing record, then the form is fully
activated. By checking the value of the global variable gvIsOn, we avoid
turning the form on or off unnecessarily when a user pages through the
records.

*** Snippet***

If IsNull([MyID]) Then 'New record.
TurnOff
Else 'Existing record. Activate the form only if needed.
If gvIsOn = False Then TurnOn
End If
*** Snippet***
The principle downside to this solution is that each time a control is added
or removed from the form, the code must be updated to reflect the design
change. On the other hand, the code is exceptionally fast becuase it uses a
minimum of resources.

Nov 13 '05 #2
Hi,

You could loop thru a recordset to obtain these settings as you said.

The "cons" (well, the "pros", too) of that approach is that you would have
to open the table directly, or from a different form in order to
select/designate these properties...

I decided to give this a try, and this is what I came up with ...a table
named:
**tblEnableCont rols**
ControlName - Text
ControlEnabled - Yes/No
ControlLocked - Yes/No
TypeOfControl - Text
(This field actually fills with the Constant rather than the actual readable
value, unfortunately. i.e. "106" instead of "vbCheckbox ") Darn!!

Then I used DLookup to find the setting in this table, and used that to set
the control properties.

I designed a seperate Sub which I can call in response to more than one
event. I wanted to be able to call it from the Form Load event, as well as
from a command button

I also realized that some ControlTypes (i.e. Labels) don't have Enabled /
..Locked properties, so you'll have have to include code to test and exclude
them. This code only sets the values for TextBoxes or CheckBoxes, using an
If statement. If there were a lot of other control types on your form, you
might want to use Select Case instead.

*************** *************** *************** **
Option Compare Database
Option Explicit

'-----------------------------------
Private Sub cmdSetEnabled_C lick()
sSetControlProp erties
End Sub

'-----------------------------------
Private Sub Form_Load()
sSetControlProp erties
End Sub

'-----------------------------------
Public Sub sSetControlProp erties()

Dim ctl As Control
Dim strName

For Each ctl In Me.Controls
strName = ctl.Name
If ctl.ControlType = 106 Or ctl.ControlType = 109 Then
'Checkbox = 106, Textbox = 109
ctl.Enabled = DLookup("[ControlEnabled]", "tblEnableContr ols",
"([ControlName] = '" & strName & "')")
ctl.Locked = DLookup("[ControlLocked]", "tblEnableContr ols",
"([ControlName] = '" & strName & "')")
End If
Next ctl

End Sub

'-----------------------------------
Private Sub cmdPopulateTabl e_Click()
'This code populates "tblEnableContr ols" with
'the names and control types of all of the
'controls on this form. (Yeah ... I'm lazy.)

Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim rst As DAO.Recordset
Set rst = MyDB.OpenRecord set("tblEnableC ontrols", dbOpenDynaset)

Dim ctl As Control
Dim strName
For Each ctl In Me.Controls
strName = ctl.Name
With rst
.AddNew
!ControlName = strName
!ControlEnabled = True
!ControlLocked = False
!TypeOfControl = ctl.ControlType
.Update
End With
Next ctl

Set rst = Nothing
Set MyDB = Nothing
End Sub

*************** *************** *************** **

HTH,
Don
"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:t2******** *************@w agner.videotron .net...
What I do now is I put a value in the tag property, and using the
form_current event, I run through all controls properties until the ones
with the required tag value are met. Sound OK in theory, but it is ...
slow.

I thought having a table with the name, type of controls and put my 'tag
value' there. At the opening of the form, I would retrieve a recordset from the table, and perform a subroutine on the current event to lock/unlock
controls witht ehstored value.

But before spending time trying it, I wonder if anybody had to face this
situation and how to deal with it the best way. TIA.


Nov 13 '05 #3
rkc

"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:t2******** *************@w agner.videotron .net...
What I do now is I put a value in the tag property, and using the
form_current event, I run through all controls properties until the ones
with the required tag value are met. Sound OK in theory, but it is ...
slow.

I thought having a table with the name, type of controls and put my 'tag
value' there. At the opening of the form, I would retrieve a recordset from the table, and perform a subroutine on the current event to lock/unlock
controls witht ehstored value.

But before spending time trying it, I wonder if anybody had to face this
situation and how to deal with it the best way. TIA.


Even in the simplest of cases I believe the information you have in the
tag property belongs in a table. The information should be read from the
table when the form opens and stored in an object that persists for the life
of the form. Again in the simplest of cases that object could be a simple
collection or array. In more complex cases where the state of a control
changes under more than a single circumstance you might want to spend
some time creating a class that monitors the form and manages the
controls as needed. The worst thing you could do is scatter that logic
all over the form's code module in the control event procedures.


Nov 13 '05 #4
In what is it that bad? I considered creating the recordset in the form
declarations (header), use a private subroutine and use the unload event to
set it to nothing.

controls as needed. The worst thing you could do is scatter that logic
all over the form's code module in the control event procedures.

Nov 13 '05 #5
Thanks for your help. But I am a bit afraid of using so much the Dlookup
function. I will give it a try with a copy of the recordset, using
recordsetclone.

For Each ctl In Me.Controls
strName = ctl.Name
If ctl.ControlType = 106 Or ctl.ControlType = 109 Then
'Checkbox = 106, Textbox = 109
ctl.Enabled = DLookup("[ControlEnabled]", "tblEnableContr ols",
"([ControlName] = '" & strName & "')")
ctl.Locked = DLookup("[ControlLocked]", "tblEnableContr ols",
"([ControlName] = '" & strName & "')")
End If
Next ctl

End Sub

'-----------------------------------
Private Sub cmdPopulateTabl e_Click()
'This code populates "tblEnableContr ols" with
'the names and control types of all of the
'controls on this form. (Yeah ... I'm lazy.)

Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim rst As DAO.Recordset
Set rst = MyDB.OpenRecord set("tblEnableC ontrols", dbOpenDynaset)

Dim ctl As Control
Dim strName
For Each ctl In Me.Controls
strName = ctl.Name
With rst
.AddNew
!ControlName = strName
!ControlEnabled = True
!ControlLocked = False
!TypeOfControl = ctl.ControlType
.Update
End With
Next ctl

Set rst = Nothing
Set MyDB = Nothing
End Sub

*************** *************** *************** **

HTH,
Don
"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:t2******** *************@w agner.videotron .net...
What I do now is I put a value in the tag property, and using the
form_current event, I run through all controls properties until the ones
with the required tag value are met. Sound OK in theory, but it is ...
slow.

I thought having a table with the name, type of controls and put my 'tag
value' there. At the opening of the form, I would retrieve a recordset

from
the table, and perform a subroutine on the current event to lock/unlock
controls witht ehstored value.

But before spending time trying it, I wonder if anybody had to face this
situation and how to deal with it the best way. TIA.



Nov 13 '05 #6

"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:h7******** *************@w agner.videotron .net...
In what is it that bad? I considered creating the recordset in the form
declarations (header),


Oops. I meant only declaring the recordset variables, creating them in
form_load event.
Nov 13 '05 #7
rkc

"Saintor" <sa******@REMOV ETHIShotmail.co m> wrote in message
news:h7******** *************@w agner.videotron .net...
In what is it that bad? I considered creating the recordset in the form
declarations (header), use a private subroutine and use the unload event to set it to nothing.

Nothings 'bad' about either method. Tags or table. I just find that keeping
the information in a table is easier to maintain and seems more inline
with the fact that you have a database readily available to store this type
of information. On the other hand, if speed is your major concern, then
hard code everything as was suggested by someone else. Not everything
has to be clever and nothings going to run faster than that.
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2843
by: el_roachmeister | last post by:
Is there a way to make a text link post to a form without passing all the parameters in the url? The urls tend to get very long and messy. I often wonder if there is a limit to how long they can get?
4
5808
by: Tom | last post by:
I have the following code as part of a procedure in a standard module: Dim ctrl As Control For Each ctrl In MyForm.Controls If ctrl.ControlType = acTextBox Then MsgBox ctrl.name MsgBox ctrl.Locked ctrl.Locked = True End If Next ctrl
1
4373
by: Ryan Moore | last post by:
is there any way to "lock" all controls? something like : myControl.LockOthers(); myControl.ReleaseOthers(); thnx
10
14352
by: Robert Jacobson | last post by:
Hi, I'm develing a COM add-in for Microsoft Word XP that displays a form. I'd like to have the form display using the Windows XP theme. However, neither using a manifest nor calling Application.EnableVisualStyles does the trick. (EnableVisualStyles works but massive instability, probably because the system is trying to theme the Word...
7
23043
by: Scott Emick | last post by:
How can I disable events for the controls on a form? I tried setting the form's enable property to false, but that doesn't stop events from firing on its controls. I need to temporarily disable a form and load values into controls without any events firing, then re-enable the form. -- Scott Emick Web Programmer
5
2533
by: cotton_gear | last post by:
Hello, When I clicked on the Reset button I need to reset only few fields selectively. How do i achieve it using javascrript ? Actually, when reset is clicked, even hidden fields are reset. I need to prevent this. Please suggest, i ma quite tensed as the deadline is close. Thanks a ton,.
0
1796
by: Ahmad Jalil Qarshi | last post by:
Hi! I have a problem while developing some webpages.The Problem is that:- How We Can Disable The Controls Of One Web Form From Other Web Form In Asp.net? Explanation:- There Should Be Two Web Form,Web Form1.aspx and Web Form2.aspx,Now From Web Form1.aspx When I Click One Button (Disable), Then Web Form2.aspx Buttons
3
4675
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The form that has the System.Windows.Forms.LinkLabel controls on it is in a different project and under a different namespace from the file where the...
8
5088
by: freeskier | last post by:
I have been using the following code to cycle through a subform and disable all textboxes on a form. If a textbox on the form has the focus when this is run I get error "can't disable a control when it has the focus." When I add a line like "txtTest.SetFocus" where txtTest is a textbox on the main form I still get the same error. How do I resolve...
0
7920
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.