Click to See Complete Forum and Search --> : calculations


cruella69
May 29th, 2001, 02:13 PM
I have an access database with a field I describe as a decimal. USing an ADO recordset I have a form where I ask the user to enter this field. I then take this field and use it to calculat another field which I also display. Here's the problem, when the percent is entered ex (fielda as.06), the textchanged event is triggered which is where I want to do my calculation. Fieldc = fielda * fieldb. As soon as I enter the decimal point into the fielda on the entry screen, the event is triggered for the calculation but I get a type mismatch trying to do the calculation because of the decimal point. I know why it is happening but I don't know how to get around it. Any suggestions?
Thanks

cruella69

ant
May 29th, 2001, 02:18 PM
you got to take the code out of the text change event. When you enter a decimal the text is changed, and then since a decimal point can't be added or whatever, it creates the type mismatch

--ant

coolbiz
May 29th, 2001, 03:03 PM
Convert the text to double with CDbl() function. fieldc = cdbl(fielda.text) * cdbl(fieldb.text).

-Cool Bizs

cruella69
May 30th, 2001, 08:04 AM
The problem is, the fields are already numeric. I want to recalculate whenever the field changes so I am using the textchange event, but as soon as I put a decimal point in I get the type mismatch on the calculation stmt. If I don't calculate, it accepts the decimal point just fine as an entry. Is there another way to detect a change in the value of the field when entered by the user besides the textchange?

cruella69

coolbiz
May 30th, 2001, 08:16 AM
Maybe I don't understand your problem exactly. When you do the multiplication, are you operating on the recordset object itself? Or you multiply on the fly and assign it to another text box and then assign it to the recordset? Perhaps posting a little bit of your code will help.

-Cool Bizs

cruella69
May 30th, 2001, 10:21 AM
I am using an ADO recordset connected to an MS Access database. In the VB form I define the field with a data source which is the table name and the data field which is the field from the table. This is all done at design time through properties. On my form I have a field (txtdead.text)(text referring to textbox) that corresponds to the field in the table which has been described as a number in the access table. I also have another field txtfinozs.text which is the result of a calculation between 2 other fields which the user enters. I do the calc at change time becasue I want the calculated field to show on the form as well. The decimal point is accepted if it is just keyed in. It is not accepted for the calculation. The change event is triggered when the decimal is entered into the field to be used for the calculation. This is the calculation: txtfinozs.text = txtwovoz.text * txtdead.text All 3 fields are on the form and part of the recordset. All 3 fields are numeric. All 3 fields can be entered by the user without error as long as they are not used for a calculation. Hope this clarifies, but I'm not sure it does. Thanks for trying

cruella69

coolbiz
May 30th, 2001, 11:30 AM
i tried to simulate your project and it seems to work for me. I guess I must be doing something different. Wish I could help you more. Sorry.

-Cool Bizs

phunkydude
May 30th, 2001, 11:59 AM
Coolbiz is right about using the conversion function. You may get errors if your text boxes contain an empty string, why not try this
fldC = CDbl("0" & FldA.text) * CDbl("0" & FldB.text)



Should do the trick.

Kdev
May 30th, 2001, 12:57 PM
Try this in your text change event:

if IsNumeric(txtwovoz.text) and IsNumeric(txtdead.text) then
txtfinozs.text = txtwovoz.text * txtdead.text
End If


-K

cruella69
May 30th, 2001, 01:45 PM
Thanks everybody. Both of these worked!!!!!!!!!

phunkydude, what does the "0" do?

cruella69

phunkydude
May 30th, 2001, 01:59 PM
Firstly, it ensures that the CDbl functions will not get nullstrings as parameter (which was similiar to the error you were getting).
Secondly, it ensures that the text displayed in fldC will be 0 (zero) when one of the other two do not have any valid data.
Phunky hey?