By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,234 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,234 IT Pros & Developers. It's quick & easy.

Lock Textboxes

P: n/a
Tom
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

The first message box shows the correct name of each textbox on the referenced
form. The second message box shows False for each textbox on the referenced
form.

Why doesn't ctrl.locked = True lock each text box on the referenced form?

Thanks!

Tom
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Tom" <th*******@bellsouth.net> wrote in message
news:OQ******************@newsread1.news.atl.earth link.net...
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

The first message box shows the correct name of each textbox on the referenced form. The second message box shows False for each textbox on the referenced
form.

Why doesn't ctrl.locked = True lock each text box on the referenced form?


It should be. How are you testing that it isn't?
--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 12 '05 #2

P: n/a
Could it be that you didn't properly reference the Form? I get a "variable
not defined" compile error on MyForm.Controls when I try to run your code
from a standard module.

When I replace "MyForm.Controls" with

"Forms!MyForm.Controls"

your code works for me. Well, your code, with the message boxes commented
out and changed to toggle the lock state, not just set it, to wit.

Public Sub ToggleLock()
Dim ctrl As Control
For Each ctrl In Forms!MyForm.Controls
If ctrl.ControlType = acTextBox Then
' MsgBox ctrl.Name
' MsgBox ctrl.Locked
ctrl.Locked = Not ctrl.Locked
End If
Next ctrl
End Sub

I called the Public Sub from the click event of a Command Button on the
Form.

"Tom" <th*******@bellsouth.net> wrote in message
news:OQ******************@newsread1.news.atl.earth link.net...
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

The first message box shows the correct name of each textbox on the referenced form. The second message box shows False for each textbox on the referenced form.

Why doesn't ctrl.locked = True lock each text box on the referenced form?

Thanks!

Tom

Nov 12 '05 #3

P: n/a
Since you are displaying the MsgBox before you lock the control, the MsgBox is showing the
state of the control before you lock it.

--
Wayne Morgan
"Tom" <th*******@bellsouth.net> wrote in message
news:OQ******************@newsread1.news.atl.earth link.net...
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

The first message box shows the correct name of each textbox on the referenced
form. The second message box shows False for each textbox on the referenced
form.

Why doesn't ctrl.locked = True lock each text box on the referenced form?

Thanks!

Tom

Nov 12 '05 #4

P: n/a
bo*****@localhost.not (Larry Linson) wrote in
<1j*****************@nwrddc02.gnilink.net>:
When I replace "MyForm.Controls" with

"Forms!MyForm.Controls"

your code works for me. Well, your code, with the message boxes
commented out and changed to toggle the lock state, not just set
it, to wit.

Public Sub ToggleLock()
Dim ctrl As Control
For Each ctrl In Forms!MyForm.Controls
If ctrl.ControlType = acTextBox Then
' MsgBox ctrl.Name
' MsgBox ctrl.Locked
ctrl.Locked = Not ctrl.Locked
End If
Next ctrl
End Sub

I called the Public Sub from the click event of a Command Button
on the Form.


If I were writing code like this, I'd pass the subroutine a Boolean
argument and use that to set .Locked.

Of course, I'd never do this any way except with a custom
collection, because I've never had a form in which I had only
textboxes that I wanted locked, or in which I wanted all the
controls locked at once. In fact, if you want it uneditable, as
long as you've got a record loaded, it's easier to just set the
form's .AllowEdits property to False. Locking individual controls
is most useful when you want some editable and some not, and in
that case you'd be better off creating a custom collection of those
controls when you load the form and using that collection when you
want to lock/unlock the controls.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.