简体   繁体   English

在 ms 访问中使用“IF”和“DLookup”

[英]using “IF” with “DLookup” in ms access

I am making a form in MS Access, my version is 2019 from Office 2019. I have problem with duplicate entry of the invoice for purchased merchandise.我正在 MS Access 中制作表格,我的版本是 Office 2019 的 2019。我在重复输入购买商品的发票时遇到问题。 Each supplier has their own way of defining their "invoice numbers" and sometimes they are using similar numbers since some use "date" to define their invoice number for example , company A invoice no is 202107001 and Company B also has 202107001 since it is July of 2021 and it happens to be the "first bill" of their month... some supplier do not have "invoice no."每个供应商都有自己定义“发票编号”的方式,有时他们使用相似的编号,因为有些供应商使用“日期”来定义他们的发票编号,例如,A 公司发票编号是 202107001,B 公司也有 202107001,因为它是 7 月2021 年,恰好是他们当月的“第一张账单”……有些供应商没有“发票号”。 because they are individual selling handmade product.因为他们是个人销售手工制品。

Therefore, I have to write vba code to prevent entering "duplicate" invoice number from our suppliers!因此,我必须编写 vba 代码以防止输入来自我们供应商的“重复”发票编号! Here is my unworking code... not even it doesn't work but also shows "method or data not found" too!这是我无法工作的代码......即使它不起作用,但也显示“找不到方法或数据”! Would there be anyone kindly help please?请问有好心人帮忙吗?

Here is the condition I intend to do with the below code "If the entered supplier invoice number is a duplicate value according to their own identification scheme, the message box should appear as the latter part of the vba code..."这是我打算使用以下代码执行的条件“如果输入的供应商发票编号根据他们自己的识别方案是重复值,则消息框应显示为 vba 代码的后半部分......”

Private Sub txtIMHead_supinvoicenumber_AfterUpdate()

    Dim supinvoicenumberstring As String
    
    
    supinvoicenumberstring = Me.IMHead_supinvoicenumber.Value
    
    
    If Me.txtHead_supinvoicenumber = DLookup("[IMHead_supinvoicenumber]", "tbl_IMHead", txt_IMHead_supid) Then
    MsgBox "This supplier's invoice number" & supinvoicenumberstring & ",has been recorded!" _
    & vbCr & " Please stop the process and record another invoice!" & vbCr & "Thank you very much!"
    Me.Undo
    
    End If

End Sub

WHERE CONDITION argument needs to specify field that criteria applies to: WHERE CONDITION 参数需要指定条件适用的字段:

DLookup("[IMHead_supinvoicenumber]", "tbl_IMHead", "some field name here=" & Me.txt_IMHead_supid)

However, that expression will return IMHead_supinvoicenumber from first record that matches the given supid.但是,该表达式将从与给定 supid 匹配的第一条记录返回 IMHead_supinvoicenumber。 You need to know if invoice number already exists for selected supid so must use invoice number in criteria as well.您需要知道所选 supid 的发票编号是否已存在,因此还必须在标准中使用发票编号。

If Not IsNull(DLookup("[IMHead_supinvoicenumber]", "tbl_IMHead", _
   "some field name here=" & Me.txt_IMHead_supid & _
   " AND IMHead_supinvoicenumber='" & supinvoicenumberstring & "'")) Then

An option is DCount:一个选项是 DCount:

If DCount("*", "tbl_IMHead", "some field name here=" & Me.txt_IMHead_supid & _
   " AND IMHead_supinvoicenumber='" & supinvoicenumberstring & "'") > 0 Then

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM