2015-11-23 55 views
1

我想輸入重複號碼時出現消息框。如果BOTH字段匹配[Source]和[Voucher_Number],則此表中的副本爲。重複輸入警告消息框

【來源】爲文本格式,

[Voucher_Number]被格式化爲編號

這裏是我的代碼如下所示:

If (IsNull(DLookup("Source", "tblInvoiceLog", "Source ='" & 
Me.Source.Value & "'"))) And _ 
(IsNull(DLookup("Voucher_Number", "tblInvoiceLog", "Voucher_Number ='" & 
Me.Voucher_Number.Value & "'"))) Then 

Else 
MsgBox "Duplicate Entery!" & Chr(13) & Chr(10) & "Please Use the Next 
Available Consecutive Voucher Number", vbInformation, "Required" 
End If 

End Sub 

我越來越:

運行時錯誤3464

除了解決這個問題之外,我最終想做的是在消息框中返回原始條目的[Vendor_Name]中字段的值。

謝謝你的任何幫助,任何人都可以借

+0

自從我使用VBA或VB以來,已經有很長的一段時間了,但是好像你在爲邏輯運算符混合&和「And」......你能檢查代碼是否正確嗎? –

+0

對不起,「Me.Source.Value&」'「)))_和_應該是」Me.Source.Value&「'」)))或_ –

+0

沒關係,我記得&是VB中的字符串concat。 ..與其他語言的按位/邏輯運算符混淆。看起來好像在if語句之前將字符串連接起來會更好,至少這是我第一次嘗試。 –

回答

1

你可以嘗試專門鑄造使用DLookup返回值作爲字符串,以確保您比較蘋果和蘋果。如果仍然出現錯誤,請使用F8逐步瀏覽並將鼠標懸停在s1stLookup和s2ndLookup上,查看分配給變量的值。

Dim s1stLookup as String 
Dim s2ndLookup as String 

'Specifically cast the DLookup return values as Strings 
s1stLookup = CStr(DLookup("Source", "tblInvoiceLog", "Source ='" & Me.Source.Value & "'")) 
s2ndLookup = CStr(DLookup("Voucher_Number", "tblInvoiceLog", "Voucher_Number ='" & Me.Voucher_Number.Value & "'")) 

If (IsNull(s1stLookup)) And (IsNull(s2ndLookup)) Then 
    '... Presumably some code here 
Else 
    MsgBox "Duplicate Entery!" & vbCrLF & _ 
    "Please Use the Next Available Consecutive Voucher Number", _ 
    vbInformation, "Required" 
End If 
+1

Upvoted只是用於提取查找出來的'If'聲明;我會認爲'IsNull(String)'會因爲'CStr'而不管'DLookup'的值是否會返回一致的'False',所以這個條件可以被重新設置爲(如果我沒有完全打破這個)'If不是(s1stLookup = vbNullString或s2ndLookup = vbNullString)Then',如果只有一個'Else'分支。另外考慮使用'vbNewLine'而不是換行+回車。 –

+0

運行時錯誤3464,它看起來像第一個查詢產生的答案,但第二個不是?我真的很感謝你投入這個 –

+0

首先,@ Mat'sMug是正確的。如果DLookup返回Null,則CStr會將其轉換爲「1」,因此它永遠不會爲空。良好的抓地墊!如果第二個DLookup引發數據類型不匹配錯誤,請嘗試:'「Voucher_Number =」&CStr(Me.Voucher_Number.Value)' – Tim

0

當前您正在單獨測試兩個值。但是

如果BOTH字段匹配[Source]和[Voucher_Number],則此表中的副本爲。

因此,您需要測試兩個值是否存在於同一記錄中,方法是將兩個條件都與AND放入DLookup調用中。既然你想要得到的[Vendor_Name]如果是重複的,可以直接查找此字段:

Dim sVendor as String 
sVendor = Nz(DLookup("Vendor_Name", "tblInvoiceLog", _ 
    "Source = '" & Me.Source & "' AND Voucher_Number = " & Me.Voucher_Number), "") 

If sVendor <> "" Then 
    MsgBox "Duplicate entry of vendor '" & sVendor & "'." 
Else 
    ' ok, no dupe 
End If 

我覺得這是最好的總是使用Nz()DLookup(),所以我可以用字符串,而不是變種工作。一個空字符串意味着沒有結果。

+0

完美結合,並且非常讚賞提示。非常感謝 –