Nvarchar overflowed an int column

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 rs1.open statement. Can anyone shed some light? Why is 'int' even involved in this?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Galisteo8Author Commented:
<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.
Galisteo8Author Commented:
The mathematics-related errors have ceased. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

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.