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
 
Okay, new thought/idea. Would this be possibe? First, my goal as it stands for this issue. I want the cmbComments_AfterUpdate() code to run ONLY if the user hits Enter or Tab. If they hit Shift-Tab, or click on a different cell, then it will not run. Well, I have this working perfectly, except for when I update the cmbComments control and click away from the control or hit Shift-Tab, then the AfterUpdate code still runs. Anyway, here is my idea, it may be crazy, but I was hoping it would work:

Let's say I define a Public Variable in the module with the code, and then have the Else part of that If-Then statement of the cmbComments_KeyDown(...) event to set the value to a particular value which will then be used to determine whether to run the code in the cmbComments_AfterUpdate() event. What I could do is put an If Then statement in the AfterUpdate event, something to the effect of this:
Code:
Private Sub cmbComments_AfterUpdate()
   If MyPublicVariable = True Then
       Run the Code
   End If
End Sub

Well, do you think something of that nature would work, or is that off the wall altogether?

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
 
Yeah, just try it.
Don't forget the resetting of this public variable.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
IT WORKED!!! YES!! [BIGGLASSES]

Well, without further ado... Here is my code (if anyone ever finds this and wants to use it, just be sure to change the control names and such). One note, this is not the entire code, just the code related to the event I was attempting to control.. otherwise this would be an extremely long post!!
Code:
Option Compare Database
Option Explicit
[B]Public bolCommentAfterUpdateRun As [BLUE]Boolean[/BLUE][/B]

Private Sub cmbComments_[B]AfterUpdate()[/B]
     If [B]bolCommentAfterUpdateRun = [BLUE]True[/BLUE][/B] Then
     [BLUE]     ~Code I wanted run for moving to next record.[/BLUE]
     End If
     [GREEN]'Reset public variable value
     [B]bolCommentAfterUpdateRun = [BLUE]True[/BLUE][/B]
End Sub

[BLUE]`Note: I have some _BeforeUpdate() Code, but not necessary for understanding this idea here.[/BLUE]

Private Sub cmbComments_[B]KeyDown(KeyCode As Integer, Shift As Integer)[/B]
    [GREEN]'If the user hits Tab or Enter, then run the 
    'cmbComments_AfterUpdate event:[/GREEN]
    If KeyCode = 9 Or KeyCode = 13 Then
        If Shift = 0 Then
            Call cmbComments_AfterUpdate
        End If
    Else
        [GREEN]'This part takes care of the possibility of 
        'the user wanting to go back and change something 
        'even if he or she already updated the last field.. 
        'for example, if the person clicks out of the 
        'control to a different control, or presses Shift-
        'Tab instead of Tab.[/GREEN]
        [B]bolCommentAfterUpdateRun = [BLUE]False[/BLUE][/B]
    End If
End Sub

I've attempted to highlight/color/bold important parts of the code. I was very excited when I got this to work, and I hope that someone else can use it as well.

Thanks for your assistance on this, as well, PHV!


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
 
Are you sure you don't need the highlighted line ?
Code:
    If KeyCode = 9 Or KeyCode = 13 Then
        If Shift = 0 Then
[highlight]            bolCommentAfterUpdateRun = True[/highlight]
            Call cmbComments_AfterUpdate
        End If
    Else

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Well, it worked without it (does a boolean variable not default to True?) I went ahead and threw it in for good measure, kind of a just in case type deal (never know if I might add something else in there that uses that for something else.) [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
 
does a boolean variable not default to True
Nope, the default value is 0 (zero), ie False.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oh, well then, glad I added that line in. B/c eventually it might have messed up! Thanks for that bit of info!
Have a star, PHV! If anything, you deserve it for putting up with this long post. [SMILE]

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