Locking Txtbox with an issue

  • Thread starter Thread starter melbourno
  • Start date Start date
M

melbourno

I'm currently using the below code to lock a textbox after the user
enter their information. Because I don't want them to go back and
change it. However, after the user saves the entry and wants to enter
new information as new record it doesn't allow him.

Private Sub Text91_AfterUpdate()
Me.Text91 = Now & ", " & vbCrLf & Me.Text91
Screen.ActiveControl.Locked = Not IsNull(Screen.ActiveControl)
End Sub
 
Is this a continuous form (or a datasheet form)?

If so, you cannot do what you're trying to do. While it may look as though
you've got, say, 20 different Text91 controls on your form, in actual fact
you've only got one, repeated 20 times, so locking it locks all occurrances.

You can put code in the control's BeforeUpdate event to prevent changes:

Private Sub Text91_BeforeUpdate(Cancel As Integer)

If IsNull(Me!Text91.OldValue) = False Then
Me!Text91.Undo
End If

End Sub
 
On the "on current" event, you could use similar code to unlock the
textbox if it is null.
 
On Wed, 25 Feb 2009 06:22:28 -0800 (PST), (e-mail address removed) wrote:

You can test if you're on a new record with the NewRecord property:
If Me.NewRecord then
'unlock the control
Else
'your current code
End If

BTW, rather than Screen.ActiveControl I would use the actual control
name. And give them decent names. "Text91" is not very descriptive.

-Tom.
Microsoft Access MVP
 
This code locked my txtbox before I was able to enter any information
in it.

Private Sub Text91_BeforeUpdate(Cancel As Integer)
If IsNull(Me!Text91.OldValue) = False Then
Me!Text91.Undo
End If
End Sub
 
I tested this code and still doesn't allow to edit or type anything in
Text91 as new record.


Private Sub Text91_AfterUpdate()
If Me.NewRecord Then
'unlock Text91
Else
'your current code
End If
Me.Text91 = Now & ", " & vbCrLf & Me.Text91
Me.Text91.Locked = Not IsNull(Me.Text91)
End Sub
 
Not quite sure what you mean by "locked my txtbox". That code doesn't lock
anything, it just prevents you from saving any change that's made if the
OldValue for the text box wasn't Null.

Do you perhaps have a DefaultValue set for the field, so that the OldValue
is never null?
 
Back
Top