我有一個表格,它根據保險已過期的公司選擇電子郵件。一些公司同時擁有企業和個人電子郵件,而另一些公司則擁有其中一個。我正在使用 DLookUp 功能根據符合過濾條件的供應商選擇電子郵件。有沒有辦法讓我選擇 BusinessEmail 但如果他們沒有選擇 PersonalEmail 代替?
目前,我已經開始=DLookUp("BusinessEmail","tblContacts","Supplier ID = " & [txtSupplierID]使用 IIf 陳述句,然后在其中使用 IIf 陳述句,但我不知道它將如何選擇另一封電子郵件,或者它是否會以這種方式作業。
聯系人樣本資料
uj5u.com熱心網友回復:
如果未找到該值,則該DLookup()函式回傳null,因此如果企業電子郵件為空,您可以使用該Nz()函式檢查個人電子郵件。
由于我們將回傳值分配給字串并且字串不能包含空值,因此Nz()如果個人電子郵件也不存在(以避免錯誤),我們再次使用該函式回傳一個空字串。
Dim criteria As String
criteria = "[Supplier ID] = " & [txtSupplierID]
Dim email As String
email = Nz(DLookup("BusinessEmail", "tblContacts", criteria), Nz(DLookup("PersonalEmail", "tblContacts", criteria), vbNullString))
編輯:
添加第二條規則以DLookup排除空值,應該可以。查看幫助函式以通過輸出引數獲取電子郵件并報告成功/失敗:
Private Function TryGetEmail(ByVal supplierId As Long, ByRef outEmail As String) As Boolean
Dim retValue As Variant
'try to get business
retValue = DLookup("BusinessEmail", "tblContacts", "[Supplier ID] = " & supplierId & " AND BusinessEmail Is Not Null")
If Not IsNull(retValue) Then
outEmail = retValue
TryGetEmail = True
Exit Function
End If
'try to get personal
retValue = DLookup("PersonalEmail", "tblContacts", "[Supplier ID] = " & supplierId & " AND PersonalEmail Is Not Null")
If Not IsNull(retValue) Then
outEmail = retValue
TryGetEmail = True
Exit Function
End If
'none found
outEmail = vbNullString
TryGetEmail = False
End Function
呼叫它:
Sub T()
Dim supplierId As Long
Dim email As String
supplierId = 19
If Not TryGetEmail(supplierId, email) Then
MsgBox "No email found"
Exit Sub
End If
'email now holds one of the two emails
End Sub
uj5u.com熱心網友回復:
首先,將“Supplier ID”欄位更改為“SupplierID”,這將使您的開發更容易。
下面是一些同時使用 Dlookup 和 IIf 的代碼,將結果放在一個名為 txtEmail 的文本框中:
Private Sub getEmail()
txtEmail = DLookup("Iif(Isnull(BusinessEmail),PersonalEmail,BusinessEmail)", "tblContacts", "SupplierID = " & txtSupplierID)
End Sub
或這個:
Private Sub getEmail()
Dim Email As Variant
Email = DLookup("BusinessEmail", "tblContacts", "SupplierID = " & txtSupplierID)
If IsNull(Email) Then Email = DLookup("PersonalEmail", "tblContacts", "SupplierID = " & txtSupplierID)
txtEmail = Email
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/482484.html
