Reselect a TextBox

P

Patrick C. Simonds

Below is my AfterUpdate code for TextBox12. Part of that code gives me a
message if an incorrect time is entered (like 2845 instead of 1845). Is
there any code that I can place after the MsgBox so that the number in
TextBox12 will be selected. My goal is to prevent them from moving on until
the correct number is entered.




Private Sub TextBox12_AfterUpdate()

On Error GoTo error


TextBox12.Value = Format(Application.Text(Replace(TextBox12.Value, ":",
""), "00\:00"), "hh:mm")

If TextBox12.Value > "" Then
If TextBox11.Value > "" Then

TextBox13.Value = (TimeValue(TextBox12.Value) -
TimeValue(TextBox11.Value)) * 24
TextBox13.Value = Format(TextBox13.Value, "0.0000")

TextBox1.Value = CDbl("0" & TextBox13.Value) + CDbl("0" &
TextBox23.Value) + CDbl("0" & TextBox33.Value) + CDbl("0" & TextBox43.Value)
+ CDbl("0" & TextBox53.Value) + CDbl("0" & TextBox63.Value) + CDbl("0" &
TextBox14.Value) + CDbl("0" & TextBox24.Value) + CDbl("0" & TextBox34.Value)
+ CDbl("0" & TextBox44.Value) + CDbl("0" & TextBox54.Value) + CDbl("0" &
TextBox64.Value)
TextBox1.Text = Format(TextBox1.Text, "0.0000")
End If
End If

'Test that to see if hours worked exceeds shift length

If TextBox12.Value > "" Then
If TextBox11.Value > "" Then

TextBox2 = TextBox4 - TextBox1

End If

If TextBox2.Value < 0 Then
TextBox2.Value = 0
GoTo Finished
End If

TextBox2 = TextBox4 - TextBox1
TextBox2.Text = Format(TextBox2.Text, "0.0000")

End If

Finished:

If TextBox12.Value = "" Then
TextBox13.Value = ""
End If

If TextBox11.Value = "" Then
TextBox13.Value = ""
End If
End

error:
MsgBox "please enter only time values"

End Sub
 
P

Patrick C. Simonds

Is there no way to get what I want without changing to the BeforeUpdate. All
I want is for the focus to return to TextBox12.
 
M

mdmackillop

It's the simplest way
e.g.
Private Sub TextBox12_BeforeUpdate(ByVal Cancel A
MSForms.ReturnBoolean)

TextBox12.Value = Format(Application.Text(Replace(TextBox12.Value, ":"
""), "00\:00"), "hh:mm")
On Error Resume Next
test = TimeValue(TextBox12.Text)
If Len(test) = 0 Then
MsgBox "Enter valid time"
TextBox12.Value = ""
Cancel = True
End If
End Sub

Private Sub TextBox12_AfterUpdate()
MsgBox TimeValue(TextBox12.Value)
End Su
 
P

Patrick C. Simonds

Ok you convinced me. Here is the code below.

Notice I added:

If TextBox11.Value = "" Then GoTo continue

just as a way to get out in case one wanted to get out without putting in a
time.


Is there anyway that if it is not a valid time (and the msgbox is triggered)
to have the number in the TextBox be selected so all the user has to do is
start entering a number, without having to manually select what is already
in the textBox? I tried using TextBox11.SetFocus but that did not work.




Private Sub TextBox11_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

Dim TestDateVariable


TestDateVariable = Format(Replace(TextBox11.Value, ":", ""), "00\:00")

If TextBox11.Value = "" Then GoTo continue

If IsDate(TestDateVariable) Then
TextBox11.Value = Format(TestDateVariable, "hh:mm")

Else
MsgBox "Sorry but that is not a correct time. Please enter a correct
time."
Cancel = True


End If

continue:

End Sub
 
M

mdmackillop

Private Sub TextBox11_BeforeUpdate(ByVal Cancel A
MSForms.ReturnBoolean

Dim TestDateVariabl

With TextBox1
If .Value = "" Then Exit Su

TestDateVariable = Format(Replace(.Value, ":", ""), "00\:00"

If IsDate(TestDateVariable) The
.Value = Format(TestDateVariable, "hh:mm"
Els
MsgBox "Sorry but that is not a correct time. Please enter a correc
time.
Cancel = Tru
.SelStart =
.SelLength = Len(.Text
End I
End Wit
End Su
 
P

Patrick C. Simonds

Thanks a lot. The code as written created errors but adding a . in front
of Value=, SelStart and SelLenength lines corrected that.
 
D

Dave Peterson

I use the _exit procedure. It also has the Cancel parm.

If you have a cancel button on the userform, you may want to make sure that the
..takefocusonclick property is set to false (either in the _initialize event or
manually changing the property).

Then if the user enters that textbox, they can still cancel by without having to
type in something that looks like a correct number.
 
M

mdmackillop

Patrick said:
Thanks a lot. The code as written created errors but adding a . i
front
of Value=, SelStart and SelLenength lines corrected that.
Hi Patrick,
This is the second time I've been told my "leading dots" are no
appearing, although I see them there. What browser are you using?
Regards
M
 
M

Mike Fogleman

I am using Outlook Express and the leading dots were not there. I connect OE
directly to news.microsoft.com.
Mke F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top