Google Answers Logo
View Question
 
Q: VBA/Excel SetFocus Problem ( No Answer,   1 Comment )
Question  
Subject: VBA/Excel SetFocus Problem
Category: Computers > Programming
Asked by: jpbischke-ga
List Price: $2.50
Posted: 05 Nov 2002 13:32 PST
Expires: 07 Nov 2002 14:37 PST
Question ID: 99681
I'm running into a problem getting the focus to change on a VBA
UserForm in Excel 2002.  This seems like it should be so easy but for
whatever reason I can't get it to take.  What I want to do is validate
data that a user enters into a text box.  If the data is within
acceptable parameters then the focus moves to the next text box.  If
the data is not within acceptable parameters then a message box pops
up telling the user how to correct the error.  At this point in time I
would like the focus to return to the text box (rather than going to
the next text box), essentially trapping the user there until valid
data is entered.

I'm using the BeforeUpdate event for the text box with the following
code that is executed if the input is not within the parameters:

txtInputBox.SetFocus

where txtInputBox is the name of the text box.  I've also tried using
me.txtInputBox.SetFocus but that doesn't seem to work either.  The
code is being run as I have substituted test code in its place and it
runs fine.  Regardless of what I seem to do, the focus always ends up
on the next text field.  Any ideas?

Clarification of Question by jpbischke-ga on 05 Nov 2002 18:17 PST
The cancel event does work but it is not the optimal solution for me
since it has to be used within the event itself (and cannot be used
within another subprocedure).  Setfocus would be a better option and
for the life of me I can't figure out why it doesn't work.

Request for Question Clarification by hammer-ga on 06 Nov 2002 11:07 PST
It sounds like you have a procedural problem in your code. For
example, code running after your SetFocus statement is moving the
focus to the next field. It is very difficult to troubleshoot
something like this without being able to see all the code. Is it
possible for you to post your file somewhere where I could get to it?

Clarification of Question by jpbischke-ga on 07 Nov 2002 14:37 PST
I've done a lot of research into this and discovered that many other
people are encountering difficulty with the setfocus method as well. 
Therefore, I've just decided to move on and use the Cancel property
within the BeforeUpdate event to keep the user from moving on.
Answer  
There is no answer at this time.

Comments  
Subject: Re: VBA/Excel SetFocus Problem
From: kproth-ga on 05 Nov 2002 15:39 PST
 
Have you tried canceling the BeforeUpdate event in your code? Usually
this event has an input parameter named Cancel, which, when set to
True, will cancel the rest of the events (AfterUpdate, Exit,
LostFocus, etc).

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy