locking information

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I want my form to load so that you can't change any
information in the records and if you want to change some
information then you have to click a button and then
change the data. Right now I have a for loop that checks
the tag property in the text boxes to see if there is a
the phrase "Lock" in it and if it finds it it, then it
will lock the text box. This is the code that I have:

Dim txt As TextBox

For Each myTxt In Me
If myTxt.Tag = "Lock" Then myTxt.Locked = True
End If
Next myTxt

Right now it says run time error 13 type mismatch and it
highlights the Next myTxt.

I would like it to also lock the check boxes and combo
boxes also if it is possible.
 
If you positively want none of the information to be edited, just change the
properties of the form. Under the Data tab, set Allow Edits to No (you
should probably set Allow Additions and Allow Deletions to No, as well, to
prevent people from creating and deleting records).

You can set the value of this in VBA using the Me.AllowEdits property.

HTH,

Marshall Smith
Project Developers, Inc.
 
Dan said:
I want my form to load so that you can't change any
information in the records and if you want to change some
information then you have to click a button and then
change the data. Right now I have a for loop that checks
the tag property in the text boxes to see if there is a
the phrase "Lock" in it and if it finds it it, then it
will lock the text box. This is the code that I have:

Dim txt As TextBox

For Each myTxt In Me
If myTxt.Tag = "Lock" Then myTxt.Locked = True
End If
Next myTxt

Right now it says run time error 13 type mismatch and it
highlights the Next myTxt.

I would like it to also lock the check boxes and combo
boxes also if it is possible.


A form's Controls collection can contain many other controls
than just text boxes. Since you're using the Tag property
to indicate the controls to lock, you don't have to be aware
of the type of the control, so your loop could look more
like:

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Lock" Then ctl.Locked = True
Next ctl
 
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Lock" Then ctl.Locked = True
Next ctl

VB tells me that the it does not support this method:
ctl.Locked = True. Is there something else that you have
to do get it to work. Also do you have to have a "End If"
statement before the "Next ctl" statement?
 
For some reason this propert doesn't seem to work:
Me.AllowEdits. It doesn't give any error or anything it
just doesn't lock fields
 
Hi Dan,

I don't see where you've declared myTxt (I'm guessing that it is declared as
textbox as is txt??). The For..Each works with the controls collection
because it enumerates all controls. I suspect the type mismatch occurs when
it hits a non-textbox control. Consider the following revisions:

Dim mytxt As Control
For Each mytxt In Me.Controls
If mytxt.Tag = "Lock" Then
mytxt.Locked = True
End If
Next mytxt
 
Dan said:
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Lock" Then ctl.Locked = True
Next ctl

VB tells me that the it does not support this method:
ctl.Locked = True. Is there something else that you have
to do get it to work

That will work, unless you've set the Tag property to "Lock"
on some other controls that can't be locked (labels, lines,
etc)

Also do you have to have a "End If"
statement before the "Next ctl" statement?

Not if it's on one line. You would if you use this form

If ctl.Tag = "Lock" Then
ctl.Locked = True
End If
 
Back
Top