• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

How to resolve Type Mismatch Error on numeric variable

Greetings!

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
Else 

[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]


rstReceivingInfo.MoveNext     

Loop

End If


rstReceivingInfo.Close
set rstReceivingInfo = nothing
cn.close
set cn = nothing

...

Open in new window

0
monkeybiz12345
Asked:
monkeybiz12345
  • 4
  • 3
  • 2
  • +2
3 Solutions
 
ThomasMcA2Commented:
1) IIRC, you need to use the "fields" method, like this:
rstReceivingInfo.Fields("NetWt")

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!

Tom
0
 
Bill PrewCommented:
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.

~bp
0
 
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
monkeybiz12345Author 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))
0
 
ThomasMcA2Commented:
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.
0
 
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.
0
 
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
rstReceivingInfo.Close
set rstReceivingInfo = Nothing
cn.close
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...
0
 
Anthony PerkinsCommented:
ThomasMcA2,

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.
0
 
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.

Thanks!
0
 
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.
0
 
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:
rstReceivingInfo.Fields("TotalNetWt").Value

Is the same as:
rstReceivingInfo.Fields("TotalNetWt")

Which should be the same as:
rstReceivingInfo("TotalNetWt")

To be clear, I am not suggesting this syntax is a good idea.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now