Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to resolve Type Mismatch Error on numeric variable

Posted on 2014-02-19
11
Medium Priority
?
357 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 375 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 58

Assisted Solution

by:Bill Prew
Bill Prew earned 375 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how the fundamental information of how to create a table.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

636 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