Solved

Treating Enter Keys as Tabs

Posted on 2001-06-13
6
230 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month4 days, 23 hours left to enroll

635 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