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

Setfocus to a textbox fails 1

Status
Not open for further replies.

hamzajosh

Programmer
Sep 18, 2002
182
US
I have a form in which i have textboxes. When a user goes out of a textbox, i check if it is empty or has spaces and if so, want to return focus to this textbox. I have put the check in textbox_exit event. It cheks fine, gives the message but does not return focus to the textbox, focus goes to the next item in tab sequence. Any suggestions how to get it to do this would be appreciated. Here's my code

Private Sub txtCatNo_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim Temp1

Temp1 = Trim(txtCatNo)

If Temp1 = "" Then
MsgBox ("Cannot leave value blank, Please enter a valid catalog number")
txtCatNo = ""
txtCatNo.SetFocus
End If

End Sub
 
Hi

I think that the problem is that you are not cancelling the exit from the textbox.

Instead of txtCatNo.SetFocus use Cancel = true

Thinking about it logically, the routine operates when you exit from the textbox. It goes through your code and sets the focus to the textbox and when finished, if it has not been told to cancel the exit, exits from that textbox and goes to the next control.

Have a try.

Paul
 
Paul, i tried cancel=true but now the focus is set to nothing, It still doesn't go to the original textbox and now does not go to the next one too. More help would be appreciated, thanks
 
Hi,

You need to first set the focus to another control, ie

Code:
txtWhatever.SetFocus
txtCatNo.SetFocus

Sharon

PS Never seen an explanation as to why!
 
Hamjosh

I have created a userform with two text boxes and created the code below for the first box.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox1.Value = "" Then
MsgBox "Enter Text"
Cancel = True
End If
End Sub

If I try to move from the box, the message comes up and the cursor remains in the textbox.

Have you set your Tab Order on your userform? I don't know why but this might be a problem if the next control from your text box is a caption or control button - in this case, you might not be seeing the cursor.

Hope that this helps

Paul
 
hamzajosh,

When I tried Paul's suggestion, I obtained the same results; i.e. the focus remained in txtCatNo. Also, if there are no other controls that can take the focus, such as Labels, hitting the TAB key has no effect and the Exit event does not fire. TAB order does not appear to play a role.

The real problem with your approach is that if txtCatNo is blank, has the focus and the dialog is cancelled, then the Exit event does fire, presenting the nag message to the user; not what you want to have happen. I recommend that you test for valid entries only when the user clicks the OK button (or equivalent). An invalid entry can be flagged and the focus set to that TextBox easily.

Regards,
Mike
 
Or, if you do want the user to be nagged immediately, use a public variable in your userform module to signal a cancel event, e.g.

public FormCancelled as boolean

sub btnCancel_click()
FormCancelled=true
unload me
end sub

sub textbox1_exit
if not FormCancelled then
...
end if
end sub
Rob
[flowerface]
 
Hey everyone thanks for the help. I tried all the above tips but none worked other than mike's suggestion.So finally i shifted checking for valid values at the ok_click event and send focus back to the textbox if valid value was not found there. That worked, thanks everyone for you help and time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top