1

我有一个表单,其中包含一个子表单,该表单显示链接到我的一个表的可编辑字段。对于我目前正在进行的项目,其中一个要求是我必须跟踪对记录进行最后一次更改的时间以及更改的人员。

所以我所做的是为表单和子表单中的每个可编辑文本框或组合框制作它,以便他们在他们的BeforeUpdate和事件上都有AfterUpdate事件。

例如我的 BeforeUpdate 文本框:

Private Sub textbox_BeforeUpdate(Cancel As Integer)
    If Not isValidUser Then
        Cancel = True
        Me.textbox.Undo
    End If
End Sub

我的更新后是:

Private Sub textbox_AfterUpdate()
    updateRecord Me.textbox.Value, UserNameWindows
End Sub

和 updateRecord 是:

Public Sub updateRecord(bucNumber As String, updater As String)

    Dim Dbs As Object
    Dim rst As Object
    Dim fldEnumerator As Object
    Dim fldColumns As Object

    sqlStatement = "SELECT fName " & _
                        "FROM t_Staff " & _
                        "WHERE uName='" & updater & "';"

    'Getting fullname of user via username
    Set rst = CurrentDb.OpenRecordset(sqlStatement)

    'Setting fullname to updater variable
    updater = rst(0)

    'Clean Up
    Set rst = Nothing

    'Opening Bucket Contents
    Set Dbs = CurrentDb
    Set rst = Dbs.OpenRecordset("Bucket Contents")
    Set fldColumns = rst.Fields

    'Scan the records from beginning to each
    While Not rst.EOF
        'Check the current column
    For Each fldEnumerator In rst.Fields
        'If the column is named Bucket No
        If fldEnumerator.Name = "Bucket No" Then
            'If the Bucket No of the current record is the same as bucketNumber
                If fldEnumerator.Value = bucNumber Then
                    'Then change the updated fields by updater and todays date
                    rst.Edit
                    rst("Last Updated By").Value = updater
                    rst("Last Updated On").Value = Date
                    rst.Update
                End If
            End If
        Next
        'Move to the next record and continue the same approach
        rst.MoveNext
    Wend

    'Clean Up
    Set rst = Nothing
    Set Dbs = Nothing
End Sub

好的,现在是奇怪的事情,当我在主窗体中对控件进行修改时,这完全可以正常工作,但是一旦尝试更改子窗体中的某些内容,它就会引发写入冲突。

如果我选择保存记录,它会忽略我的代码来更新谁最后修改它以及何时以及如果我选择放弃更改,它会运行我的代码并更新它已更改!

任何人都知道有什么问题或更好的方法吗?

4

0 回答 0