How do I accomplish the equivalent of hitting the {ESC} key in code?

  • Thread starter Bill Reed via AccessMonster.com
  • Start date
B

Bill Reed via AccessMonster.com

I have code at the end of the Before_Update event of an option group that
forces the user, by means of a MsgBox, to confirm his selection in the
option group. It works just fine, except that after canceling the update,
the record shows the little pencil up in the record selector, and the user
has to hit the {ESC} key to get rid of it. I have tried using SendKeys "
{ESC}" to no avail. Any suggestions? Here's the code:

strMsg = "You have selected " & strPhase & ". If this is correct, click
""OK""." & vbCrLf & _
"If this is not correct, click ""Cancel"" and try again"
strTitle = "Confirm Phase Selection"
iResponse = MsgBox(strMsg, vbOKCancel + vbDefaultButton2 + vbInformation,
strTitle)
If iResponse = 2 Then DoCmd.CancelEvent
SendKeys "{ESC}"
End Sub
 
R

Robert Morley

You may need to play with the optional parameter of SendKeys and use:
SendKeys "{ESC}", True 'or False, try both

You may want to try sending the ESC key a couple of times to be safe.

Also, if it's the Form update you're trying to cancel, instead try
Me.Undo 'First time cancels the field update
Me.Undo 'Second time cancels the form update



Rob
 
B

Bill Reed via AccessMonster.com

Thanks for the suggestions. I will try them out this morning. Meanwhile,
before I start another thread, maybe you can help me with my next question.

My form is incredibly slow before loading (I put a break in the on load
event and it takes forever before it even hits that code) and when going to
design of the form it takes just as long to display in design mode. I
suspect its because I am linking to a couple of mdbs on the server. If that
is the case, I could compile copies of those tables when I open my db. But
are you aware of any other reason why my form might be taking so long to
open, either in design or form view? I've been working on it for several
days now, does that have any effect on it's loading time?

Thanks,

Bill
 
J

John Nurick

Hi Bill,

When working in VBA it's always (or almost always?)) better to cancel an
event by using its Cancel argument. Normally in a BeforeUpdate event
you'd just do something along these lines:

Dim strMsg As String

strMsg = blah blah

If MsgBox(strMsg, vbYesNo+vbQuestion) <> vbYes Then
Cancel = True
End If

which would return the user to editing the control (in this case the
option group).

If you also want to terminate editing the record you'd need something
like
Me.Undo 'restore previous value of entire record
or
Me.ActiveControl.Undo 'restore previous value of option group
Me.Dirty = False 'force update of record.
 
D

Dirk Goldgar

Bill Reed via AccessMonster.com said:
Thanks for the suggestions. I will try them out this morning.
Meanwhile, before I start another thread, maybe you can help me with
my next question.

My form is incredibly slow before loading (I put a break in the on
load event and it takes forever before it even hits that code) and
when going to design of the form it takes just as long to display in
design mode. I suspect its because I am linking to a couple of mdbs
on the server. If that is the case, I could compile copies of those
tables when I open my db. But are you aware of any other reason why
my form might be taking so long to open, either in design or form
view? I've been working on it for several days now, does that have
any effect on it's loading time?

One strong possibility, if you're using Access 2000 or later, is the
operation of the Name Autocorrect "misfeature". Unless you really need
it, I suggest you turn it off: Tools -> Options... -> General tab,
uncheck "Track name AutoCorrect info". See if that makes a difference.
 
B

Bill Reed via AccessMonster.com

Thanks to all of you for your sage advice. I will try all of your
suggestions.

Bill
 

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