Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting focus to a control after hitting ENTER

Status
Not open for further replies.

kkitt

Programmer
Dec 1, 2002
122
US
I have a form with the following controls on it. 1 List Box that contains Ship From Locations, 1 Option box that allows me to select shipment type, and two Text Fields. In the first Text Field I enter a Zip Code and on the After Update I populate the second Text field with some text after performing a DB Query.

Both the List and Option controls are also updating the second Text Field if they are updated.

The Issue I am having is once I enter the ZipCode in the Text field and hit ENTER, I cannot reset the focus back to this control, it move on to the next. If I update any of the other Controls the Focus can be set to the Text Field.

How do I get the setfocus to move back to a control if the ENTER key has been pressed.

Thanks
 
Look at the keyup and keypress events of your textbox, also if you want it to not do something on the enter turning the Multiline stye option will stop it moving out of the textbox also.

HTH


Rob
 
You can also try setting
Code:
Private Sub Text1_Validate(Cancel As Boolean)
   Cancel = True
End Sub
in the text boxes validate event. This is the last event that fires and by cancelling it you are telling VB not to leave the text box and it should retain focus.

zemp
 
would something like this work...

Private Sub txtZipCode_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13 'Enter Key
txtCity = "Load data from DB query here."
' txtZipCode.SetFocus 'Not sure if this is available
txtZipCode.SelStart = 0
txtZipCode.SelLength = Len(txtZipCode)
End Select
End Sub

DarkMain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top