1

テーブルにリンクされた編集可能なフィールドを表示するサブフォームを含むフォームがあります。私が現在取り組んでいるプロジェクトの要件の 1 つは、レコードが最後に変更されたのはいつで、誰が変更したかを追跡する必要があるということです。

だから私がやったことは、フォームとサブフォーム内の編集可能なテキストボックスまたはコンボボックスごとに、イベントBeforeUpdateAfterUpdateイベントにイベントがあるようにしました。

たとえば、テキストボックスの私のBeforeUpdate:

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

そして私のAfterUpdateは:

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