Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Treating Enter Keys as Tabs

Posted on 2001-06-13
6
Medium Priority
?
236 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 150 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses

660 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