Link to home
Start Free TrialLog in
Avatar of inkineu
inkineu

asked on

Trapping ENTER key in keydown event

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.  
ASKER CERTIFIED SOLUTION
Avatar of 1William
1William

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 DrTech
DrTech

Try using the AfterUpdate event instead.
Avatar of inkineu

ASKER

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.
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
Avatar of inkineu

ASKER

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.  
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.
Avatar of inkineu

ASKER

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?
Well, being the user cannot type into a list box (only select), what validation can there be?
Avatar of inkineu

ASKER

Sorry, I meant combo box.
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
Avatar of inkineu

ASKER

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.
Is the combo being populated from a table, value list?
Avatar of inkineu

ASKER

I'm going to use the DLookup function.
Thanks for your help.
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