Solved

How to resolve Type Mismatch Error on numeric variable

Posted on 2014-02-19
11
342 Views
Last Modified: 2014-02-24
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
Comment
Question by:monkeybiz12345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 14

Accepted Solution

by:
ThomasMcA2 earned 125 total points
ID: 39871967
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
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 125 total points
ID: 39871970
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
 

Author Comment

by:monkeybiz12345
ID: 39872195
@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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Assisted Solution

by:monkeybiz12345
monkeybiz12345 earned 0 total points
ID: 39872202
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
 
LVL 14

Expert Comment

by:ThomasMcA2
ID: 39872432
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
 
LVL 12

Expert Comment

by:jkaios
ID: 39873261
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39880052
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39880054
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
 

Author Closing Comment

by:monkeybiz12345
ID: 39882064
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
 

Author Comment

by:monkeybiz12345
ID: 39882864
@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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39883476
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question