从Excel插入几行后出现溢出错误

时间:2016-04-12 18:16:31

标签: sql vba excel-vba adodb excel

我试图将数据库的副本设置到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行,所以我不期待溢出错误。 任何帮助,将不胜感激!

1 个答案:

答案 0 :(得分:1)

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

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