adding up

jessmas
05-16-2003, 10:30 PM
I have the following code in my program. I am trying to get the total values into txtTotal. But it does not want to. Is the txtTotal_Click() correct??

It also needs to adjust itself as and when the user enters the figures.

Private Sub txtTotal_Click()

txtTotal.Text = Val(txtPlans.Text) + Val(txtPermit.Text) + _
Val(txtBuilder.Text) + Val(txtBench.Text) + _
Val(txtSand.Text) + Val(txtBobcat.Text) + _
Val(txtBlocks.Text) + Val(txtLayer.Text) + _
Val(txtRoof.Text) + Val(txtWindows.Text)

End Sub

Thanks in advance

alp0001
05-16-2003, 10:54 PM
Shouldn't there be a button to click on instead of trying to click on the text field?

Anyways, the other code seems ok to me if you have those text fields.

starbitz
05-16-2003, 10:58 PM
I suggest you put the above formula to a Sub and call this Sub to each textbox's Change Event. For Example:
Public Sub GetTotal()
txtTotal.text = Val(txtPlans.Text) + Val(txtPermit.Text) + _
Val(txtBuilder.Text) + Val(txtBench.Text) + _
Val(txtSand.Text) + Val(txtBobcat.Text) + _
Val(txtBlocks.Text) + Val(txtLayer.Text) + _
Val(txtRoof.Text) + Val(txtWindows.Text)
End Sub
Then in each textbox change event, call this Sub like:
Private Sub txtPlans_Change()
GetTotal
End Sub
...and so with other textboxes, except for txtTotal_change event :) So that its value will adjust to every entry modified.

xXxRUxXx
05-16-2003, 11:04 PM
to save all this to Total.txt file i think this will work: :D


Dim txtTotal As Long

txtTotal = FreeFile

Open App.Path & "\Total.txt" For Output As #txtTotal
Print #txtTotal, txtPlans.Text + " " + txtPermit.Text + " " + txtBuilder.Text + " " + txtBench.Text + " " + txtSand.Text + " " + txtBobcat.Text + " " + txtBlocks.Text + " " + txtLayer.Text + " " + txtRoof.Text + " " + txtWindows.Text
Close #txtTotal


if it is not what you mean post and explain! :huh: :-\

alp0001
05-16-2003, 11:07 PM
xXxRUxXx: I don't think he needs to save the results to a file...just show the results in the textbox control.

xXxRUxXx
05-16-2003, 11:10 PM
ohh ok.... starbitz's method will work fine


VB:
--------------------------------------------------------------------------------
Public Sub GetTotal()
txtTotal.text = Val(txtPlans.Text) + Val(txtPermit.Text) + _
Val(txtBuilder.Text) + Val(txtBench.Text) + _
Val(txtSand.Text) + Val(txtBobcat.Text) + _
Val(txtBlocks.Text) + Val(txtLayer.Text) + _
Val(txtRoof.Text) + Val(txtWindows.Text)
End Sub
--------------------------------------------------------------------------------


Then in each textbox change event, call this Sub like:

VB:
--------------------------------------------------------------------------------
Private Sub txtPlans_Change()
GetTotal
End Sub

alp0001
05-16-2003, 11:13 PM
It all depends on what jessmas needs. It will take less coding to simply add a button (or menu) and throw all that addition code into it. But if the data should change on the fly without user interaction, then yes using the change event for EACH textbox will be needed.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum