Coding error

R

Randy

I have a code (see below) that keeps receiving a run-time error (2166) that
states "you can't lock a control while it has unsaved changes"...I have in my
code a command that should save the record before it actually locks the field
but for some reason does not seem to work.

Can anyone see what might be wrong with my code that would be causing this
to happen? Here is my code:

Private Sub NoResponse_Checkbox_Click()
If Me!NoResponse_Checkbox = True Then
Me!Update_Letter_Comments.Locked = False
DoCmd.GoToControl "Update_Letter_comments"
SendKeys "{Right}", True
SendKeys "{Enter}", True
SendKeys "No response to Update Letter. Applicant has been
de-activated as of this date ", True
SendKeys Now(), True
DoCmd.Save
Me!Update_Letter_Comments.Locked = True
Me!In_Active_Reasons = 5
Call In_Active_Click
DoCmd.GoToPage 1
End If
End Sub

Any suggestions/advice will be greatly appreciated. I thank you in advance
for any assistance you may be able provide.
 
M

Marshall Barton

Randy said:
I have a code (see below) that keeps receiving a run-time error (2166) that
states "you can't lock a control while it has unsaved changes"...I have in my
code a command that should save the record before it actually locks the field
but for some reason does not seem to work.

Can anyone see what might be wrong with my code that would be causing this
to happen? Here is my code:

Private Sub NoResponse_Checkbox_Click()
If Me!NoResponse_Checkbox = True Then
Me!Update_Letter_Comments.Locked = False
DoCmd.GoToControl "Update_Letter_comments"
SendKeys "{Right}", True
SendKeys "{Enter}", True
SendKeys "No response to Update Letter. Applicant has been
de-activated as of this date ", True
SendKeys Now(), True
DoCmd.Save
Me!Update_Letter_Comments.Locked = True
Me!In_Active_Reasons = 5
Call In_Active_Click
DoCmd.GoToPage 1
End If
End Sub

Any suggestions/advice will be greatly appreciated. I thank you in advance
for any assistance you may be able provide.


First, SendKeys is notoriously buggy and should not be used.
The problems with SendKeys start with the fact that you have
no idea what running process (in Windows or Access) will
receive the key codes.

Second, DoCmd.Save doesn't save a record. Also, DoCmd is
similar to SendKeys in that, unless you can specify the
object you want to operate on. it may operate on a different
form than you expect.

You need to use Access/DAO methods instead of those mouse
simulating actions. POssile something like this air code:

Private Sub NoResponse_Checkbox_Click()
If Me!NoResponse_Checkbox = True Then
Me!Update_Letter_Comments.Locked = False
Me.Update_Letter_comments = vbCrLf _
& "No response to Update Letter. Applicant " _
& "has been de-activated as of this date " _
& Format(Now, "m/d/yyyy h:nn:ss")
Me.Dirty = False
Me!Update_Letter_Comments.Locked = True
Me!In_Active_Reasons = 5
Call In_Active_Click
DoCmd.GoToPage 1
End If
End Sub

I didn't double check it, but I don't think you even need to
unlock the text box when you do it this way.
 
R

Randy

Hi Marshall and thank you for your response...

This worked great! Thank you for the suggestions! As you can see I am a
greenie at this and just finding my way around. Should probably take some
classes! Anyhow, as stated earlier this worked great, with one
exception....The field that the text in being input into is a "memo" field
and when this code executes it wipes out all other data entered into that
field/control.

Is there a way to have the code go to the end of the memo field/control and
input the text at the end so it does not wipe out any other text that may
already be in the field?
 
M

Marshall Barton

Randy said:
This worked great! Thank you for the suggestions! As you can see I am a
greenie at this and just finding my way around. Should probably take some
classes! Anyhow, as stated earlier this worked great, with one
exception....The field that the text in being input into is a "memo" field
and when this code executes it wipes out all other data entered into that
field/control.

Is there a way to have the code go to the end of the memo field/control and
input the text at the end so it does not wipe out any other text that may
already be in the field?


Change the one line to:

Me.Update_Letter_comments = Me.Update_Letter_comments _
& vbCrLf & "No response to Update Letter. " _
& "Applicant has been de-activated as of this " _
& "date " & Format(Now, "m/d/yyyy h:nn:ss")
 

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