【问题标题】:Overflow error after inserting only a few rows from Excel仅从 Excel 插入几行后出现溢出错误
【发布时间】:2016-04-12 18:16:31
【问题描述】:

我正在尝试将数据库的副本设置到 Excel 文档中,以便销售代表在现场时可以更新该文档。我已经为列表行运行了插入语句的代码,但仅在 20 行之后出现了溢出错误。这是我的代码:

Sub ConnectTODB()

  Dim CustomersConn As ADODB.Connection
  Dim CustomersCmd As ADODB.Command
  Dim lo As Excel.ListObject
  Dim ws As Excel.Worksheet
  Dim lr As Excel.ListRow

  Set ws = ThisWorkbook.Worksheets(8)
  Set lo = ws.ListObjects("TCustomers")

  Set CustomersConn = New ADODB.Connection
  Set CustomersCmd = New ADODB.Command

  CustomersConn.ConnectionString = SQLConStr
  CustomersConn.Open
  CustomersCmd.ActiveConnection = CustomersConn

  For Each lr In lo.ListRows
    CustomersCmd.CommandText = _
    GetInsertText( _
    Intersect(lr.Range, lo.ListColumns("Type").Range).Value, _
    Intersect(lr.Range, lo.ListColumns("Customer").Range).Value, _
    Intersect(lr.Range, lo.ListColumns("Name").Range).Value, _
    Intersect(lr.Range, lo.ListColumns("Contact").Range).Value, _
    Intersect(lr.Range, lo.ListColumns("Email").Range).Value, _
    Intersect(lr.Range, lo.ListColumns("Phone").Range).Value, _
    Intersect(lr.Range, lo.ListColumns("Corp").Range).Value)

    CustomersCmd.Execute
  Next lr

  CustomersConn.Close

  Set CustomersConn = Nothing

End Sub

Function GetInsertText(CType As String, Customer As Integer, Name As String, Contact As String, Email As String, Phone As String, Corp As String) As String
  Dim SQLStr As String

  SQLStr = _
  "INSERT INTO Customer (" & _
  "Type, Customer,Name,Contact,Email,Phone,Corp)" & _
  "VALUES (" & _
  "'" & CType & "'," & _
  "'" & Customer & "'," & _
  "'" & Name & "'," & _
  "'" & Contact & "'," & _
  "'" & Email & "'," & _
  "'" & Phone & "'," & _
  "'" & Corp & "')"

  GetInsertText = SQLStr

End Function

总表大小只有 1600 行,所以我没想到会出现溢出错误。 任何帮助,将不胜感激!

【问题讨论】:

    标签: sql vba excel adodb


    【解决方案1】:

    您收到溢出错误,因为其中一个变量无法处理给它的输入。我强烈怀疑罪魁祸首是Customer As Integer in

    Function GetInsertText(CType As String, Customer As Integer, Name As String, Contact As String, Email As String, Phone As String, Corp As String) As String

    Customer As Integer 更改为Customer As Long

    【讨论】:

    • Ints 不是在后台自动悄悄地变形为 Longs 吗?
    • @StormsEdge: Interesting Read ;)
    • 正确你是悉达多!当我向我的一位同事大声解释代码时,我偶然发现了它,当我看到你的评论时,它被证实了!谢谢! :)
    • 谢谢@siddharth!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多