Solved

Treating Enter Keys as Tabs

Posted on 2001-06-13
6
228 Views
Last Modified: 2010-05-02
    My ex-DOS users insist on using either the Tab or Enter key to navigate from one field to another. No problem. I use KeyPreview, intercept the Enter keys and send a tab. It works fine, except for one sticky point:

     When I do a data-check (on the LostFocus event of a control), I issue a MsgBox error message if the data doesn't pass muster. Then I SetFocus to the control with the bad data, and all ought to be well.

     It is, unless the user responds to the MsgBox with an Enter key. Then focus goes to the control immediately after the one with the bad data. This doesn't happen, of course, when the user clicks on the OK of the MsgBox.

     It seems clear that what's happening is that I'm setting the focus OK, but then the tab that I send when I see an Enter is doing what tabs always do, moves the focus to the next control.

     What I want to do is to avoid issuing the tab when I see an Enter, if the user is responding to a MsgBox. The problem is, how do I detect that?

     I tried this in my keyboard event Sub, just before Sending a tab when I see an Enter:

     If TypeOf Screen.ActiveControl Is MsgBox Then
          Exit Sub
     End If

     Since MsgBox is not a control, I didn't have much hope for this. It didn't work.

     Does anyone have any ideas? It seems a simple problem, but I don't have a solution.
0
Comment
Question by:GebhartBob
  • 4
6 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6189045
try this:

'In general declarations:
Private IsInvalid As Boolean

'In Form_KeyPress
  If IsInvalid Then Exit Sub
  If KeyAscii = vbKeyReturn Then
    SendKeys "{TAB}"
  End If

'In the validating procedure:
  If Text1.Text <> "Whatever" Then
    MsgBox "Bad data!"
    IsInvalid = True
  Else
    IsInvalid = False
  End If




hope that helps
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6189048
actually that code wont work :-/ Try using the text box's Validate() event instead of LostFocus.
0
 
LVL 6

Accepted Solution

by:
JonFish85 earned 50 total points
ID: 6189050
ok try this code:

Option Explicit

Private Sub Form_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
    Case vbKeyReturn
      SendKeys "{TAB}"
      KeyAscii = 0
  End Select
End Sub

Private Sub Text1_LostFocus()
  If Text1.Text <> "Help" Then
    MsgBox "Invalid Data!"
    IsBad = True
    SendKeys "+{TAB}"
  End If
End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:alanwhincup
ID: 6189051
Im not entirly sure of what you want to do but a way of using the enter key to tab between controls could be like this:

Sub Text1_KeyPress (KeyAscii As Integer)
  If KeyAscii = 13 Then  ' The Enter key.
    SendKeys "{tab}"     ' Set focus to next control.
    KeyAscii = 0         ' Ignore this key.
  End If
End Sub

cheers,

Alan
0
 

Author Comment

by:GebhartBob
ID: 6189110
    Thanks, JonFish85, for the thoughts. I have changed from LostFocus to Validate, but that didn't solve the problem. What did solve it was a variation of your idea:

---In Form Variables---
Private InMsgBox As Boolean  'Available to all Subs of Form

---Data-Check Sub---
Private Sub cmbState_Validate(KeepFocus As Boolean)
   If Not SetComboBox(cmbState, cmbState.Text) Then
      InMsgBox = True
      MsgBox "Invalid state abbreviation entered: " & cmbState & vbCrLf & "Please try again."
      KeepFocus = True
   End If
End Sub

---Keyboard Handler---
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
   If KeyCode = vbKeyReturn Then
      If InMsgBox Then
         InMsgBox = False
         Exit Sub
      Else
         SendKeys "{Tab}"
      End If
   End If
End Sub

     Actually, I've cut out a bunch of stuff from the keyboard handler, leaving only the code that's relevant to this problem.

     It works fine, although I admit it's a bit of a kludge. As far as I know, there's no other way to know that a keystroke is in response to a MsgBox.

     Anyway, thanks very much for your help. I've awarded you the 50 points.
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6189156
thanks! sorry I couldnt help more :-/
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 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