You are here:

Visual Basic/VB runtime error "13" type mismatch

Advertisement


Question
Hello-
I am trying to use functions to calculate total var cost and total cost.  My code for TVC works, but I keep getting a runtime "13" mismatch error when I included the total cost funciton.  All the data types seem to match.  What am I doing wrong?  

Option Explicit

Private Function TVC(VC As Currency, Sold As Double) As Currency
   TVC = VC * Sold
   
End Function

Private Function TC(TVC As Currency, FC As Currency) As Currency
   TC = TVC + FC
   
End Function

   
Private Sub cmdCalcTVC_Click()
   
   Dim VarCosts As Currency
   Dim FixedCosts As Currency
   Dim UnitsSold As Double
   Dim UnitPrice As Currency
  
   VarCosts = txtVC.Text
   FixedCosts = txtFC.Text
   UnitsSold = txtSold.Text
   UnitPrice = txtPrice.Text
   
   'CALL
   txtTVC.Text = TVC(VarCosts, UnitsSold)
   
   'Calculate total costs
   
   Dim TotCosts As Currency
   Dim TotVarCosts As Currency
   
   
   TotCosts = txtTC.Text (THIS IS HI-LITED for error "13")
   
   txtTC.Text = TC(TotVarCosts, FixedCosts)
   
   
   

End Sub


Answer
The best practice is to convert the data read from text boxes to numbers. One of the functions you can use is Val. So you should have the following:
  VarCosts = Val(txtVC.Text)
  FixedCosts = Val(txtFC.Text)
  UnitsSold = Val(txtSold.Text)
  UnitPrice = Val(txtPrice.Text)
  .
  .
  .
  TotCosts = Val(txtTC.Text)  

Visual Basic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Boster Sibande

Expertise

I have six years experience in Visual Basic 6 and two years in VB.Net. I am happy to answer VB, Access and ADO questions. You can also ask C# questions

Experience

Visual Basic Visual Basic.Net C#.Net

Organizations
UNDP

Education/Credentials
Currently studying MSc. in IT

©2012 About.com, a part of The New York Times Company. All rights reserved.