Solved

How to resolve Type Mismatch Error on numeric variable

Posted on 2014-02-19
11
335 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
  • 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 51

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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now