Nvarchar overflowed an int column

Posted on 2004-11-18
Last Modified: 2012-08-13
In one of my Access form's event codes is the following:

Dim rs1 As ADODB.Recordset
Dim strCriteria1 As String
Dim strCriteria2 As String

Set rs1 = New ADODB.Recordset
strCriteria1 = Me.EbayNum
strCriteria2 = Me.Parent.OrderID

rs1.Open "SELECT * FROM dbo.[ORDER-DETAIL] WHERE dbo.[ORDER-DETAIL].EbayNum = " & strCriteria1 & " AND dbo.[ORDER-DETAIL].OrderID = " & strCriteria2, CurrentProject.Connection

Me.EbayNum refers to a control that is bound to an underlying SQL table where EbayNum is of type nvarchar(20).

The code runs when I enter an EbayNum value into a subform. As I enter subsequent EbayNums, the code runs fine -- UNLESS one of the two following things happens:

1) I enter an EbayNum that is shorter than the longest EbayNum value already in the subform
2) I enter an EbayNum that is as long as the longest EbayNum value, but is of lesser numerical value

Then, I'll get one or the other of these errors:

1)"Runtime error '-2147217833 (80040e57)': Arithmetic overflow error converting numeric to data type numeric."
2)"Runtime error '-2147217833 (80040e57)': The conversion of the nvarchar value <last value in the list> overflowed an int column. Maximum integer value exceeded."

Either error occurs on the statement. Can anyone shed some light? Why is 'int' even involved in this?

Question by:Galisteo8
    LVL 2

    Accepted Solution

    I think the reason "int" is involved is that there appears to be an implicit type conversion going on. It is taking your strings, strCriteria1 or strCriteria2, and converting it to integers because you haven't quoted the strings in your Select. First, I'd try adding quotes as follows:

    rs1.Open "SELECT * FROM dbo.[ORDER-DETAIL] WHERE dbo.[ORDER-DETAIL].EbayNum = '" & strCriteria1 & "' AND dbo.
                                                                                                                                        ^                        ^
                                                                                                                                        ' "                      ' "              
    [ORDER-DETAIL].OrderID = '" & strCriteria2 & "'", CurrentProject.Connection
                                                                        " ' "
    The need not be spaces around the quotes, but below the ^ I showed them with blanks so it would be clear about how to quote them.

    One more thing. It is not necessary to store the controls in a string and then use then in the Select; it is actually redundant. Just remember that controls act like variant variables when you use them directly. So, you could have writtten:

    rs1.Open "SELECT * FROM dbo.[ORDER-DETAIL] WHERE dbo.[ORDER-DETAIL].EbayNum = '" & Me.EbayNum & "' AND dbo.[ORDER-DETAIL].OrderID = '" & Me.Parent.OrderID & "'", CurrentProject.Connection

    LVL 8

    Author Comment

    <slapping forehead> The quotes!  I've been out of the office for a week - I KNEW I was forgetting something.

    Also, I've tended to get into the habit of using these string variables in larger, more complex events, but I agree it is not necessary here.

    Anyway, I'll give this a go tomorrow (hopefully) and report back.
    LVL 8

    Author Comment

    The mathematics-related errors have ceased. Thanks!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now