Solved

Treating Enter Keys as Tabs

Posted on 2001-06-13
6
229 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

739 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