Solved

Treating Enter Keys as Tabs

Posted on 2001-06-13
6
227 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now