Link to home
Start Free TrialLog in
Avatar of szx248
szx248

asked on

getting this runtime error 2166?

I am trying to lock a control (textbox) in a sub on a change event on another textbox. I am getting this error:

run-time error 2166
You can't lock a control while it has unsaved changes

Now I have not made any changes to this control that I'm trying to lock; when I test it it shows a value of Null.
Avatar of John Mc Hale
John Mc Hale
Flag of Ireland image

Can you be more specific and post some code?
Avatar of szx248
szx248

ASKER

in an On Change (same with After Update) when I run this

If (Val(Nz(Me.CountMale.Text, 0)) + Val(Nz(Me.CountFemale, 0))) > 1 Then
    Me.Age.Value = ""
    Me.Age.Enabled = False
    Me.Age.Locked = True        << -- error on this line
    Me.Age.BackColor = 14215660
Else
    Me.Age.Enabled = True
    Me.Age.Locked = False
    Me.Age.BackColor = vbWhite
End If
try:

If (Val(Nz(Me.CountMale.Text, 0)) + Val(Nz(Me.CountFemale, 0))) > 1 Then
    Me.Age.Value = Null
    Call Age_AfterUpdate
    Me.Age.Enabled = False
    Me.Age.Locked = True        << -- error on this line
    Me.Age.BackColor = 14215660
Else
    Me.Age.Enabled = True
    Me.Age.Locked = False
    Me.Age.BackColor = vbWhite
End If
Try...

Private Sub txtMyTextBox_AfterUpdate()
   If  (Val(Me.CountMale & "") + Val(Me.CountFemale & "")) > 1 Then
       Me.Age.Value = ""
       Me.Age.Enabled = False
       Me.Age.Locked = True
       Me.Age.BackColor = 14215660
   Else
       Me.Age.Enabled = True
       Me.Age.Locked = False
       Me.Age.BackColor = vbWhite
   End If
End Sub

Note:  Definately us the AfterUpdate Event Not the Change Event
Avatar of szx248

ASKER

fredthered

I don't have an sub Age_AfterUpdate.

But trying the rest of what you suggested didn't change getting the error.
Maybe I should point out that this code is not running on an event of the Age conrol but on another control on that same form.


Avatar of szx248

ASKER

Rick_Rickards,

what do you mean by "Definately us the AfterUpdate Event Not the Change Event"?

Why can't I lock age from an On Change even of another control on that form?
ASKER CERTIFIED SOLUTION
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of szx248

ASKER

Rick_Rickards

thanks for the very explanation on value/text.
But in my case I still can't do the lock without getting the error.

I open a form with a few controls - ClientCode, CountMale, CountFemale, Age. And after skipping ClientCode, I am in CountMale, and based on what I enter in CountMale I try to Lock Age then I get above mentioned error! I am trying to do this with the CountMale ON Change event
Why code would fail when trying to set a control’s .Locked property to True is indeed a mystery.  To be honest I have no idea why this would happen.  Something is missing from the picture and I'd invite anyone reading this thread to offer any theories they may have.

Rick
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial