简体   繁体   English

变量正在跨越不同的事件

[英]Variable is crossing different events

Apologies for the vague title, but here is my issue.为模糊的标题道歉,但这是我的问题。 I have a form that has several select lists and associated text boxes.我有一个表单,其中包含多个 select 列表和关联的文本框。 Basically the way it works is if you select a name from the first list, an AfterUpdate event is triggered to query the DB to see if the Eng_ID and Person_ID already exist in the table.基本上它的工作方式是,如果您 select 来自第一个列表的名称,则会触发 AfterUpdate 事件以查询数据库以查看 Eng_ID 和 Person_ID 是否已存在于表中。 If so, then delete that row then insert the updated row.如果是这样,则删除该行然后插入更新的行。 If there is not any records, then just insert the data.如果没有任何记录,则只需插入数据。 The problem is that when I click a name in the first list, then move to the second list, what's happening is that the the Person_ID of the first list is used for the DLookup query, then it delets the record, then inserts the record of the new person I selected in a different listbox.问题是,当我在第一个列表中单击一个名称,然后移动到第二个列表时,发生的事情是第一个列表的 Person_ID 用于 DLookup 查询,然后删除记录,然后插入记录我在不同的列表框中选择的新人。 The code is below: Thanks in advance代码如下: 提前致谢

    ' Add/Remove Participant 1
Private Sub lstPar1_AfterUpdate()
    Dim n As Integer
    Dim strCriteria As String
    Dim strSQL As String
        
     With Me.lstPar1
        For n = .ListCount - 1 To 0 Step -1
            strCriteria = "Eng_ID = " & Nz(Me.Eng_ID, 0) & " And Person_ID = " & .ItemData(n)
                If .Selected(n) = False Then
                ' If a person has been deselected, then delete row from table
                If Not IsNull(DLookup("Eng_ID", "tblEngParRole", strCriteria)) Then
                    strSQL = "DELETE * FROM tblEngParRole WHERE " & strCriteria
                    CurrentDb.Execute strSQL, dbFailOnError
                    
                End If
                Else
                ' If a person has been selected, then insert row into the table
                If IsNull(DLookup("Eng_ID", "tblEngParRole", strCriteria)) Then
                   strSQL = "INSERT INTO tblEngParRole (Eng_ID, Person_ID, ParticipantNumber, Role)" & "VALUES(" & Me.Eng_ID & "," & .ItemData(n) & "," & 1 & ",'" & Me.txtParRole1.Value & "' )"
                    CurrentDb.Execute strSQL, dbFailOnError
                End If
            End If
        Next n
    End With
    
End Sub

' Add/Remove Participant 2

Private Sub lstPar2_AfterUpdate()
    Dim n As Integer
    Dim strCriteria As String
    Dim strSQL As String
    
    With Me.lstPar2
     For n = .ListCount - 1 To 0 Step -1
            strCriteria = "Eng_ID = " & Nz(Me.Eng_ID, 0) & " And Person_ID = " & .ItemData(n)
                If .Selected(n) = False Then
                ' If a person has been deselected, then delete row from table
                If Not IsNull(DLookup("Eng_ID", "tblEngParRole", strCriteria)) Then
                    strSQL = "DELETE * FROM tblEngParRole WHERE " & strCriteria
                    CurrentDb.Execute strSQL, dbFailOnError
                End If
                Else
                ' If a person has been selected, then insert row into the table
                If IsNull(DLookup("Eng_ID", "tblEngParRole", strCriteria)) Then
                    strSQL = "INSERT INTO tblEngParRole (Eng_ID, Person_ID, ParticipantNumber, Role) " & "VALUES(" & Me.Eng_ID & "," & .ItemData(n) & "," & 2 & ",'" & Me.txtParRole2.Value & "' )"
                    CurrentDb.Execute strSQL, dbFailOnError
                End If
            End If
        Next n
    End With
End Sub

在此处输入图像描述

Using this image, if I select Daniel and enter his role, then the eng_ID, Person_ID, ParticipantNumber and Role are entered into the database as 130, 118, 1, Collaborator.使用此图像,如果我 select Daniel 并输入他的角色,则 eng_ID、Person_ID、ParticipantNumber 和 Role 将作为 130、118、1、Collaborator 输入到数据库中。 If I select Kristin, it deletes Daniel becuause it's still using Person_ID of 118 instead of hers which is 134, and since there is a corresponding record, it delets Daniel then adds Kristin.如果我是 select Kristin,它会删除 Daniel,因为它仍在使用 Person_ID 118 而不是她的 134,并且由于有相应的记录,它会删除 Daniel,然后添加 Kristin。

I don't have Access to test this with, but it seems like you need to separate Participant1 records from Participant2 records when you perform your DLookups.我没有访问权限来对此进行测试,但看起来您在执行 DLookup 时需要将 Participant1 记录与 Participant2 记录分开。

Also you can generalize your code by pulling the common parts into a separate sub.您还可以通过将公共部分拉入单独的子部分来概括您的代码。

Private Sub lstPar1_AfterUpdate()
    CheckParticipant Me.lstPar1, 1, Me.txtParRole1.Value
End Sub

Private Sub lstPar2_AfterUpdate()
    CheckParticipant Me.lstPar2, 2, Me.txtParRole2.Value
End Sub

Sub CheckParticipant(objList As Object, participantNum As Long, role As String)
    Dim n As Integer
    Dim strCriteria As String
    Dim strSQL As String
        
    With objList
        For n = .ListCount - 1 To 0 Step -1
            strCriteria = "Eng_ID = " & Nz(Me.Eng_ID, 0) & " And Person_ID = " & .ItemData(n) & _
                          " And ParticipantNumber=" & participantNum
            strSQL = ""
            If Not .Selected(n) Then
                ' If a person has been deselected, then delete row from table
                If Not IsNull(DLookup("Eng_ID", "tblEngParRole", strCriteria)) Then
                    strSQL = "DELETE * FROM tblEngParRole WHERE " & strCriteria
                End If
            Else
                ' If a person has been selected, then insert row into the table
                If IsNull(DLookup("Eng_ID", "tblEngParRole", strCriteria)) Then
                   strSQL = "INSERT INTO tblEngParRole (Eng_ID, Person_ID, ParticipantNumber, Role)" & _
                             " VALUES(" & Me.Eng_ID & "," & .ItemData(n) & "," & participantNum & _
                                      ",'" & role & "' )"
                End If
            End If
            If Len(strSQL) > 0 Then CurrentDb.Execute strSQL, dbFailOnError
        Next n
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM