Solved

SIMPLE FOCUS QUESTION

Posted on 2002-05-09
13
197 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

18 Experts available now in Live!

Get 1:1 Help Now