如何在 VBA 中处理 MS Access 2010 表单控件上的 null/nothing/blank 值?

问题描述 投票:0回答:1

我需要 Access 2010 中的“跟踪更改”功能。我遇到了这个脚本,只需做一点工作就可以解决问题。

它清晰易懂,但它还不能完全处理我需要的文本框的所有情况。 (稍后我将实现其他控件。)这是所编写的相关结构。它被称为表单上的 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

如有任何帮助,我们将不胜感激。

vba logging null ms-access-2010
1个回答
4
投票

如果您很乐意避免整个空问题(这可能不适合所有人),您可以说

If .Value & "" <> .OldValue & "" Then 

它将比较字符串。

© www.soinside.com 2019 - 2024. All rights reserved.