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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Textbox focus on a userform 1

Status
Not open for further replies.

mlocke

Programmer
Aug 15, 2006
6
US
I have a textbox that collects one or more item numbers from the user. After each entry, a subroutine is run which I call from textBox_AfterUpdate.

When the user is done entering items, I need control to shift to a commandButton which runs another routine.

I've been able to construct code that allows 1 textbox item and then shifts to the commandButton but then the user has to arrow or mouse back to the textbox to enter the next item and if they inadvertently hit Enter, the program continues and they have to start over.

I can also keep focus in the textbox by setting Cancel = True in Textbox_Exit but then I can never get out of the textbox to run the code from the command button.
Can anyone suggest how I can repeat <input item, run subroutine1> until done entering items and then move on to commandButton which runs subroutine2?

I've tried setting Multiline and EnterKeyBehavior in the textbox properties to true and I can enter multiple values in the textbox, but then sub 1 is not run.

Here are some excerpts from my code:

Private Sub TxtNum_AfterUpdate()
cmdAdd
End Sub

Private Sub TxtNum_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = True 'keeps focus here always, without this
'control goes to command button
End Sub

Private Sub cmdAdd()
<code for Sub 1>
End Sub

Private Sub cmdBuild_Click()
<Attached to cmdButton -- code for Sub 2>
End Sub

Thank you so much to anyone who has any advice.

Regards,
Marcia
 
Marcia,

Seems to me that you don't know before hand how many items the user is going to enter. With Multiline and EnterKeyBehavior set to true the focus will stay in the textbox. Therefore the user will have to either tab or mouse to the command button when they are done.
 
Yes, this is true. However, I want to call my cmdAdd subroutine on each item as I go. I don't know how to do that each time the user hits enter.

Thanks,
Marcia
 
Is there some reason you need to run the cmdAdd sub before the user is done entering the items?
 
I think so. I tried to simplify my form for asking the question but there are additional considerations.
I have another method of choosing items by clicking listbox1. If they click from the list then cmdAdd is run after each item. After cmdAdd is run, the item appears in listbox2. (Another routine allows removal from the chosen items in listbox2). CmdBuild_click is meant to be run on the items in listbox2.
The users would like to have the option of either entering the item or clicking on it. So I think I have to call cmdAdd after each item in order to make things consistent.
Sorry this is so convoluted and thanks for taking the time to discuss it with me.

Regards,
Marcia

 
Maybe I am not following this, but if you want the focus to return to the textbox why do you not just use textbox.setfocus at the end of cmdAdd?

Gerry
My paintings and sculpture
 
Gerry,

Thanks for your reply. I thought that would be a good idea too and I tried it but it did not work. The focus still goes to the command button when I enter the item.

Regards,
Marcia
 
Let me see if I have this right. You have 2 listbox's.
Listbox 1 holds selectable items.
Listbox 2 holds the selected items.

You also have a textbox that the user can type in an item which is then added to Listbox 2. Assuming the textbox is textbox1:

Code:
Private Sub TextBox1_Change()

TextBox1.EnterKeyBehavior = True

'Test for line feed (user pressed enter)
If Right(TextBox1.Value, 1) = vbLf Then
    
    ' Code to put textbox value into listbox
    
    ' Clear the Textbox value
    TextBox1.Value = ""
End If
End Sub

The code will move what was typed in the textbox to the listbox when the user presses the 'enter' key. To exit the textbox the user would have to press the tab key.
 
Yes, that's pretty much what happens except that the entry in the textbox is checked by the cmdAdd routine to make sure it is 1) in listbox1, and 2) not already selected, before it is put in listbox2. In other words, the code to put textbox value into listbox = cmdAdd()

I'll give this a try and thank you so much for taking the time to help!

Regards,
Marcia
 
Illustrative rather than rigourous solution, and assumes that you've followed CBasicAsslember's advice concerning Multiline and EnterKeyBehavior :

Private Sub TextBox1_Keydown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then ListBox1.AddItem Split(TextBox1.Text, vbCrLf)(TextBox1.CurLine)
End Sub
 
Thank you very much for the suggestions. This is now working the way I was trying to get it to.

Regards,
Marcia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top