如何使用Access VBA中的.findfirst修复运行时错误3251?

时间:2017-12-13 23:02:58

标签: vba ms-access

我遇到了.findfirst

的问题
  

运行时错误3251(此类型的操作不受支持   对象)。

它发生在这一行:

rs3.FindFirst "[IsolationPoints] = '" & RWPSheetvalues(ii) & "'"

整个代码如下:

Private Sub ImportBOPData_Click()
    'Declare and set variables for this database
    Dim db As Database
    Set db = CurrentDb()
    'Declare and set variables for "rwpT" table
    Dim rs As Recordset
    Set rs = db.OpenRecordset("rwpT")
    'Declare and set variables for the Field "File" in "rwpT" table
    Dim File As Field
    Set File = rs.Fields("File")
    'Declare and set variables for the Field "LastCompDate" in "rwpT" table
    Dim LastCompDate As Field
    Set LastCompDate = rs.Fields("LastCompDate")
    Dim ExistingDate As String
    'Declare and set variables for "AIP&BOP" table
    Dim rs2 As DAO.Recordset
    Set rs2 = db.OpenRecordset("AIP&BOP")
    Set File = rs.Fields("File")
    'Declare and set variables for "AIP&BOP" table
    Dim rs3 As DAO.Recordset
    Set rs3 = db.OpenRecordset("IsolationPoints")
    'Declare and set variables for Excel Application, Workbook & Worksheet
    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
    Set xls = New Excel.Application
    'Declare and set values for cells, rows in Excell worksheets
    Dim cellstring As String
    Dim rownumber As Integer
    Dim rowstring As String
    rowstring = CStr(rownumber)
    'Declare and set variables for looping through variables and row numbers
    Dim i As Integer
    Dim ii As Integer
    'Declare variables for column values in RWP spreadsheets
    Dim orgSheetCol(7) As String
        orgSheetCol(0) = "$A$" 'Auto Isolation Point
        orgSheetCol(1) = "$D$" 'Customer Minutes Lost
        orgSheetCol(2) = "$H$" '% work plan fix
        orgSheetCol(3) = "$E$" 'Customers in Incident Sustained
        orgSheetCol(4) = "$C3$" 'Plan Title
        orgSheetCol(5) = "$B33$" 'Years of Data
        orgSheetCol(6) = "$B37$" 'Annual Improved CML
    'Declare variables for cell values attained from RWP spreadsheets
    Dim RWPSheetvalues(100) As String
        RWPSheetvalues(0) = "" 'Autoisolation Point 1
        RWPSheetvalues(1) = "" 'CML for Point 1
        RWPSheetvalues(2) = "" '% Work Plan Fix
        RWPSheetvalues(3) = "" 'CI For Point 1
        RWPSheetvalues(4) = "" 'Autoisolation Point 2
        RWPSheetvalues(5) = "" 'CML for Point 2
        RWPSheetvalues(6) = "" '% Work Plan Fix
        RWPSheetvalues(7) = "" 'CI For Point 2
        RWPSheetvalues(8) = "" 'Autoisolation Point 3
        RWPSheetvalues(9) = "" 'CML for Point 3
        RWPSheetvalues(10) = "" '% Work Plan Fix
        RWPSheetvalues(11) = "" 'CI For Point 3
        RWPSheetvalues(12) = "" 'Autoisolation Point 4
        RWPSheetvalues(13) = "" 'CML for Point 4
        RWPSheetvalues(14) = "" '% Work Plan Fix
        RWPSheetvalues(15) = "" 'CI For Point 4
        RWPSheetvalues(16) = "" 'Autoisolation Point 5
        RWPSheetvalues(17) = "" 'CML for Point 5
        RWPSheetvalues(18) = "" '% Work Plan Fix
        RWPSheetvalues(19) = "" 'CI For Point 5
        RWPSheetvalues(20) = "" 'Autoisolation Point 6
        RWPSheetvalues(21) = "" 'CML for Point 6
        RWPSheetvalues(22) = "" '% Work Plan Fix
        RWPSheetvalues(23) = "" 'CI For Point 6
        RWPSheetvalues(24) = "" 'Autoisolation Point 7
        RWPSheetvalues(25) = "" 'CML for Point 7
        RWPSheetvalues(26) = "" '% Work Plan Fix
        RWPSheetvalues(27) = "" 'CI For Point 7
        RWPSheetvalues(28) = "" 'Autoisolation Point 8
        RWPSheetvalues(29) = "" 'CML for Point 8
        RWPSheetvalues(30) = "" '% Work Plan Fix
        RWPSheetvalues(31) = "" 'CI For Point 8
        RWPSheetvalues(32) = "" 'Autoisolation Point 9
        RWPSheetvalues(33) = "" 'CML for Point 9
        RWPSheetvalues(34) = "" '% Work Plan Fix
        RWPSheetvalues(35) = "" 'CI For Point 9
        RWPSheetvalues(36) = "" 'Autoisolation Point 10
        RWPSheetvalues(37) = "" 'CML for Point 10
        RWPSheetvalues(38) = "" '% Work Plan Fix
        RWPSheetvalues(39) = "" 'CI For Point 10
        RWPSheetvalues(40) = "" 'Autoisolation Point 11
        RWPSheetvalues(41) = "" 'CML for Point 11
        RWPSheetvalues(42) = "" '% Work Plan Fix
        RWPSheetvalues(43) = "" 'CI For Point 11
        RWPSheetvalues(44) = "" 'Autoisolation Point 12
        RWPSheetvalues(45) = "" 'CML for Point 12
        RWPSheetvalues(46) = "" '% Work Plan Fix
        RWPSheetvalues(47) = "" 'CI For Point 12
        RWPSheetvalues(48) = "" 'Autoisolation Point 13
        RWPSheetvalues(49) = "" 'CML for Point 13
        RWPSheetvalues(50) = "" '% Work Plan Fix
        RWPSheetvalues(51) = "" 'CI For Point 13
        RWPSheetvalues(52) = "" 'Autoisolation Point 14
        RWPSheetvalues(53) = "" 'CML for Point 14
        RWPSheetvalues(54) = "" '% Work Plan Fix
        RWPSheetvalues(55) = "" 'CI For Point 14
        RWPSheetvalues(56) = "" 'Autoisolation Point 15
        RWPSheetvalues(57) = "" 'CML for Point 15
        RWPSheetvalues(58) = "" '% Work Plan Fix
        RWPSheetvalues(59) = "" 'CI For Point 15
        RWPSheetvalues(60) = "" 'Autoisolation Point 16
        RWPSheetvalues(61) = "" 'CML for Point 16
        RWPSheetvalues(62) = "" '% Work Plan Fix
        RWPSheetvalues(63) = "" 'CI For Point 16
        RWPSheetvalues(64) = "" 'Autoisolation Point 17
        RWPSheetvalues(65) = "" 'CML for Point 17
        RWPSheetvalues(66) = "" '% Work Plan Fix
        RWPSheetvalues(67) = "" 'CI For Point 17
        RWPSheetvalues(68) = "" 'Autoisolation Point 18
        RWPSheetvalues(69) = "" 'CML for Point 18
        RWPSheetvalues(70) = "" '% Work Plan Fix
        RWPSheetvalues(71) = "" 'CI For Point 18
        RWPSheetvalues(72) = "" 'Autoisolation Point 19
        RWPSheetvalues(73) = "" 'CML for Point 19
        RWPSheetvalues(74) = "" '% Work Plan Fix
        RWPSheetvalues(75) = "" 'CI For Point 19
        RWPSheetvalues(76) = "" 'Autoisolation Point 20
        RWPSheetvalues(77) = "" 'CML for Point 20
        RWPSheetvalues(78) = "" '% Work Plan Fix
        RWPSheetvalues(79) = "" 'CI For Point 20
        RWPSheetvalues(80) = "" 'Autoisolation Point 21
        RWPSheetvalues(81) = "" 'CML for Point 21
        RWPSheetvalues(82) = "" '% Work Plan Fix
        RWPSheetvalues(83) = "" 'CI For Point 21
        RWPSheetvalues(84) = "" 'Autoisolation Point 22
        RWPSheetvalues(85) = "" 'CML for Point 22
        RWPSheetvalues(86) = "" '% Work Plan Fix
        RWPSheetvalues(87) = "" 'CI For Point 22
        RWPSheetvalues(88) = "" 'Autoisolation Point 23
        RWPSheetvalues(89) = "" 'CML for Point 23
        RWPSheetvalues(90) = "" '% Work Plan Fix
        RWPSheetvalues(91) = "" 'CI For Point 23
        RWPSheetvalues(92) = "" 'Autoisolation Point 24
        RWPSheetvalues(93) = "" 'CML for Point 24
        RWPSheetvalues(94) = "" '% Work Plan Fix
        RWPSheetvalues(95) = "" 'CI For Point 25
        RWPSheetvalues(96) = "" 'Plan Title
        RWPSheetvalues(97) = "" 'File Location
        RWPSheetvalues(98) = "" 'Years of Data
        RWPSheetvalues(99) = "" 'Annual Improved CML

    'Open up each of the Excel Spreadhseet given in the "File" field (hyperlink) of the "rwpT" table, get appropriate data, close Excel Spreadsheet and populate "AIT&BOP" table with data if it isn't already there.
    Do Until rs.EOF
        'Open up Excel Workbook (Read Only), "Benefit of Plan" spreadsheet
        Set wkb = xls.Workbooks.Open(File, ReadOnly:=True, UpdateLinks:=False)
        Set wks = wkb.Worksheets("Benefit of Plan")
        'Get all of the auto-isolation point info
        ii = 0
        For rownumber = 7 To 30
            rowstring = CStr(rownumber)
            For i = 0 To 3
                cellstring = orgSheetCol(i) & rowstring
                RWPSheetvalues(ii) = wks.Range(cellstring).Value
                If RWPSheetvalues(ii) = "" Then
                    RWPSheetvalues(ii) = "0"
                ElseIf RWPSheetvalues(ii) = " " Then
                    RWPSheetvalues(ii) = "0"
                End If
                ii = ii + 1
            Next i
        Next rownumber
        RWPSheetvalues(96) = wks.Range("C3").Value
        RWPSheetvalues(97) = File
        RWPSheetvalues(98) = wks.Range("B33").Value
        RWPSheetvalues(99) = wks.Range("K31").Value
        'Close workbook without saving
        wkb.Close False
        'Insert all isolation points, without duplication, into IsolatPoints Table
        ii = 0
        Do Until ii = 96
            'check IsolationPoints table to see if isolation point is already there
            tablecheck = DCount("IsolationPoints", "IsolationPoints", "IsolationPoints = '" & RWPSheetvalues(ii) & "'")
            'If Isolation Point doesn't exist in IsolationPoints table, insert it
            If (tablecheck = 0 And RWPSheetvalues(ii) <> "0") Then
                rs3.AddNew
                rs3!IsolationPoints.Value = RWPSheetvalues(ii)
                rs3!FirstDate.Value = LastCompDate
                rs3.Update
            End If
            'Need to find way to determine if isolation point exists in table then get date for it, then compare to new date & replace with oldest - if necessary
            If (tablecheck = 1 And RWPSheetvalues(ii) <> "0") Then
                rs3.FindFirst "[IsolationPoints] = '" & RWPSheetvalues(ii) & "'"
                ExistingDate = rs3.Fields("FirstDate")
                If CDate(ExistingDate) > CDate(LastCompDate) Then
                    rs3!FirstDate.Value = LastCompDate
                    rs3.Update
                End If
            End If
            ii = ii + 4
        Loop

        'check AIP & BOP table to see if plan (checks for recurrence of file path) is already there
        tablecheck = DCount("FileLocation", "AIP&BOP", "FileLocation = '" & RWPSheetvalues(97) & "'")
        'If plan doesn't exist in AIP&BOP table, insert it
        If tablecheck = 0 Then
            rs2.AddNew
            rs2!PlanTitle.Value = RWPSheetvalues(96)
            rs2!FileLocation.Value = RWPSheetvalues(97)
            rs2!YearsofData.Value = RWPSheetvalues(98)
            rs2!AnnualImprovedCML.Value = RWPSheetvalues(99)
            rs2!AIP1.Value = RWPSheetvalues(0)
            rs2!AIP1CML.Value = RWPSheetvalues(1)
            rs2!AIP1BOP.Value = RWPSheetvalues(2)
            rs2!AIP1CI.Value = RWPSheetvalues(3)
            rs2!AIP2.Value = RWPSheetvalues(4)
            rs2!AIP2CML.Value = RWPSheetvalues(5)
            rs2!AIP2BOP.Value = RWPSheetvalues(6)
            rs2!AIP2CI.Value = RWPSheetvalues(7)
            rs2!AIP3.Value = RWPSheetvalues(8)
            rs2!AIP3CML.Value = RWPSheetvalues(9)
            rs2!AIP3BOP.Value = RWPSheetvalues(10)
            rs2!AIP3CI.Value = RWPSheetvalues(11)
            rs2!AIP4.Value = RWPSheetvalues(12)
            rs2!AIP4CML.Value = RWPSheetvalues(13)
            rs2!AIP4BOP.Value = RWPSheetvalues(14)
            rs2!AIP4CI.Value = RWPSheetvalues(15)
            rs2!AIP5.Value = RWPSheetvalues(16)
            rs2!AIP5CML.Value = RWPSheetvalues(17)
            rs2!AIP5BOP.Value = RWPSheetvalues(18)
            rs2!AIP5CI.Value = RWPSheetvalues(19)
            rs2!AIP6.Value = RWPSheetvalues(20)
            rs2!AIP6CML.Value = RWPSheetvalues(21)
            rs2!AIP6BOP.Value = RWPSheetvalues(22)
            rs2!AIP6CI.Value = RWPSheetvalues(23)
            rs2!AIP7.Value = RWPSheetvalues(24)
            rs2!AIP7CML.Value = RWPSheetvalues(25)
            rs2!AIP7BOP.Value = RWPSheetvalues(26)
            rs2!AIP7CI.Value = RWPSheetvalues(27)
            rs2!AIP8.Value = RWPSheetvalues(28)
            rs2!AIP9CML.Value = RWPSheetvalues(29)
            rs2!AIP8BOP.Value = RWPSheetvalues(30)
            rs2!AIP8CI.Value = RWPSheetvalues(31)
            rs2!AIP9.Value = RWPSheetvalues(32)
            rs2!AIP9CML.Value = RWPSheetvalues(33)
            rs2!AIP9BOP.Value = RWPSheetvalues(34)
            rs2!AIP9CI.Value = RWPSheetvalues(35)
            rs2!AIP10.Value = RWPSheetvalues(36)
            rs2!AIP10CML.Value = RWPSheetvalues(37)
            rs2!AIP10BOP.Value = RWPSheetvalues(38)
            rs2!AIP10CI.Value = RWPSheetvalues(39)
            rs2!AIP11.Value = RWPSheetvalues(40)
            rs2!AIP11CML.Value = RWPSheetvalues(41)
            rs2!AIP11BOP.Value = RWPSheetvalues(42)
            rs2!AIP11CI.Value = RWPSheetvalues(43)
            rs2!AIP12.Value = RWPSheetvalues(44)
            rs2!AIP12CML.Value = RWPSheetvalues(45)
            rs2!AIP12BOP.Value = RWPSheetvalues(46)
            rs2!AIP12CI.Value = RWPSheetvalues(47)
            rs2!AIP13.Value = RWPSheetvalues(48)
            rs2!AIP13CML.Value = RWPSheetvalues(49)
            rs2!AIP13BOP.Value = RWPSheetvalues(50)
            rs2!AIP13CI.Value = RWPSheetvalues(51)
            rs2!AIP14.Value = RWPSheetvalues(52)
            rs2!AIP14CML.Value = RWPSheetvalues(53)
            rs2!AIP14BOP.Value = RWPSheetvalues(54)
            rs2!AIP14CI.Value = RWPSheetvalues(55)
            rs2!AIP15.Value = RWPSheetvalues(56)
            rs2!AIP15CML.Value = RWPSheetvalues(57)
            rs2!AIP15BOP.Value = RWPSheetvalues(58)
            rs2!AIP15CI.Value = RWPSheetvalues(59)
            rs2!AIP16.Value = RWPSheetvalues(60)
            rs2!AIP16CML.Value = RWPSheetvalues(61)
            rs2!AIP16BOP.Value = RWPSheetvalues(62)
            rs2!AIP16CI.Value = RWPSheetvalues(63)
            rs2!AIP17.Value = RWPSheetvalues(64)
            rs2!AIP17CML.Value = RWPSheetvalues(65)
            rs2!AIP17BOP.Value = RWPSheetvalues(66)
            rs2!AIP17CI.Value = RWPSheetvalues(67)
            rs2!AIP18.Value = RWPSheetvalues(68)
            rs2!AIP18CML.Value = RWPSheetvalues(69)
            rs2!AIP18BOP.Value = RWPSheetvalues(70)
            rs2!AIP18CI.Value = RWPSheetvalues(71)
            rs2!AIP19.Value = RWPSheetvalues(72)
            rs2!AIP9CML.Value = RWPSheetvalues(73)
            rs2!AIP19BOP.Value = RWPSheetvalues(74)
            rs2!AIP19CI.Value = RWPSheetvalues(75)
            rs2!AIP20.Value = RWPSheetvalues(76)
            rs2!AIP20CML.Value = RWPSheetvalues(77)
            rs2!AIP20BOP.Value = RWPSheetvalues(78)
            rs2!AIP20CI.Value = RWPSheetvalues(79)
            rs2!AIP21.Value = RWPSheetvalues(80)
            rs2!AIP21CML.Value = RWPSheetvalues(81)
            rs2!AIP21BOP.Value = RWPSheetvalues(82)
            rs2!AIP21CI.Value = RWPSheetvalues(83)
            rs2!AIP22.Value = RWPSheetvalues(84)
            rs2!AIP22CML.Value = RWPSheetvalues(85)
            rs2!AIP22BOP.Value = RWPSheetvalues(86)
            rs2!AIP22CI.Value = RWPSheetvalues(87)
            rs2!AIP23.Value = RWPSheetvalues(88)
            rs2!AIP23CML.Value = RWPSheetvalues(89)
            rs2!AIP23BOP.Value = RWPSheetvalues(90)
            rs2!AIP23CI.Value = RWPSheetvalues(91)
            rs2!AIP24.Value = RWPSheetvalues(92)
            rs2!AIP24CML.Value = RWPSheetvalues(93)
            rs2!AIP24BOP.Value = RWPSheetvalues(94)
            rs2!AIP24CI.Value = RWPSheetvalues(95)
            rs2.Update
        End If
        rs.MoveNext

    Loop
    rs.Close
    rs2.Close
    rs3.Close
End Sub

1 个答案:

答案 0 :(得分:1)

试试这个:

Set rs3 = db.OpenRecordset("IsolationPoints", dbOpenDynaset)

另外

Set rs3 = db.OpenRecordset("select * from IsolationPoints")

应该有效