473,499 Members | 1,974 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 9798

"Saintor" <sa******@REMOVETHIShotmail.com> wrote in message
news:t2*********************@wagner.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.SetFocus
txtIncdntEmp.Enabled = False
txtIncdntEmp.Locked = False
cmbEID2.Enabled = False
txtSuperEmp.Enabled = False
txtSuperEmp.Locked = False
txtIncdntDate.Enabled = False
txtIncdntTime.Enabled = False
cmbDeptID.Enabled = False
cmbOPID.Enabled = False
cmbTask.Enabled = False
cmbZID.Enabled = False
ckbCI.Enabled = False
ckbClosed.Enabled = False
optgrpClass.Enabled = False
bxHS.Visible = False
bxE.Visible = False
bxQ.Visible = False
cmbTypeHS.Enabled = False
cmbBodyPart.Enabled = False
txtLostDay.Enabled = False
txtExpAmt.Enabled = False
optgrpExpUnit.Enabled = False
cmbTypeE.Enabled = False
txtInvolve.Enabled = False
txtEMSTrack.Enabled = False
cmbTypeQ.Enabled = False
txtCustomer.Enabled = False
txtOrderNum.Enabled = False
txtProductLot.Enabled = False
sbfIncident1.Visible = False
sbfIncident2.Visible = False
txtIncdntDescript.Enabled = False
txtCostAct.Enabled = False
txtCostCalc.Enabled = False
txtCause.Enabled = False
txtICAT.Enabled = False
optgrpSeverity.Enabled = False
optgrpProbability.Enabled = False
optgrpCAW.Enabled = False
txtFCAT.Enabled = False
txtCAEmp.Enabled = False
txtCAEmp.Locked = False
cmbCAEID.Enabled = False
txtCADate.Enabled = False
optgrpExternal.Enabled = False
txtENDate.Enabled = False
txtENEmp.Enabled = False
txtENEmp.Locked = False
cmbENEID.Enabled = False
txtRMEmp.Enabled = False
txtRMEmp.Locked = False
cmbRMID.Enabled = False
txtDateReview.Enabled = False
ckbxIncludeCTP.Enabled = False
tpg1.Enabled = False
sbfFormal.Visible = False
lblStart.Visible = 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.Enabled = True
txtIncdntEmp.Locked = True
cmbEID2.Enabled = True
txtSuperEmp.Enabled = True
txtSuperEmp.Locked = True
txtIncdntDate.Enabled = True
txtIncdntTime.Enabled = True
cmbDeptID.Enabled = True
cmbOPID.Enabled = True
cmbTask.Enabled = True
cmbZID.Enabled = True
ckbCI.Enabled = True
ckbClosed.Enabled = True
optgrpClass.Enabled = True
txtIncdntDescript.Enabled = True
txtCostAct.Enabled = True
txtCostCalc.Enabled = True
txtCause.Enabled = True
txtICAT.Enabled = True
optgrpSeverity.Enabled = True
optgrpProbability.Enabled = True
optgrpCAW.Enabled = True
optgrpExternal.Enabled = True
txtRMEmp.Enabled = True
txtRMEmp.Locked = True
cmbRMID.Enabled = True
txtDateReview.Enabled = True
ckbxIncludeCTP.Enabled = True
tpg1.Enabled = True
sbfFormal.Visible = True
lblStart.Visible = 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:
**tblEnableControls**
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_Click()
sSetControlProperties
End Sub

'-----------------------------------
Private Sub Form_Load()
sSetControlProperties
End Sub

'-----------------------------------
Public Sub sSetControlProperties()

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]", "tblEnableControls",
"([ControlName] = '" & strName & "')")
ctl.Locked = DLookup("[ControlLocked]", "tblEnableControls",
"([ControlName] = '" & strName & "')")
End If
Next ctl

End Sub

'-----------------------------------
Private Sub cmdPopulateTable_Click()
'This code populates "tblEnableControls" 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.OpenRecordset("tblEnableControls", 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******@REMOVETHIShotmail.com> wrote in message
news:t2*********************@wagner.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******@REMOVETHIShotmail.com> wrote in message
news:t2*********************@wagner.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]", "tblEnableControls",
"([ControlName] = '" & strName & "')")
ctl.Locked = DLookup("[ControlLocked]", "tblEnableControls",
"([ControlName] = '" & strName & "')")
End If
Next ctl

End Sub

'-----------------------------------
Private Sub cmdPopulateTable_Click()
'This code populates "tblEnableControls" 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.OpenRecordset("tblEnableControls", 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******@REMOVETHIShotmail.com> wrote in message
news:t2*********************@wagner.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******@REMOVETHIShotmail.com> wrote in message
news:h7*********************@wagner.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******@REMOVETHIShotmail.com> wrote in message
news:h7*********************@wagner.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
2818
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...
4
5795
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...
1
4368
by: Ryan Moore | last post by:
is there any way to "lock" all controls? something like : myControl.LockOthers(); myControl.ReleaseOthers(); thnx
10
14329
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...
7
22977
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...
5
2523
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...
0
1786
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...
3
4662
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...
8
5076
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...
0
7131
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
7174
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,...
0
7220
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...
1
4919
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...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
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...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.