2

我正在从其他几个领域生成标题,并且想要“正确”的方式来做:

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

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

4

2 回答 2

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"
于 2008-11-25T17:45:29.137 回答
0

完全重写,这次经过适当测试:

 IIf(IsNull(Partstr), IIf(IsNull(Conference), Speaker, Conference & " - " + Speaker), Conference + " - " & Speaker + " - " & Partstr)
于 2008-11-25T17:59:17.177 回答