Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access 2002 VBA Keycode question 1

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
How can I return the Keycode of when I hit "Enter" or "Tab" or whatever other button. For now, I just want a msgBox to pop up and tell what was pressed. My ultimate goal is to say something like:
Code:
Private Sub cmbCombo1_AfterUpdate()
     If KeyCode = vbKeyTab Or vbKeyReturn Then
          ~code to update form and move to next record
     Else
          ~alternate code (or just do nothing)
     End If
End Sub

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
You can catch the keycode on the Form_KeyDown event. I currently use one in a form I have, something like this:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' Allow for paging up and down through months
    Select Case KeyCode
    Case 33, 37 ' Page up, Left Arrow
        Call labMthSub_Click
    Case 34, 39 ' Page down, Right Arrow
        Call labMthAdd_Click
    End Select
End Sub

If you want the exact keycode, try
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Debug.Print "KeyCode: " & KeyCode
End Sub
 
So, with using that, could I do something like this:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ctl as Control
' Allow for paging up and down through months
    If ctl = "cmbCombo1" Then
         If KeyCode = {code for Tab} or {code for CarriageReturn} Then
              ~code for moving to next record, etc.
         Else
              ~other code
         End IfEnd Sub
    End If
End Sub

???

Also, what is the integer value that the KeyCode returns? Is it the Hex value or the Decimal value?

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
kjv1611, I suggest you to play with the KeyDown event procedure of cmbCombo1 instead of the form.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks, PHV, I'll try that next... will post back.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Okay, here's what I did. In the control's _AfterUpdate event, I placed the code for what I want it to do. In the control's _KeyDown event, I placed this code:
Code:
Private Sub cmbCombo1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
        Call cmbCombo1_AfterUpdate
    End If    
End Sub

So, now, when I get to that control, and hit tab, enter, or do anything to try and exit out of the control, it does nothing. Any ideas? Am I supposed to be using a particular integer value for the particular key? If so, where can I find a chart of the values - I was thinking it was not the actual ASCII values, but some other value?

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Okay, after finding a database that came with my Access 2002 VBA Handbook or Enterprise Developers handbook by Sybex, and comparing that to an ASCII table on the web at I found that the codes used for KeyCode in the _KeyPress() event are the Decimal values for the keys... so, Tab is 9, and Enter(Carriage Return) is 13. Anyway, I'll try to use this instead of just using If KeyCode = vbKeyReturn or KeyCode = vbKeyTab... So, My code will be more like this:
If KeyCode = 9 or KeyCode = 13 Then
...
End If

Will post back with progress... was just about to give up..

[WINK]

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Why not add some debug in the KeyDown event procedure with something like this ?
MsgBox "You pressed Key with KeyCode=" & KeyCode
Then pressing the keys you want to trap you see their respective keycodes.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hmm... could try that as well.. thanks, will try a little more tomorrow and post back..

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Okay, back to this whole deal. This may not work, or I may be off the wall in how to make it work, but I would think I could do something like this. Any suggestions as to the format, or how to run the idea?
Code:
Private Sub cmbComments_AfterUpdate()
     ~code to run after update
End Sub

Private Sub cmbComments_BeforeUpdate()
     ~code to run before update (this is just extra stuff 
     for taking a set phrase, and allowing user to add to 
     it - works great!
End Sub

Private Sub cmbComments_KeyDown(KeyCode As Integer, Shift As Integer)
     If KeyCode = 9 or KeyCode = 13 Then
          Call cmbComments_BeforeUpdate
          Call cmbComments_AfterUpdate
     Else
          Cancel/Don't Call cmbComments_BeforeUpdate
          Cancel/Don't Call cmbComments_AfterUpdate
     End If
End Sub
Okay, of course some of this is more or less psuedo code, but it's my thoughts nonetheless. Basically, If Tab or Enter (9 or 13) is entered, then I want it to check to see if the "comment" given is in the tblComments table, and if so, ask if the user wants to add to the comment.. then run the after update event, where the record is saved, move to the next record, and set particular fields on the form equal to the previous values (fields that don't need to change very often).
Of course, If Tab or Enter (9 or 13) is not entered there, then I don't want any of the BeforeUPdate or AfterUpdate code to run, b/c that would mean that the user decided to just click on another control or something, and change the info the put there. Basically, the way I had it working before was I had the _LostFocus event calling the _AfterUpdate event, so that that would occur if nothing was entered into the cmbComments control.

Anyway, any suggestions would be greatly appreciated at this point... as to my idea, it's probability, and how to possibly make it work.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Ahh....! It works, kind of. The only thing not working now, is if someone presses Shift + Tab, it reads it as just hitting Tab.. any ideas of how to change that? I'm sure it's something simple. Here's my code now: simpler than I first thought:
Code:
Private Sub cmbComments_BeforeUpdate()
     ~code
End Sub

Private Sub cmbComments_AfterUpdate()
     ~code
End Sub

Private Sub cmbComments_KeyDown(KeyCode as Integer, Shift as Integer)
     If KeyCode = 9 Or KeyCode = 13 Then
          Call cmbComments_AfterUpdate
     End If
End Sub
Did not need to call the BeforeUpdate event, b/c there is no need for that code if it is blank - duh! on my part.. But I would like to add something in, I guess setting Shift to 1, maybe? that would not run the AfterUpdate event if someone presses Shift+Tab

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Well, tested some more, and... it's actually doing some of what I want, but not all (need something for the Shift-Tab stuff as well) But, I want it to do something like this:

Code:
Public Sub cmbComments_KeyDown(KeyCode As Integer, Shift As Integer)
     If KeyCode = 9 And KeyCode = 13 Then
          Call cmbComments_AfterUpdate
     Else
          Cancel AfterUpdate
     End If
End Sub

Is something of this nature possible?

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Cancel AfterUpdate
What is your meaning here ?
You're in a combo's KeyDown event procedure, which AfterUpdate will you cancel ?
If you don't want any other keystroke than Tab,Enter,... then simply do this:
KeyCode = 0
This will "cancel" the keystroke.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sorry... left that part out.. I want to Cancel (or in other words, don't want it to run) cmbComments_AfterUpdate if something other than Tab or Enter, regardless of whether the data in that control has been updated or not... I'll try the KetCode = 0 first, and see how that does and post back..
Thanks!


Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Well, tried changing to this:
Code:
Public Sub cmbComments_KeyDown(KeyCode As Integer, Shift As Integer)
     If KeyCode = 9 And KeyCode = 13 Then
          Call cmbComments_AfterUpdate
     Else
          KeyCode = 0
     End If
End Sub

But that's definitely not what I want, b/c it just freezes if I hit anything.. I'm going to try something else as well..

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Okay, I've got some of it working... So far, here is code:
Code:
Private Sub cmbComments_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 9 Or KeyCode = 13 Then
        If Shift = 0 Then
            Call cmbComments_AfterUpdate
        End If
    Else
End If
End Sub
( I know there is nothing in the Else part there, but I do want something there.) Is there a way to say something to the extent of Cancel all events in the Else part there, and then re-enable events after the KeyUp event possibly? Say, put something in the Else part above that says "Cancel All events", then in the KeyUp property, If Events.Cancelled = True, then Events.Cancelled = False?


Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
One other note that might help understanding here is this: part of the code in the cmbComments_AfterUpdate event is used to move to the next record. If a user wants to edit something in the current record without going to the next record, I would like the user to be able to do that, say by clicking another control with the mouse, or hitting Shift-Tab. It works fine if the cmbComments has not been changed, but if it has been changed/updated, then it automatically runs the _BeforeUpdate, then _AfterUpdate code.

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
kjv1611, are you aware that anything you put in the empty Else part will contrary text typing in your combo ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Um, no.. hmm.. can't even say I know what that is. I'll look at it Monday when I'm back at the office. If there is any good info on that you could point me to, I'd appreciate it, otherwise, I'll just check out the helpfile and Mr. Google. [WINK] Thanks for attempting to help me on yet another issue.. I can say that through posting questions here, and learning through this, I've come a long way in my understanding of VBA coding, some SQL, and Access Dbases in general. Never thought I'd learn more this way than in my previous college classes, but I'm glad I have!

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Well, color me stupid or tired the other night, June 18 - last post - it was late, and I was tired.. [SMILE].. I know what you're talking about - the Else part - um.. anyway, though...

Stephen [infinity]
"Jesus saith unto him, I am the way, the truth, and the life:
no man cometh unto the Father, but by me." John 14:6 KJV
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top