[英]over flow error ( VBA,excel that inserts data in sql database)
Here is my code in the excel sheet 这是我在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
the excel sheet has 129848 records Excel工作表中有129848条记录
it is always giving the error in the record 32766 它总是在记录32766中给出错误
no matter what is the record 无论记录如何
Here is a link to download the excel sheet 这是下载Excel工作表的链接
SHEET 2 HAS SETTINGS REQUIRED TO CONNECT TO THE DATABASE 数据表2必须进行设置才能连接到数据库
From the Working with Numbers section of the VBA documentation : 在VBA文档的“使用数字”部分中 :
The Integer, Long, and Byte Data Types
整型,长型和字节数据类型
...
...
The Integer and Long data types can both hold positive or negative values.
Integer和Long数据类型都可以包含正值或负值。 The difference between them is their size: Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647.
它们之间的区别是它们的大小:整数变量的值可以介于-32,768和32,767之间,而Long变量的值可以介于-2,147,483,648到2,147,483,647之间。
Replace Dim intImportRow As Integer
with Dim intImportRow As Long
. 用
Dim intImportRow As Long
替换Dim intImportRow As Integer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.