从 Excel 中仅插入几行后出现溢出错误
Overflow error after inserting only a few rows from 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
总 table 大小只有 1600 行,所以我没想到会出现溢出错误。
任何帮助,将不胜感激!
您收到溢出错误,因为其中一个变量无法处理提供给它的输入。我强烈怀疑罪魁祸首是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
我正在尝试将数据库的副本设置到 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
总 table 大小只有 1600 行,所以我没想到会出现溢出错误。 任何帮助,将不胜感激!
您收到溢出错误,因为其中一个变量无法处理提供给它的输入。我强烈怀疑罪魁祸首是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