Solved

SIMPLE FOCUS QUESTION

Posted on 2002-05-09
13
217 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 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

772 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