Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Trapping ENTER key in keydown event

Posted on 2003-03-19
14
Medium Priority
?
565 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
  • 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

580 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