473,807 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you do a SendKey for the escape button?

147 New Member
I have a form that calls up a report in the form I have several date fields that have date input masks. I have created a cancel button as:

Private Sub btnCancel_Click ()
On Error GoTo Err_btnCancel_C lick

Dim stDocName As String

DoCmd.Close

Exit_btnCancel_ Click:
Exit Sub

Err_btnCancel_C lick:
MsgBox Error.Descripti on
Resume Exit_btnCancel_ Click

End Sub

But if someone has started to enter a date in a date field and did not finish the date you get an error box and you have to press the escape key to clear the form before you can use the cancel button. I would like to have my cancel button clear the form and then close it. I thought I could do a sendkey command but I can't figure out how.

Thanks
Dan
Nov 29 '07 #1
12 19606
FishVal
2,653 Recognized Expert Specialist
Hi, Dan.

Try
Expand|Select|Wrap|Line Numbers
  1. Me.Undo
  2.  
Using SendKeys() is not reliable.

Regards,
Fish
Nov 29 '07 #2
DAHMB
147 New Member
Hi, Dan.

Try
Expand|Select|Wrap|Line Numbers
  1. Me.Undo
  2.  
Using SendKeys() is not reliable.

Regards,
Fish

Thankyou for the reply. I entered it as below, but nothing happens when I click Cancel, and I mean nothing, the button won't even get the focus.

Private Sub btnCancel_Click ()
On Error GoTo Err_btnCancel_C lick

Dim stDocName As String

Me.Undo
DoCmd.Close

Exit_btnCancel_ Click:
Exit Sub

Err_btnCancel_C lick:
MsgBox Error.Descripti on
Resume Exit_btnCancel_ Click

End Sub
Nov 29 '07 #3
FishVal
2,653 Recognized Expert Specialist
Nice.

Try to relink event procedure.
  • open the form in design view
  • open the button properties window
  • goto [Events]->[OnClick]
  • select [Event Procedure]
  • Click on [...] button
Nov 29 '07 #4
DAHMB
147 New Member
Nice.

Try to relink event procedure.
  • open the form in design view
  • open the button properties window
  • goto [Events]->[OnClick]
  • select [Event Procedure]
  • Click on [...] button

OK I did that and still nothing. However if I hit the escape key and clear the form then the cancel button works. It seem to be just the undo part that is sticking.
Nov 29 '07 #5
FishVal
2,653 Recognized Expert Specialist
OK I did that and still nothing. However if I hit the escape key and clear the form then the cancel button works. It seem to be just the undo part that is sticking.
First of all make sure the code is running.
  • go to VBA window
  • toggle breakpoint on the first line of the btnCancel_Click sub (F9)
  • click the button
  • when code execution stops on the breakpoint run the code stepwise (F8) to the end of the sub, check that Me.Undo command runs
  • clear breakpoint (F9)

And a question.
Do you try to undo changes on the form where the button is located or in a subform located on the form?
Nov 29 '07 #6
DAHMB
147 New Member
First of all make sure the code is running.
  • go to VBA window
  • toggle breakpoint on the first line of the btnCancel_Click sub (F9)
  • click the button
  • when code execution stops on the breakpoint run the code stepwise (F8) to the end of the sub, check that Me.Undo command runs
  • clear breakpoint (F9)

And a question.
Do you try to undo changes on the form where the button is located or in a subform located on the form?

OK 1st the button is on the same form as the changes I am try to make are. 2nd I tried the above and if I have any data in the date fields at all it will not work. It won't even leave the first line. It just turns brown and beeps. When I clear the form by hitting escape it will run through fine.
Nov 29 '07 #7
FishVal
2,653 Recognized Expert Specialist
2nd I tried the above and if I have any data in the date fields at all it will not work. It won't even leave the first line. It just turns brown and beeps.
What does it mean?
It gets brown when you toggle breakpoint. When you hit the button and event handler procedure runs code execution stops on a line breakpoint is toggled on. Here you can run code line-by-line by pressing F8 to check execution results of each code line.
You should take a look at Debugging in VBA tutorial if you feel difficulties with debugging VBA code.
Nov 29 '07 #8
sierra7
446 Recognized Expert Contributor
Hi D

I think that the problem lies with the fact that you are using an Input Template on the field(s) and Access is doing a low-level check before handing back to VBA and will not allow the focus to move to another control if the data input is inaccurate or incomplete. I think that if you use nice little features like the input template then you must play by Access rules and make users press the escape key manually.

I've built a demo form and can replicate your issue. I've tried putting a MsgBox in the Before Update event but Access will not even get there unless the data is compliant. Text0 is a text box with a template

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text0_BeforeUpdate(Cancel As Integer)
  2. On Error GoTo Err_Text0_beforeUpdate
  3.  
  4. If Me.Text0 > 1 Then
  5.     MsgBox "Hi"
  6. End If
  7.  
  8. Exit_Text0_BeforeUpdate:
  9.     Exit Sub
  10.  
  11. Err_Text0_beforeUpdate:
  12.     MsgBox err.Number & " - " & err.Description
  13.     Cancel = True
  14.  
  15.     Resume Exit_Text0_BeforeUpdate
  16.  
  17. End Sub
If you the Sendkeys command at line 14 this would cancel the data in Text0 (and I agree with Fish that it is often unreliable) then I would use;

Expand|Select|Wrap|Line Numbers
  1. SendKeys "{Esc}{Esc}"
I have fallen into the habit of using two 'escapes' to make sure it works but on this occasion could cancel other inputs which may be correct.

I also know that MS code is not case sensitive but in the past (when grasping at straws!) I've changed {esc} or {ESC} to upper and lower case {Esc}, although this is probably no more effective than crossing your fingers!

However, this is all acedemic because as I said above I don't think this will help you in your present problem but will watch with interest for a more expert diagnosis.

Best of luck
Nov 29 '07 #9
FishVal
2,653 Recognized Expert Specialist
.............
I think that the problem lies with the fact that you are using an Input Template on the field(s) and Access is doing a low-level check before handing back to VBA and will not allow the focus to move to another control if the data input is inaccurate or incomplete.
..............
Nice catch, Sierra.

Regards,
Fish
Nov 29 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
693
by: Cary Linkfield | last post by:
I have a standard form I use. It inherits from Windows.Forms.Form. I usually add a Cancel button the form in the designer. I want to raise the Cancel button's Click event when the user presses the escape key. Public Class myStandardForm Inherits Windows.Forms.Form Private WithEvents _btnCancel As Button Private Sub _Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
3
4627
by: Paul | last post by:
I have an Access 2000 database with a form that is giving me some major headaches. When you open the form, it displays all records and allows editing, but has AllowAdditions set to False so that the user has to use my New Record button. When you click the New Record button, the form presents a new record for editing. My client wants to use the Escape key to cancel changes to new or existing records. On existing records, Access already...
1
2410
by: Norman Fritag | last post by:
Hi there, I have one subform in a Master form, which is set to add mode only. I allow only for data entry. Sofar all words fine. All information on the main form is set for display only and have no tab stops, and all controls are looked. 1) I like to know what event is triggered, when the user presses the <esc button>, because the subform doesn't remain in addmode, it shows the first record and subsequent records?
2
9267
by: Tim Marshall | last post by:
What is the docmd.runcommand accmd? for the equivalent of pressing the escape button when adding a record to a table? I'm setting up a bound form and would like to have a "cancel" button that does this. Thanks in advance. -- Tim - http://www.ucs.mun.ca/~tmarshal/
3
24696
by: baileystuff | last post by:
I have an issue where I have three Cases that SHOULD act the same but don't. If you look at the code below you will see that the general premise is to open a MSWord template and then use SendKey commands to get it to save the file using a new name (NewFName is assigned earlier in the code). A couple of issues to keep in mind. These are MailMerge templates that are attached to a Text Deliminted File (a separate one for each Case, i.e....
8
5417
by: drose0927 | last post by:
Please help! I can't get my program to exit if the user hits the Escape button: When I tried exit(EXIT_SUCCESS), it wouldn't compile and gave me this error: Parse Error, expecting `'}'' 'else if (choice == 27) exit(0) } }' Here is my program (Simple loop to display currency equivalencies based
8
10012
by: Terry | last post by:
I have noiticed a change in behavior between VB6 and VB.Net (2003 and 2005) that I don't find documented anywhere. It has to do with 'causesvalidation' and the button on the Form defined to be the cancelbutton. According to the documentation: "The cancel button for a form is the button control that is clicked whenever the user presses the ESC key." Yet 'clicking' the button and pressing the escape key do not produce the same results. ...
0
1854
by: Gunawan | last post by:
Can someone help me how to write console application to sendkey c:>sendkey {ALT}+{TAB} simulate key ALT+TAB TIA Gun
1
3148
by: John Dio | last post by:
I am trying to create a on screen key board. I simply want to send a key (Simulate the keyboard) to a combo box using the Handle of the control. The below code is not working
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.