• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1171
  • Last Modified:

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.
0
szx248
Asked:
szx248
  • 4
  • 3
  • 3
2 Solutions
 
John Mc HaleForensic Computer Examiner, Analyst/Programmer & Database ArchitectCommented:
Can you be more specific and post some code?
0
 
szx248Author Commented:
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
0
 
John Mc HaleForensic Computer Examiner, Analyst/Programmer & Database ArchitectCommented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Rick_RickardsCommented:
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
0
 
szx248Author Commented:
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.


0
 
szx248Author Commented:
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?
0
 
Rick_RickardsCommented:
The Change Event...
   1) Fires every time anything in the control changes (when changes are being executed from within the control itself)
   2) Avails the contents of the control only via the .Text property instead of the default property .Value.  If you want access to the data within the control you'll need to enter Me.txtMyControl.Text to get it's value because Me.txtMyControl or Me.txtMyControl.Value will reflect what was there, not what is there now.

The After Update Event...
   1) Fires 1 time, after the control has been updated
   2) Once the event fires the value of .Text and .Value will be updated to match (well almost - more on that latter).
   3) Using After_Update in liue of Change can elmiinate fliker that can unfortunately accompany the Change event as Change fires with each keystroke, After_Update does not.

When the value of .Text and .Value doesn't match.

.Value (accurate only when the After_Update event has fired) reflects the true value of the control.  If the value of the control is anything other than an empty string and/or Null .Text will match the .Value.  If, however, the actual value of the control is Null .Text will claim it is an empty string when in fact it is null.  The .Value property, however, will offer the truth.  If it's null you get null, if it's an empty string you get an empty string, if it's a bunch of characters you get that too, (.Value never lies), (.Text only returns text values so if it's null you'll get an empty string not null).

In your case you can probably use Change and/or After_Update but it seems that After_Update is the preferable of the two, but if I'm wrong, the above should help you to know for certain one way or the other and pick the event best suited to your needs and situation.

Best of luck,

Rick
0
 
szx248Author Commented:
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
0
 
Rick_RickardsCommented:
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
0
 
John Mc HaleForensic Computer Examiner, Analyst/Programmer & Database ArchitectCommented:
One other observation:

Why do U require a control to be both Locked and Disabled, either one will do to prevent the user from making changes, my favourite is Me.ControlName.Enabled = False

BTW: The reason your code is failing is clearly because of a cached value change to the Age control which has not had a chance to be updated before you try and Lock the control.

Forcing the control to update by making an explicit call to Control_AfterUpdate() normally does the trick when you need a quick and dirty fix.

If the Age control does'nt have one, there is no harm adding one, even if it does not contain any meaningfull code; e.g.


Private Sub Age_AfterUpdate()

End Sub


By adding a 'stub' after update event to the Age control will allow you to call it explicitely from your code thus allowing for code like:

If (Val(Nz(Me.CountMale.Text, 0)) + Val(Nz(Me.CountFemale, 0))) > 1 Then
    ' Change value of control
    Me.Age.Value = Null
    ' Force AfterUpdate event
    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
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now