2008-11-25 57 views
2

我生成片頭一些其他領域,並希望「正確」的方式來做到:如何篩選出數組的空元素在MS Access

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

除了任何[會議]中,[speaker]或者parttr可能是空的,我不想要額外的「 - 」。有沒有任何功能可以使這項工作簡單直接?

回答

2

都能跟得上 - 你必須在最後

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" 
+0

我仍然希望有一個更好的答案,我會做很多次,並且不想複製這麼多的代碼。 – Thelema 2008-11-25 19:24:04

+0

我感覺到你的痛苦 - 但是再多做一點工作,你可以創建一個接受數組和分隔符並返回字符串的通用函數。 – 2008-11-25 19:27:51

0

一完成重新寫入,並且這次正確測試:

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