Code to drop to next textbox with Enter after entry is made
I have a form with 6 variable textboxes and wish to know how to provide the coding that will move the User to the next Textbox once an entry is made in a given textbox.
How do I make the "Enter" key trigger the move, instead of the tab key.
are you really sure you want to do that? I usually strongly recommend against it. The Return key is the "OK" key (if a button has the Default property set will be activate pressing return), that means "do whatever you need to do in the form, then press Return and see the results". Think for example of the Windows log in dialog, or a log in page in a web site. You have to enter the username, press Tab, enter the password and then press return, that activates the "Login" button. It is really confusing for a user to have the same key with different action. If they get used to return to switch control, they will end up ending a transaction in a web page before they finish enter all the data.
Anyhow, you can achive that setting the KeyPreview property of you form to True and using the KeyPress event:
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
KeyAscii = 0
"There are two ways to write error-free programs. Only the third one works."
However ... sometimes there are apps that would seem to the user to function a little better if they could just press 'Enter' and go to the next 'whatever'. (After all, perhaps it is better to do what users want, rather than what we want ????)
In which case, either use Marco's 'Form_KeyPress' event, or the Keypress event of each text box. This assumes that you have the Tab order set for your text boxes. If there is some reason that you want 'Tab' to take you somewhere else (e.g. another Frame which contains various controls) then you can use 'MyNextTextBox.Setfocus' when you receive 'vbReturn'.
P.S. We still have a minor prob with users who want to Tab to each col in a dbgrid, instead of 'Enter' ('tab' taking them to the next control) - if anyone has a sol'n to this, please let me know.
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL