0

Hi I have a excel VB Program that pulls info from a database. My code for the function in question is below. This code WAS working for years, now i am getting an error: "Error converting datatype nvarchar to bigint" Why would this happen all of a sudden and what is the fix?

Public Function GetGiftCardsRedeem(iColumn As Integer, sFrom As String, sTo As String)

    Dim rsResults As New ADODB.Recordset
    Dim sSQL As String
    Dim iRecordCount As Integer

    ' Assign the Connection object.
    rsResults.ActiveConnection = gcnnDB

    ' Extract the required records.
    sSQL = "Select Sum(dTotalAmount) as TotalAmount from GiftCardTransactions where dtCreated between '" & sFrom & " 00:00:00' and '" & sTo & " 23:59:59'"
    sSQL = sSQL & " and CONVERT(BigINT, sCCNumber) Between 800110110000 and 800110159999" '800110110000
    sSQL = sSQL & " and not sCCNumber is null and sCCNumber <> ''"


    rsResults.Open sSQL, gcnnDB, adOpenStatic, adLockReadOnly, adCmdText
    If Not rsResults.EOF Then
        'Let's Cycle through the records..
        'We need to move last and then move back first to get a correct recordCount.

        DoEvents: DoEvents

        Sheet1.Cells(MTD_FREEGIFTCARDS_QTY_ROW, iColumn) = rsResults.Fields("TotalAmount").Value
    End If
    rsResults.Close
End Function
Community
  • 1
  • 1
Shmewnix
  • 1,553
  • 10
  • 32
  • 66

2 Answers2

1

It would happen if somehow a non-numeric value was inserted into your sCCNumber field. The fix would be to find that value and fix it.

Or you could replace

and sCCNumber <> ''

with

and ISNUMERIC(sCCNumber) = 1
James Curtis
  • 784
  • 4
  • 15
0

As James has pointed out, it's likely you have invalid data in there. Perhaps an extra leading space, or the web non-breaking space, or a comma.

I would change the last line, since you need it to be 12 digits.

sSQL = sSQL & " and sCCNumber not like '%[^0-9]%' and len(sCCNumber) = 12"
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262