Visual Basic/VB runtime error "13" type mismatch
Expert: Boster Sibande - 11/17/2006
QuestionHello-
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
AnswerThe 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)