如何在MS Access中过滤掉数组的null元素

时间:2008-11-25 17:26:27

标签: arrays ms-access null

我正在从其他几个领域中创建标题,并希望采用“正确”的方式:

Me.Title.Value = Join(Array([Conference], [Speaker], partstr), " - ")

除了[conference]之外,[speaker]或partstr可能为null,我不想要额外的“ - ”。是否有任何功能可以使这项工作变得简单明了?

2 个答案:

答案 0 :(得分:3)

不 - 你必须检查每一个,然后在最后清理

Dim Temp As String

If Not IsNull([Conference]) Then
  Temp = Temp & [Conference] & " - "
End If

If Not IsNull([Speaker]) Then
  Temp = Temp & [Speaker] & " - "
End If

If Not IsNull(partstr) Then
  Temp = Temp & partstr & " - "
End If

If Temp > "" then
  Me.Title.Value = Left(Temp, Len(Temp) - 3)
Else
  Me.Title.Value = Null
End If

使用通用功能修改:

Public Function JoinEx(ByVal pArray As Variant, ByVal pDelimiter As String) As String

  Dim sTemp As String
  Dim iCtr As Integer

  For iCtr = 0 To UBound(pArray)
    If Not IsNull(pArray(iCtr)) Then
      sTemp = sTemp & pArray(iCtr) & pDelimiter
    End If
  Next

  If sTemp > "" Then
   JoinEx = Left(sTemp, Len(sTemp) - Len(pDelimiter))
  End If

End Function

致电示例:

 JoinEx(Array("one","two","three"), " - ")  'Returns "One - Two - Three"
 JoinEx(Array(null,"two","three"), " - ")  'Returns "Two - Three"

答案 1 :(得分:0)

完全重写,这次正确测试:

 IIf(IsNull(Partstr), IIf(IsNull(Conference), Speaker, Conference & " - " + Speaker), Conference + " - " & Speaker + " - " & Partstr)