Link to home
Start Free TrialLog in
Avatar of marku24
marku24Flag for United States of America

asked on

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
Avatar of etech0
etech0
Flag of United States of America image

Try this:

Application.SendKeys("{ESCAPE}")

Open in new window

Avatar of Jim Dettman (EE MVE)
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.
Avatar of marku24

ASKER

i get invalid use of the "Me" function
<No points wanted>

Then try something like this:
    Forms!YourFormName.Undo
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.
Avatar of marku24

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
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 marku24

ASKER

simple but worked perfectly