?
Solved

Trapping ENTER key in keydown event

Posted on 2003-03-19
14
Medium Priority
?
558 Views
Last Modified: 2008-03-10
I'm fairly new to working with Access 2000 and have run into the following problem.  I have a SUBFORM with a number of controls (Date, listbox, text).  For speeding up Data entry, I have allowed the use of the ENTER key on each control.  I test for this in the KEYDOWN event for that control.  If the ENTER key was pressed, I call a subroutine that does some field edits, sets visible = false for the active subform and visible = true for the next data entry subform.  My problem is that ACCESS field validation seems to occur after the KEYDOWN event and I therefore receive an error when I try to set the focus to the next subform.  Is there a way to have ACCESS do the field validations from within or prior to the KEYDOWN event?  I would cancel the call to my subroutine if there were any errors.  Current code follows:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyReturn Then
      cbExit.SetFocus <-- required to pickup changed data in control  ** ERROR occurs here **
      ProcessEntry
      KeyCode = 0
   ElseIf KeyCode = vbKeyF3 Then
      cbExit.SetFocus
      KeyCode = 0
      cbExit_Click
   End If
End Sub

Any help would be appreciated.  Thanks.  
0
Comment
Question by:inkineu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 18

Accepted Solution

by:
1William earned 300 total points
ID: 8166033
Instead of doing field validatation in the table, you ought to do it in the form.  Try it and you'll find your troubles disappear!
0
 
LVL 6

Expert Comment

by:DrTech
ID: 8166913
Try using the AfterUpdate event instead.
0
 
LVL 1

Author Comment

by:inkineu
ID: 8167539
re suggestion: Field validation in the Form.  Would this mean removing the Format property from all Controls (date format = blank, listbox setting LIMIT to List = No)?

re suggestion: Use AfterUpdate event.  How would I trap the ENTER key in the AfterUpdate event?

Forgot to mention that the controls are all Temporary fields (Control Source property = blank) and do not exist in a Table.  I manipulate the data entered and then update a number of Tables using code.  

Thanks again.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:1William
ID: 8167578
No, it means on the after update event/lost focus (which would occur upon pressing the enter key, to have some code to confirm/validate the data
0
 
LVL 1

Author Comment

by:inkineu
ID: 8167995
The KeyDown event occurs before the AfterUpdate event.  I need to move the focus to another control so that the value in the current control is activated (found this out the hard way).  In the KeyDown event I come up with the error:
Run-time error 2110: cannot move focus to the control cbExit.
This happens when I enter an invalid Date in a control where the Format is set to Short Date.  Seems to me that at this point Access knows there's an invalid date but gives control to the KeyDown event.  Hope this makes sense.
If I remove the FORMAT Short Date and instead do my own validation using IsDate, it works.  
0
 
LVL 18

Expert Comment

by:1William
ID: 8168043
It does make sense.  I had a suspicion that you problem was what you had described.  Not duplicating it I could not be positive, but I was sure that the issue was when the data was being validated.  You may want to use an input mask on the date field, at least the user will have to enter some sort of a date, might reduce the number of times your validatation rags on the user.
0
 
LVL 1

Author Comment

by:inkineu
ID: 8168108
Shall do.  One last thing, what's the best thing to use for my listbox validation, as I will need to remove the NOTINLIST event?
0
 
LVL 18

Expert Comment

by:1William
ID: 8168151
Well, being the user cannot type into a list box (only select), what validation can there be?
0
 
LVL 1

Author Comment

by:inkineu
ID: 8168178
Sorry, I meant combo box.
0
 
LVL 18

Expert Comment

by:1William
ID: 8168263
Access will do it for you.  If you need to validate (is, a value that is in the combo but not correct for other choices the user has made), do it in the after update event
0
 
LVL 1

Author Comment

by:inkineu
ID: 8168289
The problem is the same as the Date problem.  Same error message.  I need to set the limit to list = No and do my own validation.
0
 
LVL 18

Expert Comment

by:1William
ID: 8168310
Is the combo being populated from a table, value list?
0
 
LVL 1

Author Comment

by:inkineu
ID: 8171393
I'm going to use the DLookup function.
Thanks for your help.
0
 
LVL 18

Expert Comment

by:1William
ID: 8173172
I had a few thoughts on your combo NotInList-Data validatation:

Private Sub Combo22_NotInList(NewData As String, Response As Integer)

    If MsgBox("Hey, wadda ya doin'?", 37) = 2 Then   ' Not going to select a value at this time
        Me.Combo22.Undo             ' Clear the value
        SendKeys "{TAB}"            ' Go to the next control in the tab order
    Else                            ' They are going to pick a choice from the list
        MsgBox "Like, you must enter or select a valid choice from this list!", vbExclamation
    End If
   
    Response = acDataErrContinue
   
End Sub
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question