Locking all textbox fields on a form

C

CNA527

I am trying to write a piece of code that I can re-use on differnet forms.
The code locks all the textbox fields in a form so that a user can not edit
a record unless they click a command button called Edit. I also have a
separate piece of code (gUnlockFields) that reverses the changes by the code
below. The code below exists in a module. The code works fine when called
from the OnClick event of a command button. However, I would like this
piece of code to run when a form is opened by the user but I get an error
message because I am using "Screen.ActiveForm". How can I modify the code
below so that it will execute when the form is opened (or opening)?

Sub gLockFields()
On Error GoTo Err_gLockFields

Dim frm As Form
Dim ctl As Control
Dim intI As Integer, intCanEdit As Integer
Const conWhite = 16777215
Set frm = Screen.ActiveForm
For Each ctl In frm.Controls
With ctl
Select Case .ControlType
Case acTextBox
.SpecialEffect = acEffectNormal
.Enabled = True
.Locked = False
.BackColor = conWhite
End Select
End With
Next ctl

Exit_gLockFields:
Exit Sub

Err_gLockFields:
MsgBox Err.Description
Resume Exit_gLockFields

End Sub

AA
 
W

Wayne Morgan

Try passing the name of the form (Me.Name) to the routine.
Sub gLockFields(strFormName As String)

To call this you would use
gLockFields Me.Name

You could then refer to the form as
Forms(strFormName)
 
C

CNA527

Thanks, works great.

AA

Wayne Morgan said:
Try passing the name of the form (Me.Name) to the routine.
Sub gLockFields(strFormName As String)

To call this you would use
gLockFields Me.Name

You could then refer to the form as
Forms(strFormName)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top