Solved

SIMPLE FOCUS QUESTION

Posted on 2002-05-09
13
231 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
[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
  • 3
  • 3
  • +2
13 Comments
 
LVL 100

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
MS Dynamics Made Instantly Simpler

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

 
LVL 18

Accepted Solution

by:
mdougan earned 50 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 100

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 100

Expert Comment

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

mlmcc
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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 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…

737 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