-
Nov 6th, 2017, 02:21 AM
#1
Thread Starter
Lively Member
[RESOLVED] How to do the TOTAL of more then One TextBox ?
hello, all.
i have a form, which has 33 Textboxes (Text10 To Text42). i want 32 textboxs total in last 34th Textbox (Text42).
when ever i lostfoucs from my perticular textbox, total must be update. i am using following code for all the text boxes.
Code:
Private Sub Text10_LostFocus()
TotAmt = Val(Text10.Text)
Text42.Text = TotAmt
End Sub
Private Sub Text11_LostFocus()
TotAmt = TotAmt + Val(Text11.Text)
Text42.Text = TotAmt
End Sub
Private Sub Text12_LostFocus()
TotAmt = TotAmt + Val(Text12.Text)
Text42.Text = TotAmt
End Sub
now, the problem is, when i move forward, from text10 to text11 or text11 to text12, its working fine.
but, if i am in text12 and i want to make changes in text10 then final TOTAL (text42) is not updating.
where am i doing wrong?
plz. help.
Thank you in advance.
kaushal
-
Nov 6th, 2017, 02:43 AM
#2
Re: How to do the TOTAL of more then One TextBox ?
try like, for all textboxes do a complete recount
Code:
tot = 0
for i 10 to 41
tot = tot + val(me.controls("text" & i).text)
next
text42.text = tot
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 6th, 2017, 03:00 AM
#3
Re: How to do the TOTAL of more then One TextBox ?
Hi kaushal,
or a diffrent approach...
Code:
Option Explicit
'you need:
'1 Flexgrid
'4 Textboxes name txtRe with Index 0 bis 3
'1 CommandButton name cmdAdd
Private Sub cmdAdd_Click()
Dim i As Long, j As Long
Dim myValue As Currency, myTotal As Currency
'check the textboxes
For i = 0 To txtRe.UBound
If Len(txtRe(i).Text) = 0 Then
MsgBox "somethings missing "
Exit Sub
End If
Next
With MSFlexGrid1
i = .Rows - 1
.AddItem "", i
.TextMatrix(i, 0) = i
.TextMatrix(i, 1) = txtRe(0).Text
.TextMatrix(i, 2) = txtRe(1).Text
.TextMatrix(i, 3) = txtRe(2).Text
.TextMatrix(i, 4) = Format(txtRe(3).Text, "0.00")
myValue = Val(txtRe(2).Text) * CCur(txtRe(3).Text)
.TextMatrix(i, 5) = Format(myValue, "0.00")
For j = 1 To .Rows - 2
myTotal = myTotal + CCur(.TextMatrix(j, 5))
Next
.TextMatrix(.Rows - 1, 5) = Format(myTotal, "0.00")
End With
'clear Textboxes for next input
For i = 0 To txtRe.UBound
txtRe(i).Text = ""
Next
End Sub
Private Sub Form_Load()
FlexInit
txtRe(0).Text = "Ab001"
txtRe(1).Text = "myDiscription"
txtRe(2).Text = "2"
txtRe(3).Text = "12,12"
End Sub
Private Sub FlexInit()
With MSFlexGrid1
.Rows = 2
.Clear
.Cols = 6
.TextMatrix(0, 0) = "Pos"
.TextMatrix(0, 1) = "ArtNo."
.TextMatrix(0, 2) = "Description"
.TextMatrix(0, 3) = "Quantity"
.TextMatrix(0, 4) = "Price"
.TextMatrix(0, 5) = "Amount"
.ColWidth(2) = 1800
.ColAlignment(0) = flexAlignRightCenter
.ColAlignment(1) = flexAlignRightCenter
.ColAlignment(2) = flexAlignLeftCenter
.ColAlignment(3) = flexAlignRightCenter
.ColAlignment(4) = flexAlignRightCenter
.ColAlignment(5) = flexAlignRightCenter
End With
End Sub
Private Sub txtRe_KeyPress(Index As Integer, KeyAscii As Integer)
Select Case Index
Case 0, 3, 4
'only numbers
Case Else
End Select
End Sub
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Nov 6th, 2017, 04:27 AM
#4
Thread Starter
Lively Member
Re: How to do the TOTAL of more then One TextBox ?
Thank you very much, Westconn1 and Chris for immediate help.
my work perfectly done with "westconn1" help.
chris, your suggestion is little long, but it will also helpful in my future project.
thank you,
kaushal
-
Nov 6th, 2017, 06:06 AM
#5
Re: How to do the TOTAL of more then One TextBox ?
Originally Posted by westconn1
try like, for all textboxes do a complete recount
Code:
tot = 0
for i 10 to 41
tot = tot + val(me.controls("text" & i).text)
next
text42.text = tot
Who knew?
So much for needing to use a control array
Nice ..
Spoo
-
Nov 6th, 2017, 06:41 AM
#6
Re: [RESOLVED] How to do the TOTAL of more then One TextBox ?
the upside of not using meaningful names for controls
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 6th, 2017, 08:33 AM
#7
Re: [RESOLVED] How to do the TOTAL of more then One TextBox ?
Using the Controls collection is slow and sloppy even though it works. There is also no reason to qualify it with Me, it is in the namespace of the Form already.
Val() is a poor choice and a bad habit. It is locale-blind so it will fail for many countries. Even where it works though it is also slow and sloppy.
Decide on the data type you want, then use Clng(), CInt(), CSng(), CDbl(), CCur() as appropriate. The newer, faster functions are locale-aware and generally a far better idea.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|