ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SetFocus method (https://www.excelbanter.com/excel-programming/405603-setfocus-method.html)

Capt. Bangs

SetFocus method
 
I am creating a user form and am trying to include in the code behind the
form functionality that will ensure that the user has filled in all of the
required information. So I have a long "Validate Self" procedure that
depends on a series of message boxes to tell the user that various bits of
information have not been provided. Here's an example:

Else
If txtUserEmail.Value = "" Then 'if the user has not
entered a name in the text box
Dim Answer As VbMsgBoxResult
Answer = MsgBox("You have not entered your e-mail
address.", vbOKCancel)
If Answer = 2 Then
Exit Function
Else
Exit Function
txtUserEmail.SetFocus
End If
End If
End If

What happens is that the message box doesn't disappear on the first click of
any of the buttons or the "x" at the top right of the box. It takes several
clicks for the message box to decide it's okay to disappear. And then, when
it does disappear, it's not at all clear that the txtUserEmail text box is
what has the focus.

Any suggestions as to what I am doing wrong would be greatly appreciated!

Susan

SetFocus method
 
if you have a loooooong laundry list of textboxes & other controls the
macro has to check individually before it does anything, that will
take a lot of time.

i suggest you try making the checks more generic & using a for next,
if loop that will be a lot speedier. something like this..........

dim oControl as control

For Each oControl In Me.Controls
If oControl.Visible = True Then
If TypeOf oControl Is msforms.TextBox Then
If Len(oControl.Text) = 0 Then
MsgBox "You must enter all visible fields!", vbExclamation
+ vbOKOnly
oControl.SetFocus
Exit Sub
End If
End If
End If
Next oControl

obviously you could take out the visibility issue if you don't need
it. you'll see that the macro automatically sets the focus on the
offending textbox. or, as another idea, you could tag each textbox
with a name of some sort & have the messagebox use that tag in the
comment:

MsgBox "You must enter the " & ocontrol.tag & " field!"

hope it gets you started!
:)
susan




On Feb 5, 10:19*am, Capt. Bangs
wrote:
I am creating a user form and am trying to include in the code behind the
form functionality that will ensure that the user has filled in all of the
required information. *So I have a long "Validate Self" procedure that
depends on a series of message boxes to tell the user that various bits of
information have not been provided. *Here's an example:

* Else
* * * * * * * * If txtUserEmail.Value = "" Then * * * 'if the user has not
entered a name in the text box
* * * * * * * * * * Dim Answer As VbMsgBoxResult
* * * * * * * * * * Answer = MsgBox("You have not entered your e-mail
address.", vbOKCancel)
* * * * * * * * * * If Answer = 2 Then
* * * * * * * * * * * * Exit Function
* * * * * * * * * * Else
* * * * * * * * * * * * Exit Function
* * * * * * * * * * * * txtUserEmail.SetFocus
* * * * * * * * * * End If
* * * * * * * * End If
* * * * * * End If

What happens is that the message box doesn't disappear on the first click of
any of the buttons or the "x" at the top right of the box. *It takes several
clicks for the message box to decide it's okay to disappear. *And then, when
it does disappear, it's not at all clear that the txtUserEmail text box is
what has the focus.

Any suggestions as to what I am doing wrong would be greatly appreciated!



Rick Rothstein \(MVP - VB\)[_1228_]

SetFocus method
 
In your embedded Else statement, you are executing the Exit Function
statement **before** you (try to) execute the SetFocus statement... that
means the SetFocus statement is never being executed (the function is exited
before it can happen). Try reversing those two statements (everywhere you
have ordered it like that) and see if that makes your code execute the way
you expect.

Rick


"Capt. Bangs" wrote in message
...
I am creating a user form and am trying to include in the code behind the
form functionality that will ensure that the user has filled in all of the
required information. So I have a long "Validate Self" procedure that
depends on a series of message boxes to tell the user that various bits of
information have not been provided. Here's an example:

Else
If txtUserEmail.Value = "" Then 'if the user has not
entered a name in the text box
Dim Answer As VbMsgBoxResult
Answer = MsgBox("You have not entered your e-mail
address.", vbOKCancel)
If Answer = 2 Then
Exit Function
Else
Exit Function
txtUserEmail.SetFocus
End If
End If
End If

What happens is that the message box doesn't disappear on the first click
of
any of the buttons or the "x" at the top right of the box. It takes
several
clicks for the message box to decide it's okay to disappear. And then,
when
it does disappear, it's not at all clear that the txtUserEmail text box is
what has the focus.

Any suggestions as to what I am doing wrong would be greatly appreciated!



Capt. Bangs

SetFocus method
 
Rick,

Thanks. I did what you suggested. It still is requiring me to click on the
OK button twice before the message box disappears, but the cursor is
appearing in the text box that I wanted to have the focus, so that's a good
thing.

Thanks for your help!

"Rick Rothstein (MVP - VB)" wrote:

In your embedded Else statement, you are executing the Exit Function
statement **before** you (try to) execute the SetFocus statement... that
means the SetFocus statement is never being executed (the function is exited
before it can happen). Try reversing those two statements (everywhere you
have ordered it like that) and see if that makes your code execute the way
you expect.

Rick


"Capt. Bangs" wrote in message
...
I am creating a user form and am trying to include in the code behind the
form functionality that will ensure that the user has filled in all of the
required information. So I have a long "Validate Self" procedure that
depends on a series of message boxes to tell the user that various bits of
information have not been provided. Here's an example:

Else
If txtUserEmail.Value = "" Then 'if the user has not
entered a name in the text box
Dim Answer As VbMsgBoxResult
Answer = MsgBox("You have not entered your e-mail
address.", vbOKCancel)
If Answer = 2 Then
Exit Function
Else
Exit Function
txtUserEmail.SetFocus
End If
End If
End If

What happens is that the message box doesn't disappear on the first click
of
any of the buttons or the "x" at the top right of the box. It takes
several
clicks for the message box to decide it's okay to disappear. And then,
when
it does disappear, it's not at all clear that the txtUserEmail text box is
what has the focus.

Any suggestions as to what I am doing wrong would be greatly appreciated!





All times are GMT +1. The time now is 10:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com