PDA

View Full Version : Locking Form Controls



Glaswegian
03-18-2008, 05:28 AM
Hi again

I have a multi page userform with a variety of controls. Briefly, users complete certain fields, option buttons etc and data is transferred to a hidden sheet. Users can recall this data but cannot make any further amendments. To ensure they cannot amend any data I'm using this code, which works fine:

For Each ctl In frmMain.Controls
If Not TypeOf ctl Is MSForms.MultiPage Then
If Not ctl.Name Like "*Close*" Then
ctl.Object.Enabled = False
End If
End If
Next ctl
This ensures that several 'Close' command buttons are available to close the form.

However, disabling the controls makes the data slightly difficult to read. Is there a way to loop through and Lock the controls, rather than disable them? Would I need to loop through each control type - combobox, textbox optionbutton etc?

Thanks for any assistance or suggestions.

Bob Phillips
03-18-2008, 05:48 AM
Why does disbaling them make it difficult to read?

What do YOU men by lock?

mikerickson
03-18-2008, 05:48 AM
Would it work to set the control's .Visible property?

Glaswegian
03-18-2008, 05:52 AM
Apologies if I was not clear.

When the controls are disabled Excel makes the font fainter than it was. By locking, I mean that the control will look as normal but the user will not be able to amend the value, for example, when the form is loaded, I format the date field and then lock it, so that users cannot change it

.txbDate.Value = Format(Date, "dd mmmm yyyy")
.txbDate.Locked = True
The controls need to be visible.

Bob Phillips
03-18-2008, 05:57 AM
You could use a Boolean variable that is module scope, and set that to indicate disabled, and then check it in the controls event. Something like this air code



Private fDisabled As Boolean

Private Sub cmClose1_Click()

If Not fDisabled Then

'do your stuff
End If
End SUb


You will have to determine where the Boolean gets set to True and reset back.

Glaswegian
03-18-2008, 07:02 AM
I understand what you mean, but I don't want the fields disabled, I would prefer them to be locked. This means they would still retain the 'look' of an unlocked field, but users would not be able to amend any data.

Bob Phillips
03-18-2008, 07:09 AM
They wouldn't be disabled, you just cripple the event action.

Glaswegian
03-18-2008, 07:45 AM
Doh! Thanks!