By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

How do you do a SendKey for the escape button?

100+
P: 147
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_Click

Dim stDocName As String

DoCmd.Close

Exit_btnCancel_Click:
Exit Sub

Err_btnCancel_Click:
MsgBox Error.Description
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
Share this Question
Share on Google+
12 Replies


FishVal
Expert 2.5K+
P: 2,653
Hi, Dan.

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

Regards,
Fish
Nov 29 '07 #2

100+
P: 147
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_Click

Dim stDocName As String

Me.Undo
DoCmd.Close

Exit_btnCancel_Click:
Exit Sub

Err_btnCancel_Click:
MsgBox Error.Description
Resume Exit_btnCancel_Click

End Sub
Nov 29 '07 #3

FishVal
Expert 2.5K+
P: 2,653
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

100+
P: 147
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
Expert 2.5K+
P: 2,653
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

100+
P: 147
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
Expert 2.5K+
P: 2,653
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

Expert 100+
P: 446
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
Expert 2.5K+
P: 2,653
.............
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

100+
P: 147
Hi thanks Sierra I will try your suggestion and also hope someone else can add imput.

Dan
Dec 1 '07 #11

Expert 100+
P: 446
Hi thanks Sierra I will try your suggestion and also hope someone else can add imput.

Dan
Sorry that I did not offer a solution other than to say 'train your users'

If you wanted the response you indicated I think you will have to dispense with the template method and check the data in code (Before_Update) This way you have total control in VBA, can send meaningful messages and take what action you find appropriate.

However, if your code is not 'soldier proof' you can end up with duff data.

I think you said you were entering Date fields. I have always found that if a control was formatted for a date Access will only allow a valid date (i.e disallow 31st February which an input template may not do). I would alway format a date AFTER INPUT before using it because I am in the UK and don't want dates interpreted as USA format.

Best of luck
Dec 2 '07 #12

MMcCarthy
Expert Mod 10K+
P: 14,534
Can I suggest that the solution to your problem is to use a calendar control and lock the date fields so the user can't manually enter a date. This way you don't have to use input masks and your cancel button should work fine.

The MS ActiveX calendar control can cause problems but there is a very efficient calendar control available for download on Allen Brownes site.
Dec 11 '07 #13

Post your reply

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