Trigger ESC key in VBA code

I am running VBA code to insert data to a table.  At the end of the insert I need to manually click the ESC key to reset the form data.  Can I trigger this key in code?

Thanks
marku24Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

etech0Commented:
Try this:

Application.SendKeys("{ESCAPE}")

Open in new window

0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
You can do that with SendKeys, but its always best to stay away from SendKey's if possible.

SendKey's goes to the current window, which if your user happens to click else where, may end up getting sent to another window.

Instead, if your trying to undo a form, use the undo method:

Me.Undo

In general, you will find that there are very few things you really need send keys for within Access.

Almost every operation is a method of an existing object.

Jim.
0
marku24Author Commented:
i get invalid use of the "Me" function
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Jeffrey CoachmanMIS LiasonCommented:
<No points wanted>

Then try something like this:
    Forms!YourFormName.Undo
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
You only can use "Me" if the code is in the forms module.   If your doing this in a standard module, you'll need to pass in a form reference or a form name as a string so you can reference it and call the undo method.

For example:

Call MyRoutine([Form])   or Call MyRoutine([Me])


Sub MyRoutine(frm as Form)

   ' Do your process

   ' Reset form
   frm.Undo

End Sub

or with the form name as a string:

Call MyRoutine(Me.Name) or Call MyRoutine("MyFormNameIsThis")

Sub MyRoutine(strFormName as string)

   ' Do your process

   ' Reset form
   Forms(strFormName).Undo

End Sub

  On the last, if your going to do multiple things with form, your better off to set an object reference:


Sub MyRoutine(strFormName as string)

    Dim frm As Form

    Set frm = Forms(strFormName)

   ' Do your process

   ' Do something to the form
   frm.Visible = False

   ' Reset the form
    frm.Undo

    ' Make it visible and repaint
    frm.Visible = True
    frm.Repaint

End Sub

Jim.
0
marku24Author Commented:
i think I may get it to work by going to a new record.  The reason the ESC works is it resets my Autonumber.  A new record would do the same.  Can I add a new record somehow by :

myform goto newrecord?

something like that?  This may be easier.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Sure you can go to the new record, but the requirement is still basically the same; as long as this code runs in a standard module, you need to specify which form it is that needs to go to the new record.

  So the way to make it easier would be to move this code into the forms module.  Then you could use the "Me" keyword.

Jim.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marku24Author Commented:
simple but worked perfectly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.