2017-02-09 88 views
0

我有以下vba代碼來檢查產品是否已在子窗體上選中。然後提醒用戶。如何檢查是否已經在子窗體上選擇了一個項目?

Private Sub ProdId_Combo_BeforeUpdate(Cancel As Integer) 
    Dim icount As Long 
    icount = Nz(DCount("[ProdID]", "ProdRestockDetails", "[ProdID]=" & Me.ProdID & " AND RestockID=" & Me.RestockID, 0)) 
    If icount <> 0 Then 
     MsgBox "this item has already been selected." 
     Cancel = True 
     Undo 
    End If 
End Sub 

但它會引發錯誤。 有人可以幫我正確設置它嗎?

+0

你的錯誤發生在哪裏?您的ProdID數字(和RestockID相同)?如果它們是文本字段,則需要添加引號。例如icount = DCount(「[ProdID]」,「ProdRestockDetails」,「[ProdID] ='」&Me.ProdID&「'AND RestockID ='」&Me.RestockID&「'」) – RyanL

+0

它們都是自動編號。鈮:當我只使用ProdId它的工作原理,但是當我在where子句中添加RestockID時,它會拋出錯誤。 – ezybusy

回答

0

看起來像你的Dcount和Nz函數沒有適當地結束。你可以試試這個嗎?此外,使用DCount函數,您不需要Nz包裝器,因爲如果未找到該值,則應該返回零。

Private Sub ProdId_Combo_BeforeUpdate(Cancel As Integer) 
    Dim icount As Long 
    icount = Nz(DCount("[ProdID]", "ProdRestockDetails", "[ProdID]=" & Me.ProdID & " AND RestockID=" & Me.RestockID), 0) 
    If icount <> 0 Then 
     MsgBox "this item has already been selected." 
     Cancel = True 
     Undo 
    End If 
End Sub 
+0

太棒了。它完成這項工作。謝謝RyanL – ezybusy

相關問題