PC Review


Reply
Thread Tools Rate Thread

change event; return focus to original textbox

 
 
matt
Guest
Posts: n/a
 
      22nd Feb 2007
I have a user form (i.e. frmGrade) that has a number of text boxes in
it (i.e. properties window/name = txtExpDesignA, txtExpDesignB,
txtExpDesignC...). The text boxes are set to 0 (i.e. properties winow/
value = 0) and the textboxes can take on a value between 0 and 5. I'm
using an event (for when the user switches text boxes to enter
additional information) to call a function that validates whether or
not the text box value is indeed between 0 and 5. If the value is NOT
between 0 and 5 then I want the program to "reselect" the textbox that
has the error in it.

For example, assume that the first text box is txtExpDesignA and when
you hit <tab> the cursor moves to txtExpDesignB, and when you hit
<tab> again the cursor moves you to txtExpDesignC, and so on.

If I enter 6 in txtExpDesignA (which is outside of the values 0 to 5)
and hit <tab> the program will tell you that you entered an incorrect
value for txtExpDesignA. As a result, I want the cursor to stay in
txtExpDesignA and NOT move to txtExpDesignB on the change. In the
code below you will notice that I'm using
frmGrade.txtExpDesignA.SetFocus, but this does not seem to "reselect"
txtExpDesignA because the program just "tabs" to the next text box
(i.e. txtExpDesignB); however, if I use
frmGrade.txtExpDesignC.SetFocus, the program will skip over
txtExpDesignB and select txtExpDesignC just fine. Can someone clue me
in on how to "reselect" txtExpDesignA on the event.

An added question would be this: Is there a way to highlight all the
contents of the text box on using the .SetFocus method rather than
just moving the cursor? (i.e. if I do txtExpDesignC.SetFocus, the
cursor blinks at the end of the text...is there a way to have it
highlight the entire text just like the text is highlighted when you
hit <tab>?)

Thanks in advance,

Matt

Option Explicit
Dim score As Boolean

Private Sub txtExpDesignA_AfterUpdate() 'Change
score = validate5(frmGrade.txtExpDesignA.Value)
If score = False Then
MsgBox ("You input an incorrect value in ""A.""" & Chr(13) _
& "The value must be a number and <= 5.")
'frmGrade.txtExpDesignC.SetFocus
frmGrade.txtExpDesignA.SetFocus
End If
End Sub

Private Function validate5(number) As Boolean
If (IsNumeric(number) = True) And (number <= 5) And (number > 0) Then
validate5 = True
Else
validate5 = False
End If
End Function

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      22nd Feb 2007
In the exit event of the textbox, validate the entry. If it is invalid, set
the cancel property to true and the user won't exit the textbox.

--
Regards,
Tom Ogilvy


"matt" wrote:

> I have a user form (i.e. frmGrade) that has a number of text boxes in
> it (i.e. properties window/name = txtExpDesignA, txtExpDesignB,
> txtExpDesignC...). The text boxes are set to 0 (i.e. properties winow/
> value = 0) and the textboxes can take on a value between 0 and 5. I'm
> using an event (for when the user switches text boxes to enter
> additional information) to call a function that validates whether or
> not the text box value is indeed between 0 and 5. If the value is NOT
> between 0 and 5 then I want the program to "reselect" the textbox that
> has the error in it.
>
> For example, assume that the first text box is txtExpDesignA and when
> you hit <tab> the cursor moves to txtExpDesignB, and when you hit
> <tab> again the cursor moves you to txtExpDesignC, and so on.
>
> If I enter 6 in txtExpDesignA (which is outside of the values 0 to 5)
> and hit <tab> the program will tell you that you entered an incorrect
> value for txtExpDesignA. As a result, I want the cursor to stay in
> txtExpDesignA and NOT move to txtExpDesignB on the change. In the
> code below you will notice that I'm using
> frmGrade.txtExpDesignA.SetFocus, but this does not seem to "reselect"
> txtExpDesignA because the program just "tabs" to the next text box
> (i.e. txtExpDesignB); however, if I use
> frmGrade.txtExpDesignC.SetFocus, the program will skip over
> txtExpDesignB and select txtExpDesignC just fine. Can someone clue me
> in on how to "reselect" txtExpDesignA on the event.
>
> An added question would be this: Is there a way to highlight all the
> contents of the text box on using the .SetFocus method rather than
> just moving the cursor? (i.e. if I do txtExpDesignC.SetFocus, the
> cursor blinks at the end of the text...is there a way to have it
> highlight the entire text just like the text is highlighted when you
> hit <tab>?)
>
> Thanks in advance,
>
> Matt
>
> Option Explicit
> Dim score As Boolean
>
> Private Sub txtExpDesignA_AfterUpdate() 'Change
> score = validate5(frmGrade.txtExpDesignA.Value)
> If score = False Then
> MsgBox ("You input an incorrect value in ""A.""" & Chr(13) _
> & "The value must be a number and <= 5.")
> 'frmGrade.txtExpDesignC.SetFocus
> frmGrade.txtExpDesignA.SetFocus
> End If
> End Sub
>
> Private Function validate5(number) As Boolean
> If (IsNumeric(number) = True) And (number <= 5) And (number > 0) Then
> validate5 = True
> Else
> validate5 = False
> End If
> End Function
>
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      22nd Feb 2007
Matt,

The easiest way is to use the Exit event procedure, test the value of the
text box, and set Cancel equal to True if it is an invalid entry. For
example

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
If Int(.Text) < 0 Or Int(.Text) > 5 Then
.SelStart = 0
.SelLength = Len(.Text)
Cancel = True
End If
End With
End Sub

The only problem with that is that it gives the users no escape mechanism if
they want to completely cancel out of the form. I usually write a "back
door" to allow the user to exit with an invalid value, such as holding down
the SHIFT key. E.g,

Private Declare Function GetKeyState Lib "user32" ( _
ByVal nVirtKey As Long) As Integer
''''''''''''''''''''''''''''''''''''''''''
' This constant is used in a bit-wise AND
' operation with the result of GetKeyState
' to determine if the specified key is
' down.
''''''''''''''''''''''''''''''''''''''''''
Private Const KEY_MASK As Integer = &HFF80 ' decimal -128

'''''''''''''''''''''''''''''''''''''''''
' KEY CONSTANTS. Values taken
' from VC++ 6.0 WinUser.h file.
'''''''''''''''''''''''''''''''''''''''''
Private Const VK_LSHIFT = &HA0
Private Const VK_RSHIFT = &HA1
Private Const VK_LCONTROL = &HA2
Private Const VK_RCONTROL = &HA3
Private Const VK_LMENU = &HA4 ' LEFT ALT KEY
Private Const VK_RMENU = &HA5 ' RIGHT ALT KEY
'''''''''''''''''''''''''''''''''''''''''

Private Function IsShiftKeyDown() As Boolean
Dim Res As Long
Res = GetKeyState(vbKeyShift) And KEY_MASK
IsShiftKeyDown = CBool(Res)
End Function



Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With Me.TextBox1
If Int(.Text) < 0 Or Int(.Text) > 5 Then
.SelStart = 0
.SelLength = Len(.Text)
If IsShiftKeyDown() = False Then
Cancel = True
End If
End If
End With
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)




"matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a user form (i.e. frmGrade) that has a number of text boxes in
> it (i.e. properties window/name = txtExpDesignA, txtExpDesignB,
> txtExpDesignC...). The text boxes are set to 0 (i.e. properties winow/
> value = 0) and the textboxes can take on a value between 0 and 5. I'm
> using an event (for when the user switches text boxes to enter
> additional information) to call a function that validates whether or
> not the text box value is indeed between 0 and 5. If the value is NOT
> between 0 and 5 then I want the program to "reselect" the textbox that
> has the error in it.
>
> For example, assume that the first text box is txtExpDesignA and when
> you hit <tab> the cursor moves to txtExpDesignB, and when you hit
> <tab> again the cursor moves you to txtExpDesignC, and so on.
>
> If I enter 6 in txtExpDesignA (which is outside of the values 0 to 5)
> and hit <tab> the program will tell you that you entered an incorrect
> value for txtExpDesignA. As a result, I want the cursor to stay in
> txtExpDesignA and NOT move to txtExpDesignB on the change. In the
> code below you will notice that I'm using
> frmGrade.txtExpDesignA.SetFocus, but this does not seem to "reselect"
> txtExpDesignA because the program just "tabs" to the next text box
> (i.e. txtExpDesignB); however, if I use
> frmGrade.txtExpDesignC.SetFocus, the program will skip over
> txtExpDesignB and select txtExpDesignC just fine. Can someone clue me
> in on how to "reselect" txtExpDesignA on the event.
>
> An added question would be this: Is there a way to highlight all the
> contents of the text box on using the .SetFocus method rather than
> just moving the cursor? (i.e. if I do txtExpDesignC.SetFocus, the
> cursor blinks at the end of the text...is there a way to have it
> highlight the entire text just like the text is highlighted when you
> hit <tab>?)
>
> Thanks in advance,
>
> Matt
>
> Option Explicit
> Dim score As Boolean
>
> Private Sub txtExpDesignA_AfterUpdate() 'Change
> score = validate5(frmGrade.txtExpDesignA.Value)
> If score = False Then
> MsgBox ("You input an incorrect value in ""A.""" & Chr(13) _
> & "The value must be a number and <= 5.")
> 'frmGrade.txtExpDesignC.SetFocus
> frmGrade.txtExpDesignA.SetFocus
> End If
> End Sub
>
> Private Function validate5(number) As Boolean
> If (IsNumeric(number) = True) And (number <= 5) And (number > 0) Then
> validate5 = True
> Else
> validate5 = False
> End If
> End Function
>



 
Reply With Quote
 
matt
Guest
Posts: n/a
 
      26th Feb 2007
On Feb 22, 1:28 pm, "Chip Pearson" <c...@cpearson.com> wrote:
> Matt,
>
> The easiest way is to use the Exit event procedure, test the value of the
> text box, and set Cancel equal to True if it is an invalid entry. For
> example
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> With Me.TextBox1
> If Int(.Text) < 0 Or Int(.Text) > 5 Then
> .SelStart = 0
> .SelLength = Len(.Text)
> Cancel = True
> End If
> End With
> End Sub
>
> The only problem with that is that it gives the users no escape mechanism if
> they want to completely cancel out of the form. I usually write a "back
> door" to allow the user to exit with an invalid value, such as holding down
> the SHIFT key. E.g,
>
> Private Declare Function GetKeyState Lib "user32" ( _
> ByVal nVirtKey As Long) As Integer
> ''''''''''''''''''''''''''''''''''''''''''
> ' This constant is used in a bit-wise AND
> ' operation with the result of GetKeyState
> ' to determine if the specified key is
> ' down.
> ''''''''''''''''''''''''''''''''''''''''''
> Private Const KEY_MASK As Integer = &HFF80 ' decimal -128
>
> '''''''''''''''''''''''''''''''''''''''''
> ' KEY CONSTANTS. Values taken
> ' from VC++ 6.0 WinUser.h file.
> '''''''''''''''''''''''''''''''''''''''''
> Private Const VK_LSHIFT = &HA0
> Private Const VK_RSHIFT = &HA1
> Private Const VK_LCONTROL = &HA2
> Private Const VK_RCONTROL = &HA3
> Private Const VK_LMENU = &HA4 ' LEFT ALT KEY
> Private Const VK_RMENU = &HA5 ' RIGHT ALT KEY
> '''''''''''''''''''''''''''''''''''''''''
>
> Private Function IsShiftKeyDown() As Boolean
> Dim Res As Long
> Res = GetKeyState(vbKeyShift) And KEY_MASK
> IsShiftKeyDown = CBool(Res)
> End Function
>
> Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
> With Me.TextBox1
> If Int(.Text) < 0 Or Int(.Text) > 5 Then
> .SelStart = 0
> .SelLength = Len(.Text)
> If IsShiftKeyDown() = False Then
> Cancel = True
> End If
> End If
> End With
> End Sub
>
> --
> Cordially,
> Chip Pearson
> Microsoft MVP - Excel
> Pearson Software Consulting, LLCwww.cpearson.com
> (email address is on the web site)
>
> "matt" <meh2...@gmail.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> >I have a user form (i.e. frmGrade) that has a number of text boxes in
> > it (i.e. properties window/name = txtExpDesignA, txtExpDesignB,
> > txtExpDesignC...). The text boxes are set to 0 (i.e. properties winow/
> > value = 0) and the textboxes can take on a value between 0 and 5. I'm
> > using an event (for when the user switches text boxes to enter
> > additional information) to call a function that validates whether or
> > not the text box value is indeed between 0 and 5. If the value is NOT
> > between 0 and 5 then I want the program to "reselect" the textbox that
> > has the error in it.

>
> > For example, assume that the first text box is txtExpDesignA and when
> > you hit <tab> the cursor moves to txtExpDesignB, and when you hit
> > <tab> again the cursor moves you to txtExpDesignC, and so on.

>
> > If I enter 6 in txtExpDesignA (which is outside of the values 0 to 5)
> > and hit <tab> the program will tell you that you entered an incorrect
> > value for txtExpDesignA. As a result, I want the cursor to stay in
> > txtExpDesignA and NOT move to txtExpDesignB on the change. In the
> > code below you will notice that I'm using
> > frmGrade.txtExpDesignA.SetFocus, but this does not seem to "reselect"
> > txtExpDesignA because the program just "tabs" to the next text box
> > (i.e. txtExpDesignB); however, if I use
> > frmGrade.txtExpDesignC.SetFocus, the program will skip over
> > txtExpDesignB and select txtExpDesignC just fine. Can someone clue me
> > in on how to "reselect" txtExpDesignA on the event.

>
> > An added question would be this: Is there a way to highlight all the
> > contents of the text box on using the .SetFocus method rather than
> > just moving the cursor? (i.e. if I do txtExpDesignC.SetFocus, the
> > cursor blinks at the end of the text...is there a way to have it
> > highlight the entire text just like the text is highlighted when you
> > hit <tab>?)

>
> > Thanks in advance,

>
> > Matt

>
> > Option Explicit
> > Dim score As Boolean

>
> > Private Sub txtExpDesignA_AfterUpdate() 'Change
> > score = validate5(frmGrade.txtExpDesignA.Value)
> > If score = False Then
> > MsgBox ("You input an incorrect value in ""A.""" & Chr(13) _
> > & "The value must be a number and <= 5.")
> > 'frmGrade.txtExpDesignC.SetFocus
> > frmGrade.txtExpDesignA.SetFocus
> > End If
> > End Sub

>
> > Private Function validate5(number) As Boolean
> > If (IsNumeric(number) = True) And (number <= 5) And (number > 0) Then
> > validate5 = True
> > Else
> > validate5 = False
> > End If
> > End Function- Hide quoted text -

>
> - Show quoted text -


Thank you Tom and Chip for the assistance. It is very helpful.

Matt

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NotInList Event does not return to original record khel Microsoft Access 7 8th May 2009 06:31 PM
is there a got focus event for a textbox? mbosco51@hotmail.com Microsoft ASP .NET 2 28th Jun 2007 02:32 PM
Focus return to textbox on main form after opening popup =?Utf-8?B?U3RldmVu?= Microsoft Access Forms 1 10th Aug 2005 08:36 PM
set focus to textbox after click event- tried everything please please help! JC Microsoft ASP .NET 4 8th Jul 2004 12:18 AM
SP2 TextBox.Focus event problem Stephane Tombeur Microsoft Dot NET Compact Framework 8 30th Dec 2003 01:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:24 PM.