我需要Access 2010中的「跟蹤更改」功能。我跑過this script這應該可以做一些小工作。如何在VBA中處理MS Access 2010窗體控件上的null/nothing/blank值?
這很清楚,可以理解,但它還沒有完全處理我需要的文本框的所有情況。 (我稍後會實施其他控制。)這是書面的相關結構。它被稱爲表單上的Before Update事件的事件過程:
Const cDQ As String = """"
Sub AuditTrail(frm As Form, recordid As Control)
'Track changes to data.
'recordid identifies the pk field's corresponding
'control in frm, in order to id record.
Dim ctl As Control
Dim varBefore As Variant
Dim varAfter As Variant
Dim strControlName As String
Dim strSQL As String
On Error GoTo ErrHandler
'Get changed values.
For Each ctl In frm.Controls
With ctl
'Avoid labels and other controls with Value property.
If .ControlType = acTextBox Then
If .Value <> .OldValue Then
'Add record of change to logging table
End If
End If
End With
Next
Set ctl = Nothing
Exit Sub
ErrHandler:
'Handle error
End Sub
只要文本框內的值不是空白,就可以工作。如果.Value
或.OldValue
之一爲空(或者什麼也沒有 - 我對此感到困惑),那麼不平等就會失敗。
我試着將With ctl
以內的部分更新爲以下內容。這使我可以記錄其中一個值(之前或之後)爲空的更改。其餘註釋掉的東西是我試圖記錄所有值爲空但不變的情況。換句話說,IsNull(ctl)
可以讓所有事情都完成,但我無法弄清楚如何再次將平等情況過濾掉。
If .ControlType = acTextBox Then
If IsNull(ctl) Or .Value <> .OldValue Then
'If Not (Len(.Value & vbNullString) = 0 And Len(.OldValue & vbNullString) = 0) Then
'If oldValueIsNull Then
' varBefore = "--NULL--"
'Else
' varBefore = .OldValue
'End If
'If newValueIsNull Then
' varAfter = "--NULL--"
'Else
' varAfter = .Value
'End If
'Add record of change to logging table
'End If
End If
End If
任何幫助表示讚賞。
如果你很高興能夠避免整個null問題,你可以說'如果.Value&「」<> .OldValue&「」然後'這將比較字符串。 – Fionnuala
@Remou請將此作爲答案寫下,我會接受。這很好。 – John