How to resolve Type Mismatch Error on numeric variable


New to VB Script here... I'm trying to populate a recordset with the results of a SQL query, then loop through that recordset and add up the values in the NetWt field to get a total net weight.

I'm getting a Type Mismatch error on the line in bold.  The field NetWt in my SQL database is type Numeric(8,2).  How can I resolve this error?  Or is there a better way to do this?

Thanks, in advance, for your help!

cn.Open strConnectionString

 strSQL = "SELECT * FROM dbo.ReceiptInfo WHERE Job = '54' ORDER BY CustomerID"

Set rstReceivingInfo = CreateObject("ADODB.Recordset")

'Open the recordset object executing the SQL statement and return records 
rstReceivingInfo.Open strSQL,cn

'determine whether there are any records 
If rstReceivingInfo.EOF Then 
msgbox "Recordset is empty. Check your SQL string."
Exit Function

[b]dim TotalNetWt               ' this is the variable I'm having trouble with[/b]

'if there are records then loop through them
Do While NOT rstReceivingInfo.EOF   

' this is where I'm trying to force the user of a numeric variable so I can add values
[b]TotalNetWt = CDbl(TotalNetWt) + rstReceivingInfo("NetWt")[/b]



End If

set rstReceivingInfo = nothing
set cn = nothing


Open in new window

Who is Participating?
ThomasMcA2Connect With a Mentor Commented:
1) IIRC, you need to use the "fields" method, like this:

2) Your "else" where you check for EOF is not needed. Since the Exit Function will have triggered, you can change the else to an End If. Your code will be a little cleaner that way. :)

3) Your dim clause is declaring a variant, not a double. Change that to Dim TotalNetWt as Double.

4) Your attempts at [b ]bold[/b ] inside your code are not creating bold in your post, and it makes your code confusing. (Those don't work inside code blocks)

Happy coding!

Bill PrewConnect With a Mentor Commented:
Outside of the loop, after you Dim it, assign TotalNetWt a starting value, like:

TotalNetWt = CDBL(0)

Then in the loop remove the Cdbl from the summing line you have.

monkeybiz12345Author Commented:
@ThomasMcA2, thanks for the tips.  You're right, the bold indicators look horrible.  I didn't notice it when I previewed my post . My bad...  I'd already tried declaring the variable as
Dim TotalNetWt As Double.  This results in an Expected End of Statement error.  I learned that all variables are declared as variants; hence, my problem.
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

monkeybiz12345Connect With a Mentor Author Commented:
Turns out, both of you gave me clues to arrive at the code that works.

Here it is:

dim TotalNetWt
TotalNetWt = CDBL(0)

'Loop through and total up the net weights of each pallet in the shipment
Do While NOT rstReceivingInfo.EOF  

TotalNetWt = (TotalNetWt + CDBL(rstReceivingInfo.Fields("NetWt").Value))
I learned that all variables are declared as variants;

They are declared as variants when you leave off the "as something." But you don't have to use variants.
jkaiosIT DirectorCommented:
This question might have been answered, but I just wanted to make it clear that ALL of the following 4 different assignments will achieve the exact same result:

result = rstReceivingInfo("NetWt")
result = rstReceivingInfo("NetWt").Value
result = rstReceivingInfo.Fields("NetWt")
result = rstReceivingInfo.Fields("NetWt").Vaue

Open in new window

That's because the Fields object/collection is the default property of the ADODB.Recordset object, and that the Value property is the default property attribute of the Fields object.
Anthony PerkinsCommented:
The question you should be asking yourself is why in the world are you looping in the first place?  Or is this just an exercise using VBScript?  If not I suspect you may find this a lot easier on the eyes and perform a tad better:
cn.Open strConnectionString

strSQL = "SELECT SUM(NetWt) TotalNetWt FROM dbo.ReceiptInfo WHERE Job = '54'"

Set rstReceivingInfo = CreateObject("ADODB.Recordset")

'Open the recordset object executing the SQL statement and return records 
rstReceivingInfo.Open strSQL, cn
TotalNetWt = rstReceivingInfo.Fields("TotalNetWt").Value
set rstReceivingInfo = Nothing
set cn = Nothing

Open in new window

You can even make that code even more compact and efficient, but I will leave that as an exercise...
Anthony PerkinsCommented:

They are declared as variants when you leave off the "as something."
I suspect the author has not choice as they are using VBScript and not VB.  All you can use in VBScript are variants.
monkeybiz12345Author Commented:
Tom gave me the .Fields method which got me to .Value, the thing I really wanted and bp gave me a way to force my initial declaration to be a number with CDBL.

monkeybiz12345Author Commented:
@Anthony Perkins... yes.  After I posted my original question, I figured out the SELECT... Sum... structure, as you suggest.  

And yes, VBScript only so all data types are variants.
Anthony PerkinsCommented:
Tom gave me the .Fields method which got me to .Value
Not to beat a dead horse, but Fields and Value are actually properties of the ADO Recordset object.  Also, if I recall correctly, VB (and VBScript) support default properties so:

Is the same as:

Which should be the same as:

To be clear, I am not suggesting this syntax is a good idea.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.