Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Type Mismatch with calculation.

Status
Not open for further replies.

KM8888

Programmer
Nov 21, 2011
69
US
Hi all, I am new to programming and vbscript as a whole so I apologize if my question comes across as fairly obvious, or if I have posted incorrectly. I have scraped google but no answer really made sense to me. Basically I am getting a "type mismatch error" when I try and run the following code (which I have condensed for simplicity.)

Dim sNum1, sNum2, sTotal, sNum3 sOthertotal

After I declare those variables I grab the values of sNum1 and sNum2 with the gettext method. Then I say

sTotal = sNum1, sNum2

After this I want it to write this sOthertotal in another field so I do something like this

sOthertotal = sTotal - sNum3
.Sendkeys sOthertotal


So the error occurs with the sTotal - sNum3 line. I realize it has something to do with not being able to subtract a the total that was determined earlier in the script but I'm not sure how to fix it... I saw in some places that it's because strings are involved but being fairly new I'm not too familiar with a lot of that.

Any simplistic help offered would be greatly appreciated, thanks everyone!
 
sTotal = sNum1, sNum2
What is supposed to do this line of code ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
My apologies, it's supposed to say sTotal= sNum1 + (or&) sNum2
 
The problem is the code is treating your input as text and the "+" operator works on strings to concatenate them, so instead of 2 + 2 = 4 you are getting "2" + "2" = "22". No syntax error here detected by the script. The "-" operator is invalid to use on strings, hence the type mismatch when you get to that line.

Try converting your values before you add or subtract them.
Code:
sTotal = CCur(sNum1) + CCur(sNum2)
sOtherTotal = CCur(sTotal) - CCur(sNum3)

To make the script more robust you may want to test the input with the IsNumeric() function before attempting to convert it and warn the user/exit gracefully if the input cannot be converted to a number.
 
Thank you! The adding of the CCur seemed to bypass the issue I was having, much appreciated! :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top