1

这是我在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

excel表格有129848条记录

它总是在记录 32766 中给出错误

不管是什么记录

这是下载excel表的链接

表 2 具有连接到数据库所需的设置

4

1 回答 1

2

从 VBA 文档的使用数字部分

整数、长整数和字节数据类型

...

Integer 和 Long 数据类型都可以保存正值或负值。它们之间的区别在于它们的大小:整数变量的值可以在 -32,768 到 32,767 之间,而长变量的值可以在 -2,147,483,648 到 2,147,483,647 之间。

替换Dim intImportRow As IntegerDim intImportRow As Long

于 2013-04-11T11:27:11.843 回答