So I'm attempting to adopt some code that I came across in an effort to prevent people from double entering invoices. I think I understand the basic premise which is to use the DLookup to compare what has been entered to values already existing in the table and if it matches, then prevent the invoice from being entered.
The code I found uses only one criteria to prevent duplicate entry. However, I need to use 4 criteria to prevent an entry (VendorID, InvoiceDate, InvoiceNum, and Amount). In addition, I'm using a combobox to allow the user to select the VendorID. Here is what I have so far.
The first problem I get when I run this is a datatype mis-match on the VendorID in the DLookup. After I fix this problem I know there will be some sort of If statement or Loop to allow me to use all 4 DLookup's, but I need to get through this initial problem first and understand what I'm doing wrong with the DLookup itself. The commented portion of the code is the original code I'm working from.
Table Fields are set as follows, VendorID is an integer, Amount is set as current, InvoiceDate is a date/time, and InvoiceNum is text
The code I found uses only one criteria to prevent duplicate entry. However, I need to use 4 criteria to prevent an entry (VendorID, InvoiceDate, InvoiceNum, and Amount). In addition, I'm using a combobox to allow the user to select the VendorID. Here is what I have so far.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Vendor As Variant
Dim Amount As Variant
Dim InvoiceDate As Variant
Dim InvoiceNum As Variant
Vendor = DLookup("[VendorID]", "tblMaintenance", "[VendorID] = '" & Me.VendorID & "'")
Amount = DLookup("[Amount]", "tblMaintenance", "[Amount] ='" & Me.Amount & "'")
InvoiceDate = DLookup("[InvoiceDate]", "tblMaintenance", "[InvoiceDate] ='" & Me.InvoiceDate & "'")
InvoiceNum = DLookup("[InvoiceNum]", "tblMaintenance", "[InvoiceNum] ='" & Me.InvoiceNum & "'")
'Answer = DLookup("[SocialSecurity]", "tblApplicant", "[SocialSecurity] = '" & Me.SocialSecurity & "'")
'if Not IsNull(Answer) Then
'MsgBox "Duplicate Social Security Number Found" & vbCrLf & "Please enter again.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
'Cancel = True
'Me.SocialSecurity.Undo
If Not IsNull(Vendor) Then
MsgBox "Duplicate Vendor Found" & vbCrLf & "Please enter again.", vbCritical + vbOKOnly + vbDefaultButton1, "Duplicate"
Cancel = True
Me.VendorID.Undo
Else:
End If
End Sub
Table Fields are set as follows, VendorID is an integer, Amount is set as current, InvoiceDate is a date/time, and InvoiceNum is text
Last edited: