Maybe someone can help me with this...
I have a form with, let's say, 10 text boxes on it.
The user enters numeric values into each box and the TOTAL box at the
bottom shows the total.
I want to be able to have the total at the bottom recalculate itself
anytime a text box value is changed - without the need for a Button Event
to occur.
I have been able to figure out one method to accomplish this. I used the
Sub procedure TextBox1_TextChanged. This works, but the problem is that it
updates my total after every digit is entered. For example, if I type in
"235", and my TOTAL is currently showing a value of "100", as soon as I
enter the first digit, "2", the total is incremented by "2". But, as soon
as I enter the second digit, "3", it changes the TOTAL to its origial value
of "100" + adds the new first two digits "23". And so on...
How do I get it to wait until I am done?
Thanks!
Dave
dave@carrollhome.com
J French - 27 Jul 2004 08:57 GMT
>Maybe someone can help me with this...
>
[quoted text clipped - 14 lines]
>as I enter the second digit, "3", it changes the TOTAL to its origial value
>of "100" + adds the new first two digits "23". And so on...
Do the Recalc in the LostFocus event of the Textbox
Note that clicking a Menu does not cause a LostFocus
David W. Carroll - 29 Jul 2004 03:35 GMT
Since your post I have been trying to get familiar with the LostFocus
method and am not quite there yet.
For example, I used something like:
If TextBox1.Focus = True Then
do something
End If
The "do something" part doesn't start doing anything until I begin
typing. I would like the "do something" part to start doing its thing
when the text box becomes active.
I thought Focus = True would do that, but it doesn't seem to.
Thoughts?
TIA-
Dave
>>Maybe someone can help me with this...
>>
[quoted text clipped - 19 lines]
>
> Note that clicking a Menu does not cause a LostFocus
Atreju - 29 Jul 2004 03:41 GMT
As far as I know, Focus is an Event, not a Property.
Therefore you have two options:
Text1.GotFocus
Text1.LostFocus
I am wondering if there's a way to see if it actually has focus at any
given time.
Any takers on this?
>Since your post I have been trying to get familiar with the LostFocus
>method and am not quite there yet.
[quoted text clipped - 40 lines]
>>
>> Note that clicking a Menu does not cause a LostFocus
---Atreju---
Don@home.com - 29 Jul 2004 03:57 GMT
Option Explicit
'bMyTextBoxHasFocus can be tested anywhere
'in code to see if Text1 has the focus or not
Private bMyTextBoxHasFocus As Boolean
Private Sub Text1_GotFocus()
bMyTextBoxHasFocus = True
End Sub
Private Sub Text1_LostFocus()
bMyTextBoxHasFocus = False
End Sub
>As far as I know, Focus is an Event, not a Property.
>
[quoted text clipped - 55 lines]
>
>---Atreju---
Have a good day...
Don
Atreju - 30 Jul 2004 23:41 GMT
I'm embarrased I didn't think of that, but I have an excuse - um... I
was tired! yeah.
Indeed, pretty easy just to use a boolean keeping track of it.
Duuuuh. :-)
>Option Explicit
>
[quoted text clipped - 74 lines]
>
>Don
---Atreju---
Rick Rothstein - 29 Jul 2004 05:08 GMT
> As far as I know, Focus is an Event, not a Property.
>
[quoted text clipped - 8 lines]
>
> Any takers on this?
Within any executing code, you can ask your program what control has
focus at anytime using the Screen.ActiveControl object. For example
If Screen.ActiveControl = Text1 Then
MsgBox "Text1 has the focus."
End If
You could also check this "dynamically" like this
TestForThisControl = "Text1"
If Screen.ActiveControl.Name = TestForThisForm Then
MsgBox "Text1 has the focus."
End If
The default for the Screen.ActiveControl object is the
Screen.ActiveForm. Of course, you can use Screen.ActiveForm to check
which form is hosting the control that has focus.
If Screen.ActiveForm Is Form1 Then
MsgBox "Form1 is hosting the control with focus."
End If
You can query any form (assuming you have more than one) by asking it
what its ActiveControl is.
If Form2.ActiveControl Is Text2 Then
.....etc.
Rick - MVP
Bob Butler - 29 Jul 2004 14:30 GMT
Late night Rick?
> Within any executing code, you can ask your program what control has
> focus at anytime using the Screen.ActiveControl object. For example
>
> If Screen.ActiveControl = Text1 Then
If Screen.ActiveControl IS Text1 Then
> MsgBox "Text1 has the focus."
> End If
[quoted text clipped - 3 lines]
> TestForThisControl = "Text1"
> If Screen.ActiveControl.Name = TestForThisForm Then
If Screen.ActiveControl.Name = TestForThisControl Then
> MsgBox "Text1 has the focus."
> End If
Rick Rothstein - 29 Jul 2004 16:04 GMT
> Late night Rick?
You saw my faux pas over in microsoft.public.vb.general.discussion too
then, huh?
> > Within any executing code, you can ask your program what control has
> > focus at anytime using the Screen.ActiveControl object. For example
> >
> > If Screen.ActiveControl = Text1 Then
>
> If Screen.ActiveControl IS Text1 Then
WHAP! <the sound of a hand slapping a forehead>
> > MsgBox "Text1 has the focus."
> > End If
[quoted text clipped - 5 lines]
>
> If Screen.ActiveControl.Name = TestForThisControl Then
WHAP! <the sound of a hand slapping a forehead... again>
OUCH!!! I've really got to start re-reading these quickly drafted
messages of mine. Thanks for catching those.
Rick
Atreju - 30 Jul 2004 23:42 GMT
SNIP
>OUCH!!! I've really got to start re-reading these quickly drafted
>messages of mine. Thanks for catching those.
>
>Rick
Indeed, especially because we're not just typing words here, we're
typing code. One single character is all it takes...
---Atreju---
Don@home.com - 27 Jul 2004 09:10 GMT
>Maybe someone can help me with this...
>
[quoted text clipped - 21 lines]
>Dave
>dave@carrollhome.com
One way to work around this is the Validate Event...
Such as->
Option Explicit
Private Sub Form_Load()
Dim llp As Long
'Text(5) will be used for the total...
Text1(0).Text = ""
For llp = 1 To 5
Load Text1(llp)
With Text1(llp)
.Top = Text1(llp - 1).Top + Text1(llp - 1).Height + 40
.Visible = True
End With
Next llp
With Text1(5)
.Locked = True
.TabStop = False
End With
End Sub
Private Sub Text1_Validate(Index As Integer, Cancel As Boolean)
Dim llp As Long
Dim lTotal As Long
For llp = 0 To 4
lTotal = lTotal + Val(Text1(llp))
Next llp
Text1(5).Text = CStr(lTotal)
End Sub
Have a good day...
Don
Don@home.com - 27 Jul 2004 09:26 GMT
>>Maybe someone can help me with this...
>>
[quoted text clipped - 21 lines]
>>Dave
>>dave@carrollhome.com
If you really want it to work on a KeyPress by KeyPress then this will work...
Note: Watch out for the annomolies of using Val()... ;-<
Option Explicit
Private bLoading As Boolean
Private Sub Form_Load()
Dim llp As Long
bLoading = True
'Text(5) will be used for the total...
Text1(0).Text = ""
For llp = 1 To 5
Load Text1(llp)
With Text1(llp)
.Top = Text1(llp - 1).Top + Text1(llp - 1).Height + 40
.Visible = True
End With
Next llp
With Text1(5)
.Locked = True
.TabStop = False
End With
bLoading = False
End Sub
Private Sub Text1_Change(Index As Integer)
Dim llp As Long
Dim lTotal As Long
If bLoading Then Exit Sub
For llp = 0 To 4
lTotal = lTotal + Val(Text1(llp))
Next llp
Text1(5).Text = CStr(lTotal)
End Sub
Have a good day...
Don
Atreju - 27 Jul 2004 16:32 GMT
>Maybe someone can help me with this...
>
[quoted text clipped - 21 lines]
>Dave
>dave@carrollhome.com
Dave,
First of all, what's wrong with it actually recalculating on every
keystroke?
If you want it to calc only when you're done, I'm afraid you somehow
have to tell the program you're done. You can but a button somewhere
and make its Default property to True. You then can just press ENTER
when you want a re-calc update. Otherwise you could use the LostFocus
event for the textboxes, but you would always have to tab away from
the box in order to get this to fire.
Hope one of these options is sufficient for you.
---Atreju---
Atreju - 27 Jul 2004 16:36 GMT
>Maybe someone can help me with this...
>
[quoted text clipped - 21 lines]
>Dave
>dave@carrollhome.com
Dave, just thought of one more idea.
You could have a timer control. Whenever keystrokes begin, you could
first shut off then turn back on the timer at, say, 1000 (1 second).
then when the timer event fires, re-calc, and turn off the timer. Only
turn it back on when another key is pressed. This is a bit messy, but
basically what you do is turn it off and then on again every time a
key is pressed. This way if you are typing numbers in, generally it
wouldn't take a whole second between numbers, so every keystroke would
basically reset the timer to another 1 second. once you're done
typing, that 1 second would be allowed to pass, and the recalc would
fire.
I will try to make a sample and if it works well, i will post the
code.
---Atreju---
Atreju - 27 Jul 2004 16:47 GMT
SNIP
>How do I get it to wait until I am done?
>
>Thanks!
>
>Dave
>dave@carrollhome.com
Okay, here's some code:
Make a form with as many textboxes you want, but make them all in an
array.
I have used the name txtInput(0) and so on for all the textboxes.
Add one last textbox called txtResul
Add a timer called Timer1. Set its interval to 1000
Here's form code:
(please note, I have not included any verification for numeric, I just
wanted to get you the code. please verify the text before allowing it
or calculating it. i suggest just testing the keystroke and
disallowing it if not numeric). The calc procedure here also skips a
textbox if there's nothing in it. You could also, I suppose, add
testing for numeric.
Hope this helps.
Option Explicit
Private lSum As Integer
Private Sub Timer1_Timer()
Dim iCount As Integer
lSum = 0
For iCount = 0 To txtInput.Count - 1
lSum = lSum + IIf(txtInput(iCount).Text <> "",
txtInput(iCount).Text, 0)
Next iCount
txtResul.Text = Str(lSum)
Timer1.Enabled = False
End Sub
Private Sub txtInput_KeyPress(Index As Integer, KeyAscii As Integer)
Timer1.Enabled = False
Timer1.Enabled = True
End Sub
---Atreju---
David W. Carroll - 28 Jul 2004 02:44 GMT
Thanks to all for the tips. I am trying out the different ideas as we
speak. Will let you know if I run into any snags.
Dave