FindFirst搜索多个值

时间:2018-03-30 10:57:40

标签: vba ms-access access-vba ms-access-2010

我有一个名为listcomponents的列表框,每次用户都可以选择多个值。当用户选择列表框时,执行Findfirst操作并计算机器系统ID和机器子系统,并且基于这两个,获得rs1![机器子系统ID]。然后在下一个列表框的条件中再次使用rs1![机器子系统ID]中获得的值。

问题是FindFirst操作只检查rs1![Machine Subsystem ID]中的最后一个存储值,而不是所有生成的值(可能因为只存储了最后一个值)。那么有没有办法让我检查rs1中生成的所有值![机器子系统ID]。这是代码:

   ID = DMax("[MAchine ID]", "tblmachine")

 Set db = CurrentDb()
 Set rs = db.OpenRecordset("tblMachineSystem", dbOpenDynaset, dbAppendOnly)
  Set rs1 = db.OpenRecordset("tblMachineSubSystem", dbOpenDynaset, 
 dbAppendOnly)
 Set rs2 = db.OpenRecordset("tblComponents", dbOpenDynaset, dbAppendOnly)
 Set rsmas = db.OpenRecordset("tblMasterData", dbOpenDynaset, dbAppendOnly)

 'add selected value(s) to table
Set ctl = Me.listMachineSystem
Set ctl1 = Me.listMachineSubSystem
Set ctl2 = Me.listComponents

For Each varItem In ctl.ItemsSelected
rs.AddNew
rs!MachineSystem = ctl.ItemData(varItem)
rs![MAchine ID] = ID
rs.Update
 Next varItem


   For i = 0 To Me.listMachineSubSystem.ListCount - 1
    If Me.listMachineSubSystem.Selected(i) Then
        rs.FindFirst "[Machine ID]=" & ID & " AND [MachineSystem]=  '" & DLookup(" 
        [MachineSystem]", "tblMachineSystem", "[Machine System ID]=" & 
        Me.listMachineSubSystem.Column(2, i)) & "'"
        rs1.AddNew
        rs1![MachineSubsystem] = Me.listMachineSubSystem.Column(0, i)
        rs1![Machine Sytem ID] = rs![Machine System ID]
        rs1.Update
    End If
    Next i
    For i = 0 To Me.listComponents.ListCount - 1
        If Me.listComponents.Selected(i) Then
            rs1.FindFirst "[Machine Sytem ID]=" & rs![Machine System ID] & " AND 
            [MachineSubsystem]=  '" & DLookup("[MachineSubsystem]", 
            "tblMachineSubSystem", 
            "[Machine Subsystem ID]=" & Me.listComponents.Column(2, i)) & "'"
            If rs1.NoMatch Then
                MsgBox "no records found"
            Else
                Do While Not rs1.NoMatch
                    MsgBox "i found it!!!"
                    rs1.FindNext "[Machine Sytem ID]=" & rs![Machine System ID] & " 
                    AND [MachineSubsystem]=  '" & DLookup("[MachineSubsystem]", 
                    "tblMachineSubSystem", "[Machine Subsystem ID]=" & 
                    Me.listComponents.Column(2, i)) & "'"
                Loop
                rs1.FindNext "[Machine Sytem ID]=" & rs![Machine System ID] & " AND 
                [MachineSubsystem]=  '" & DLookup("[MachineSubsystem]", 
                "tblMachineSubSystem", "[Machine Subsystem ID]=" & 
                Me.listComponents.Column(2, i)) & "'"
            End If
            rs2.AddNew
            rs2![Components] = Me.listComponents.Column(0, i)
            rs2![Machine Subsystem ID] = rs1![Machine Subsystem ID]
            rs2.Update
        End If
    Next i

我有两个带有fiels和tblComponents的表tblmMchineSubSystem,当上面的代码运行时,我希望将机器子系统ID(tblmMchineSubSystem的主键)填充到tblComponents的机器子系统ID字段中。上面代码的问题是在findfirst函数中只检查了rs![Machine System ID]中的最后一个值。我想要FindFirst函数检查rs![机器系统ID]中的所有值。

新代码

   ID = DMax("[MAchine ID]", "tblmachine")
   Dim sMachineSubsystem As String, varSelectedID11 As Variant
   Dim vMaxMachineSubsystemID As Variant

   Set db = CurrentDb()
  Set rs = db.OpenRecordset("tblMachineSystem", dbOpenDynaset, dbAppendOnly)
   Set rs1 = db.OpenRecordset("tblMachineSubSystem", dbOpenDynaset, 
  dbAppendOnly)
 Set rs2 = db.OpenRecordset("tblComponents", dbOpenDynaset, dbAppendOnly)
 Set rsmas = db.OpenRecordset("tblMasterData", dbOpenDynaset, dbAppendOnly)

 Set ctl = Me.listMachineSystem
 Set ctl1 = Me.listMachineSubSystem
 Set ctl2 = Me.listComponents

'add selected value(s) from listMachineSystem to tblMachineSystem
For Each varItem In ctl.ItemsSelected
rs.AddNew
rs!MachineSystem = ctl.ItemData(varItem)
rs![MAchine ID] = ID
rs.Update
Next varItem

 For i = 0 To Me.listMachineSubSystem.ListCount - 1
 If Me.listMachineSubSystem.Selected(i) Then
  rs.FindFirst "[Machine ID]=" & ID & " AND [MachineSystem]=  '" & DLookup(" 
[MachineSystem]", "tblMachineSystem", "[Machine System ID]=" & 
 Me.listMachineSubSystem.Column(2, i)) & "'"

 rs1.AddNew
 rs1![MachineSubsystem] = Me.listMachineSubSystem.Column(0, i)
 rs1![Machine Sytem ID] = rs![Machine System ID]
 rs1.Update

    'Grab the maximum Machine System ID for the last selected item in the 
listMachineSubSystem list, for use in the next section
'To add components for every item selected in listMachineSubSystem, move the 
 entire below section of code to inside the previous section (so it's a loop 
 within a loop... be sure to rename i to something else)
  vMaxMachineSubsystemID = DMax("[Machine System ID]", "tblMachineSystem", " 
 [Machine ID]=" & ID & " AND [MachineSystem]=  '" & DLookup(" 
 [MachineSystem]", 
"tblMachineSystem", "[Machine System ID]=" & rs![Machine System ID]) & "'")

 'add selected value(s) from listComponents to tblComponents
 For l = 0 To Me.listComponents.ListCount - 1
  If Me.listComponents.Selected(l) Then
    varSelectedID2 = Me.listComponents.Column(2, l)
      sMachineSubsystem = DLookup("[MachineSubsystem]", 
"tblMachineSubSystem", "[Machine Subsystem ID]=" & varSelectedID2)

    rs1.FindFirst "[Machine Sytem ID]=" & vMaxMachineSubsystemID & " AND 
    [MachineSubsystem]=  '" & sMachineSubsystem & "'"
    If rs1.NoMatch Then
        MsgBox "no records found"
     Else
        Do While Not rs1.NoMatch
            MsgBox "I found it!!!"
            rs2.AddNew
            rs2![Components] = Me.listComponents.Column(0, l)
            rs2![Machine Subsystem ID] = rs1![Machine Subsystem ID]
            rs2.Update
            rs1.FindNext "[Machine Sytem ID]=" & vMaxMachineSubsystemID & " 
   AND [MachineSubsystem]=  '" & sMachineSubsystem & "'"
        Loop
    End If
   End If
  Next l
  End If
  Next i

1 个答案:

答案 0 :(得分:0)

我假设你把rs(没有1或2)设置为更早的东西?

这似乎非常复杂,并且会导致机器进行大量不必要的工作。我将一个TransactionID添加到tblMachineSubSystem表中,然后执行以下操作:

dim MachineSystemID as variant, iMaxTransactionID as integer, iNewTransactionID as integer

Set rs1 = db.OpenRecordset("SELECT * FROM tblMachineSubSystem WHERE [Machine Sytem ID] = 1", dbOpenDynaset, dbAppendOnly) 'Saves time when all you want to do is add to the table

For i = 0 To Me.listMachineSubSystem.ListCount - 1
    If Me.listMachineSubSystem.Selected(i) Then
        MachineSystemID = DLookup("[Machine System ID]", "tblMachineSystem", "[Machine System ID]=" & Me.listMachineSubSystem.Column(2, i))
        iMaxTransactionID = DMax("[TransactionID]", "tblMachineSystem") 'Note that this logic assumes a single-user database, so there aren't multiple transactions going on at the same time
        rs1.AddNew
        rs1![MachineSubsystem] = Me.listMachineSubSystem.Column(0, i)
        rs1![Machine Sytem ID] = rs![Machine System ID]
        iNewTransactionID = iMaxTransactionID + 1
        rs1![TransactionID] = iNewTransactionID
        rs1.Update
    End If
Next i
rs1.Close

docmd.runsql "INSERT INTO tblComponents (Components, [Machine Subsystem ID]) SELECT MachineSubsystem, [Machine Subsystem ID] FROM tblMachineSubSystem WHERE TransactionID = " & iNewTransactionID

编辑:好的,这是新版本:

ID = DMax("[MAchine ID]", "tblmachine")
dim sMachineSubsystem as string, varSelectedID11 as variant
dim vMaxMachineSubsystemID as variant

Set db = CurrentDb()
Set rs = db.OpenRecordset("tblMachineSystem", dbOpenDynaset, dbAppendOnly)
Set rs1 = db.OpenRecordset("tblMachineSubSystem", dbOpenDynaset, 
dbAppendOnly)
Set rs2 = db.OpenRecordset("tblComponents", dbOpenDynaset, dbAppendOnly)
Set rsmas = db.OpenRecordset("tblMasterData", dbOpenDynaset, dbAppendOnly)

Set ctl = Me.listMachineSystem
Set ctl1 = Me.listMachineSubSystem
Set ctl2 = Me.listComponents

'add selected value(s) from listMachineSystem to tblMachineSystem
For Each varItem In ctl.ItemsSelected
    rs.AddNew
    rs!MachineSystem = ctl.ItemData(varItem)
    rs![MAchine ID] = ID
    rs.Update
Next varItem

'add selected value(s) from listMachineSubSystem to tblMachineSubSystem
For i = 0 To Me.listMachineSubSystem.ListCount - 1
    If Me.listMachineSubSystem.Selected(i) Then
        varSelectedID1 = Me.listMachineSubSystem.Column(2, i)
        sMachineSubsystem = Me.listMachineSubSystem.Column(0, i)

        rs1.AddNew
        rs1![MachineSubsystem] = sMachineSubsystem
        rs1![Machine Sytem ID] = varSelectedID1
        rs1.Update
    End If
Next i

'Grab the maximum Machine System ID for the last selected item in the listMachineSubSystem list, for use in the next section
'To add components for every item selected in listMachineSubSystem, move the entire below section of code to inside the previous section (so it's a loop within a loop... be sure to rename i to something else)
vMaxMachineSubsystemID = DMax("[Machine System ID]", "tblMachineSystem", "[Machine ID]=" & ID & " AND [MachineSystem]=  '" & DLookup("[MachineSystem]", "tblMachineSystem", "[Machine System ID]=" & varSelectedID1) & "'")

'add selected value(s) from listComponents to tblComponents
For i = 0 To Me.listComponents.ListCount - 1
    If Me.listComponents.Selected(i) Then
        varSelectedID2 = Me.listComponents.Column(2, i)
        sMachineSubsystem = DLookup("[MachineSubsystem]", "tblMachineSubSystem", "[Machine Subsystem ID]=" & varSelectedID2)

        rs1.FindFirst "[Machine Sytem ID]=" & vMaxMachineSubsystemID & " AND [MachineSubsystem]=  '" & sMachineSubsystem & "'"
        If rs1.NoMatch Then
            MsgBox "no records found"
        Else
            Do While Not rs1.NoMatch
                MsgBox "I found it!!!"
                rs2.AddNew
                rs2![Components] = Me.listComponents.Column(0, i)
                rs2![Machine Subsystem ID] = rs1![Machine Subsystem ID]
                rs2.Update
                rs1.FindNext "[Machine Sytem ID]=" & vMaxMachineSubsystemID & " AND [MachineSubsystem]=  '" & sMachineSubsystem & "'"
            Loop
        End If
    End If
Next i