[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

How do I suppress the character on a KeyDown event

I have a form with 2 sub forms for the purpose of entering financial data. I want the user to be able to move between the main forn and subforms by entering the forward slash character on the numeric keypad. My code works as intended, but it leaves the "/" character behind when intitiated from either of the subforms. It does not from the main form. How do I suppress the "/" from appearing in my field?

Main Form Code:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode
        Case vbKeyDivide
            Me.frmDescriptionSubform.SetFocus
    End Select
   
End Sub

(This works and does not leave the "/" character.)

Subform 1 Code:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode
        Case vbKeyDivide
            Screen.ActiveForm.[frmMetalsSubform].SetFocus
    End Select

End Sub

(This works as far a movement, but leaves the "/" character.)

Subform 2 Code:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode
        Case vbKeyDivide
            Screen.ActiveForm.[Comb08].SetFocus
    End Select

End Sub

(This works as far a movement, but leaves the "/" character.)

0
dwcummings
Asked:
dwcummings
  • 2
  • 2
1 Solution
 
Dale FyeCommented:
To suppress the key that was pressed, set the KeyCode value to 0
0
 
Dale FyeCommented:
Your code would look like:

    Select Case KeyCode
        Case vbKeyDivide
            Screen.ActiveForm.[Comb08].SetFocus
            KeyCode = 0
    End Select

Not sure why you are using a Select Case instead of an IF Then, but that is your choice.  Personally, I'd just use:

    If KeyCode = Case vbKeyDivide Then
        Screen.ActiveForm.[Comb08].SetFocus
        KeyCode = 0
    End IF
0
 
jdc1944Commented:
Not sure if this will work as i have not tried this myself, but have you set the KeyPreview property on the form to Yes?

If you do this, try the following  
Select Case KeyCode
        Case vbKeyDivide
            KeyCode = 0
            Screen.ActiveForm.[Comb08].SetFocus
End Select

Open in new window

0
 
jdc1944Commented:
damn got beaten to it, taking too long editing.  Oh well at least someone agrees with my suggestion
0
 
dwcummingsAuthor Commented:
Thanks for the help.  

I used your code, except that it doesn't work with "Case" in the if statement. I used the following:

If KeyCode = vbKeyDivide Then
    Screen.ActiveForm.[Combo8].SetFocus
    KeyCode = 0
End if

Again, thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now