Problem adding the values of 2 TextBoxes

P

Patrick C. Simonds

Can anyone tell me why these do not add together?

TextBox1 = TextBox13.Value + TextBox23.Value

Lets say the value of TextBox13 is 53 and the value of TextBox23 is 10.
Instead of returning 63 I get 5310.



Below is the code that generates the value of TextBox13

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



TextBox12 value is entered by the user as a time without the : such as 1843
for 6:43 pm and then converted to the correct format by the code below


Private Sub TextBox12_AfterUpdate()

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

End Sub
 
G

Gary Keramidas

sorry, don't know what I was thinking

CDbl(Me.TextBox2.Value) + CDbl(Me.TextBox1.Value)
 
P

Patrick C. Simonds

Thanks Gary

But of course my problems continue. The full line of code is below, and my
problem is that if any one of those TextBoxes is blank, I get a Type
Mismatch error. I am trying to avoid using helper cells on the worksheet.

Any ideas on how I can get it to ignore blank TextBoxes?





TextBox1.Value = CDbl(TextBox13.Value) + CDbl(TextBox23.Value) +
CDbl(TextBox33.Value) + CDbl(TextBox43.Value) + CDbl(TextBox53.Value) +
CDbl(TextBox63.Value) + CDbl(TextBox14.Value) + CDbl(TextBox24.Value) +
CDbl(TextBox34.Value) + CDbl(TextBox44.Value) + CDbl(TextBox54.Value) +
CDbl(TextBox64.Value)
 
D

Dave Peterson

TextBox1.Value = CDbl("0" & TextBox13.Value) + CDbl("0" & TextBox23.Value) + ...

But this won't help if those textboxes contain real text.

I'd check each textbox for numeric data

Dim mySum as double

mySum = 0
if isnumeric(me.textbox13.value) then
mysum = mysum + cdbl(me.textbox13.value)
end if
if isnumeric(me.textbox23.value) then
mysum = mysum + cdbl(me.textbox13.value)
end if
....
me.textbox1.value = mysum

or...

Dim iCtr as long
dim mySum as double

mysum = 0
for ictr = 13 to 63 step 10
if isnumeric(me.controls("textbox" & ictr).value) then
mysum = mysum + me.controls("textbox" & ictr).value
end if
if isnumeric(me.controls("textbox" & ictr + 1).value) then
mysum = mysum + me.controls("textbox" & ictr + 1).value
end if
next ictr
me.textbox1.value = mysum
 

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