Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SIMPLE FOCUS QUESTION

Posted on 2002-05-09
13
Medium Priority
?
247 Views
Last Modified: 2006-11-17
Can someone please tell me what is wrong with the below code????

Private Sub txtCallerForeName_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
    KeyAsacii = vbTAB
End If
End Sub

All I want to do is is the User presses enter in a specific textbox control (txtCallerForeName) I wish to emulate the user pressing the TAB key.
This is because what I really want is to move the focus onto the next controll specifiec in the Tabindex property.

Any ideas????

Madmarlin
0
Comment
Question by:Madmarlin
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 6998814
You will have to do something like the following

Private Sub txtCallerForeName_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
   NextControl.SetFocus
End If
End Sub

mlmcc
0
 
LVL 15

Expert Comment

by:ameba
ID: 6998822
>     KeyAsacii = vbTAB
        someInteger = "         "
0
 
LVL 15

Expert Comment

by:ameba
ID: 6998850
Madmarlin,
>      KeyAsacii = vbTAB
try:
       KeyAsacii = Asc(vbTAB)
or:
       KeyAsacii = vbKeyTab
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 18

Accepted Solution

by:
mdougan earned 200 total points
ID: 6998925
If none of the above works you can always try:

Private Sub txtCallerForeName_KeyPress(KeyAscii As Integer)
   If KeyAscii = 13 Then
      KeyAsacii = 0
      SendKeys "{TAB}"
   End If
End Sub



0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 6998965
Set the Forms Key Preview Property to True

Private Sub Form_KeyPress(KeyAscii As Integer)
        'Form's KeyPreview = true inorder this function to work
        On Error Resume Next
        If KeyAscii = 13 Then
            SendKeys Chr(9)
        End If
End Sub 'Form_KeyPress(KeyAscii As Integer)
0
 
LVL 15

Expert Comment

by:ameba
ID: 6998970
mdougan is right, my code won't move the focus.
(I didn't think further from correcting "Type Mismatch" error ;-)
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6999463
There is a typo in my sample:

KeyAsacii should be KeyAscii
0
 
LVL 18

Expert Comment

by:mdougan
ID: 6999467
Actually, that typo was in Madmarlin's original code... maybe that was the problem all along <grin>
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 7000278
KeyAscii is an input parameter to the KeyPress function.  Changing its value does not change the key that was pressed.  It is used to determine what key was pressed.

The only way to move the focus is to set the focus to another control.

mlmcc
0
 
LVL 3

Expert Comment

by:n_narayanan
ID: 7000345
If you want the Tab key simulation for a specific control say text1 then,

Private Sub txtCallerForeName_KeyPress(KeyAscii As Integer)
       On Error Resume Next
       If KeyAscii = 13 Then
           SendKeys Chr(9)
       End If
End Sub

Or if you want for every control if user presses enter then it should be moved to next control then,

First set your form's keyspreview property to True in design time.


Private Sub Form_KeyPress(KeyAscii As Integer)
       'Form's KeyPreview = true inorder this function to work
       On Error Resume Next
       If KeyAscii = 13 Then
           SendKeys Chr(9)
       End If
End Sub 'Form_KeyPress(KeyAscii As Integer)

Cheers
Narayanan


0
 
LVL 18

Expert Comment

by:mdougan
ID: 7000371
mlmcc, that is not correct.

The keypress event happens BEFORE the key's ascii value is processed.  You can set the keyascii to 0 if you want to cancel the keystroke and that will change the key that was pressed.  But, I think you may be right that changing the keyascii to a asc(char(9)) may not cause a tab to occurr.
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 7000645

Well dispite you slagging off my spelling (thank god for Spellcheckers) your code was simple and effective.

I'd actually tried the sendKeys call but didnt realise that you had to but the {} around the TAB ..

Cheers Chris

PS, Thanks to all you others who proposed answers, unfortunately only one has to win the points..
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 7002753
mdougan
I'll have to check that out.  I guess I learn something everyday.

mlmcc
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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
Suggested Courses

782 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