CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: calculations

  1. #1
    Join Date
    May 2000
    Location
    NH
    Posts
    49

    calculations

    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

  2. #2
    Join Date
    May 2001
    Posts
    155

    Re: calculations

    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


  3. #3
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: calculations

    Convert the text to double with CDbl() function. fieldc = cdbl(fielda.text) * cdbl(fieldb.text).

    -Cool Bizs

    Good Luck,
    -Cool Bizs

  4. #4
    Join Date
    May 2000
    Location
    NH
    Posts
    49

    Re: calculations

    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

  5. #5
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: calculations

    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

    Good Luck,
    -Cool Bizs

  6. #6
    Join Date
    May 2000
    Location
    NH
    Posts
    49

    Re: calculations

    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

  7. #7
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: calculations

    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

    Good Luck,
    -Cool Bizs

  8. #8
    Join Date
    Aug 2000
    Location
    Namibia
    Posts
    139

    Re: calculations

    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.


  9. #9
    Join Date
    Jan 2001
    Posts
    165

    Re: calculations

    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



  10. #10
    Join Date
    May 2000
    Location
    NH
    Posts
    49

    Re: calculations

    Thanks everybody. Both of these worked!!!!!!!!!

    phunkydude, what does the "0" do?

    cruella69

  11. #11
    Join Date
    Aug 2000
    Location
    Namibia
    Posts
    139

    Re: calculations

    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?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured