2012-11-13 71 views
1

我需要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 

任何帮助表示赞赏。

+3

如果你很高兴能够避免整个null问题,你可以说'如果.Value&“”<> .OldValue&“”然后'这将比较字符串。 – Fionnuala

+0

@Remou请将此作为答案写下,我会接受。这很好。 – John

回答

4

如果你很高兴,只是避免整个空的问题,可能不适合每个人,你可以说

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

这将比较字符串。

+0

啊,我明白你现在在说什么。我认为现在空白在语义上与我的应用程序的空值相同,所以它会没事的。 – John

相关问题