溢出错误(在sql数据库中插入数据的VBA、excel)
这是我在excel表格中的代码溢出错误(在sql数据库中插入数据的VBA、excel),sql,vba,excel,Sql,Vba,Excel,这是我在excel表格中的代码 Private Sub btnUpdate_Click() On Error GoTo errH Dim con As New ADODB.Connection Dim rs As New ADODB.Recordset Dim strPath As String Dim intImportRow As Integer Dim strFirstName, strLastName As String Dim se
Private Sub btnUpdate_Click()
On Error GoTo errH
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strPath As String
Dim intImportRow As Integer
Dim strFirstName, strLastName As String
Dim server, username, password, table, database As String
With Sheets("Settings")
server = .txtServer.Text
table = .txtTable.Text
database = .txtDatabase.Text
' server = "LB-HO-NAYEF\MYMSSQLSERVER"
' table = "tblLSItems"
' database = "LMStock"
If con.State <> 1 Then
con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;"
'con.Open
End If
'this is the TRUSTED connection string
Set rs.ActiveConnection = con
'delete all records first if checkbox checked
If .cbDelete Then
con.Execute "delete " & table & ""
End If
'set first row with records to import
'you could also just loop thru a range if you want.
intImportRow = 2
Do Until Sheet1.Cells(intImportRow, 1) = ""
Dim strItemCode As String
Dim strScanCode As String
Dim strStyle As String
Dim strDescription As String
Dim strPrice As String
Dim strSalePrice As String
strItemCode = Sheet1.Cells(intImportRow, 2)
strScanCode = Sheet1.Cells(intImportRow, 3)
strStyle = Sheet1.Cells(intImportRow, 4)
strDescription = Sheet1.Cells(intImportRow, 5)
strPrice = Sheet1.Cells(intImportRow, 6)
strSalePrice = Sheet1.Cells(intImportRow, 7)
'insert row into database
COMMANDSTRING = "insert into tblLSItems (ItemCode, ScanCode,Style,Description,Price,SalePrice) values ('" & strItemCode & "','" & strScanCode & "','" & strStyle & "', '" & strDescription & "','" & strPrice & "','" & strSalePrice & "')"
' con.Execute "insert into tblLSItems (ItemCode, ScanCode,Style,Description,Price,SalePrice) values ('" & strItemCode & "','" & strScanCode & "','" & strStyle & "', '" & strDescription & "'," & strPrice & "," & strSalePrice & ")"
con.Execute COMMANDSTRING
intImportRow = intImportRow + 1
Loop
MsgBox ("Done importing")
con.Close
Set con = Nothing
End With
Exit Sub
errH:
MsgBox Err.Description
MsgBox (COMMANDSTRING)
End Sub
Private Sub btnUpdate_Click()
关于错误转到错误
Dim con作为新的ADODB连接
将rs设置为新ADODB.Recordset
将strPath设置为字符串
Dim intImportRow作为整数
Dim strFirstName,strLastName作为字符串
Dim服务器、用户名、密码、表、数据库作为字符串
带图纸(“设置”)
服务器=.txtServer.Text
table=.txtTable.Text
数据库=.txtDatabase.Text
'server=“LB-HO-NAYEF\MYMSSQLSERVER”
'table=“tblLSItems”
'database=“LMStock”
如果符合第1条,则
con.Open“Provider=SQLOLEDB;Data Source=“&server&”Initial Catalog=“&database&”Integrated Security=SSPI;”
“开
如果结束
'这是受信任的连接字符串
设置rs.ActiveConnection=con
'如果选中复选框,则首先删除所有记录
如果。那么
con.执行“删除”和表(&T)
如果结束
'设置要导入的记录的第一行
如果你愿意,你也可以在一个范围内循环。
intImportRow=2
直到活页1。单元格(intImportRow,1)=“”
作为字符串的Dim strItemCode
作为字符串的Dim strScanCode
作为字符串的Dim strStyle
Dim strDescription作为字符串
作为字符串的Dim strPrice
将字符串变暗
strItemCode=Sheet1.单元格(intImportRow,2)
strScanCode=Sheet1.单元格(intImportRow,3)
strStyle=Sheet1.单元格(intImportRow,4)
strDescription=Sheet1.单元格(intImportRow,5)
strPrice=Sheet1.单元格(intImportRow,6)
strSalePrice=Sheet1.单元格(intImportRow,7)
'将行插入数据库
COMMANDSTRING=“插入tblLSItems(项目代码、扫描代码、样式、描述、价格、售价)值(“&strItemCode&“,”&strScanCode&“,”,“&StrstStyle&“,”,“&StrderDescription&“,”&strPrice&“,”&strSalePrice&“,“&strSalePrice&“)”
“con.Execute”插入tblLSItems(项目代码、扫描代码、样式、说明、价格、售价)值(“&strItemCode&“,”&strScanCode&“,”,“&StrsStyle&“,”,“&StrderDescription&“,”&strPrice&“,&strSalePrice&“,&strSalePrice&”)
con.executecommandstring
intImportRow=intImportRow+1
环
MsgBox(“完成导入”)
结案
设置con=Nothing
以
出口接头
呃:
MsgBox错误说明
MsgBox(命令字符串)
端接头
excel工作表有129848条记录
它总是在记录32766中给出错误
不管记录是什么
以下是下载excel工作表的链接
Private Sub btnUpdate_Click()
On Error GoTo errH
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strPath As String
Dim intImportRow As Integer
Dim strFirstName, strLastName As String
Dim server, username, password, table, database As String
With Sheets("Settings")
server = .txtServer.Text
table = .txtTable.Text
database = .txtDatabase.Text
' server = "LB-HO-NAYEF\MYMSSQLSERVER"
' table = "tblLSItems"
' database = "LMStock"
If con.State <> 1 Then
con.Open "Provider=SQLOLEDB;Data Source=" & server & ";Initial Catalog=" & database & ";Integrated Security=SSPI;"
'con.Open
End If
'this is the TRUSTED connection string
Set rs.ActiveConnection = con
'delete all records first if checkbox checked
If .cbDelete Then
con.Execute "delete " & table & ""
End If
'set first row with records to import
'you could also just loop thru a range if you want.
intImportRow = 2
Do Until Sheet1.Cells(intImportRow, 1) = ""
Dim strItemCode As String
Dim strScanCode As String
Dim strStyle As String
Dim strDescription As String
Dim strPrice As String
Dim strSalePrice As String
strItemCode = Sheet1.Cells(intImportRow, 2)
strScanCode = Sheet1.Cells(intImportRow, 3)
strStyle = Sheet1.Cells(intImportRow, 4)
strDescription = Sheet1.Cells(intImportRow, 5)
strPrice = Sheet1.Cells(intImportRow, 6)
strSalePrice = Sheet1.Cells(intImportRow, 7)
'insert row into database
COMMANDSTRING = "insert into tblLSItems (ItemCode, ScanCode,Style,Description,Price,SalePrice) values ('" & strItemCode & "','" & strScanCode & "','" & strStyle & "', '" & strDescription & "','" & strPrice & "','" & strSalePrice & "')"
' con.Execute "insert into tblLSItems (ItemCode, ScanCode,Style,Description,Price,SalePrice) values ('" & strItemCode & "','" & strScanCode & "','" & strStyle & "', '" & strDescription & "'," & strPrice & "," & strSalePrice & ")"
con.Execute COMMANDSTRING
intImportRow = intImportRow + 1
Loop
MsgBox ("Done importing")
con.Close
Set con = Nothing
End With
Exit Sub
errH:
MsgBox Err.Description
MsgBox (COMMANDSTRING)
End Sub
第2页有连接到数据库所需的设置:
整型、长型和字节数据类型
整型和长型数据类型都可以包含正值或负值。它们之间的区别在于大小:整数变量的值可以在-32768和32767之间,而长变量的值可以在-2147483648到2147483647之间
将
Dim intImportRow替换为Integer
替换为Dim intImportRow As Long
尝试Dim intImportRow As Long
-听起来您达到了有符号整数中存储的最大值的限制