How to Lock Fields using Module

  • Thread starter Thread starter cp2599
  • Start date Start date
C

cp2599

I'm trying to lock fields; change tabstop and backstyle using a
module; however, I can't seem to get the correct datatype defined.
Any help would be appreciated.

Public Function LockField (chrFieldName as ????)
chrFieldName.locked = true
chrFieldName.backstyle = 0
chrFieldName.tabstop = false

end function

LockField (Me.NameOfField)
 
Public Function LockField (chrFieldName as Strong)

With Me.Controls(chrFieldName)
.Locked = true
.Enabled = False
End With

end function

Is all you need
 
???

Since the poster wants to use LockField (Me.NameOfField), I'd suggest that
it should be

Public Function LockField (chrFieldName As Control)

chrFieldName.Locked = True
chrFieldName.BackStyle = 0
chrFieldName.TabStop = False

End Function

To use your approach (which I assume you meant to be

Public Function LockField (chrFieldName as String)

With Me.Controls(chrFieldName)
.Locked = True
.Enabled = False
End With

End Function

you'd need to use LockField Me.NameOfField.Name (or LockField
"NameOfField"), and it would only work if it was in the module associated
with the form.

To the original poster, the correct syntax to use the function would
actually be

Call LockField(Me.NameOfField)

or simply

LockField Me.NameOfField

When you use a function as a sub (i.e.: you don't assign its return value to
a variable), putting parentheses around the parameters actually changes the
meaning of the routine: it changes how you're passing the value from the
default ByRef to ByVal. In this particular case, you can probably get away
with it, but it will definitely fail if you have more than one parameter,
plus it not work as intended if the function changes the value of the
parameter and you expect to be able to use that changed value.
 
Okay, got your point, but with that asside, I believe my using locked and
enabled together provides the same visual effect and doesn't require altering
the tab or backcolor properties.

Seems more concise to me. But, you are right 99 44/100% of the time :)
 
Actually, the major point I was trying to make (which I admit was buried in
a comment) was that since your routine relied on using the Me keyword, it
had to be placed in the form's module, whereas my approach could be in a
stand-alone module, and hence used from any form. It could have been
rewritten as

Public Function LockField (chrFieldName As Control)

With chrFieldName
.Locked = True
.Enabled = False
End With

End Function
 
Back
Top