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
Solved

SIMPLE FOCUS QUESTION

Posted on 2002-05-09
13
223 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

808 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