Duplicated Invoice number checking when leave the field

A

aw

I have a FORM to input NEW invoice information.

How can I write code for prompt user of duplicate invoice number being
inputted (under the form) when user leave this field IMMEDIATELY

(rather than my existing one do this when all information already input and
prompt you at the time of saving)
 
A

Allen Browne

Private Sub InvoiceID_BeforeUpate(Cancel As Integer)
Dim strWhere As String
Dim strMsg As String
Dim varResult As Variant

With Me.[InvoiceID]
If Me.NewRecord And Not IsNull(.Value)
strWhere = "[InvoiceID] = " & .Value
varResult = DLookup("InvoiceID", "tblInvoice", strWhere)
If Not IsNull(varResult) Then
Cancel = True
strMsg = "That invoice number already exists." & vbCrLf & _
"Enter a different number, or press "<Esc> to undo."
MsgBox strMsg, vbExclamation, "Duplicate"
End If
End If
End With
End Sub
 
A

Al Campagna

aw said:
I have a FORM to input NEW invoice information.

How can I write code for prompt user of duplicate invoice number being
inputted (under the form) when user leave this field IMMEDIATELY

(rather than my existing one do this when all information already input
and
prompt you at the time of saving)
 
J

John W. Vinson

I have a FORM to input NEW invoice information.

How can I write code for prompt user of duplicate invoice number being
inputted (under the form) when user leave this field IMMEDIATELY

(rather than my existing one do this when all information already input and
prompt you at the time of saving)

If the user is manually entering the invoice number into txtInvoiceNo, you can
use the BeforeUpdate event of txtInvoiceNo (not the event of the Form):

Private Sub txtInvoiceNo_BeforeUpdate(Cancel as Integer)
If Not IsNull(DLookUp("InvoiceNo", "Invoices", _
"[InvoiceNo] = '" & Me!txtInvoiceNo & "'") Then
MsgBox "This invoice number has been used"
Cancel = True
<any other appropriate actions>
End If
End Sub
 
A

Al Campagna

aw,
Several ways to do that. One would be...
Your InvoiceNo should be a unique value key field.
In your table design, make it...
Indexed - No Duplicates
Use the AfterUpdate event of InvoiceID to
Refresh
the form, and trigger an Access "Duplicate Value" error.

Or... use the InvoiceID BeforeUpdate event to do a DLookup of the just
entered value, to see if it matches any previous values. If so,
post a custom message to the user that the InvoiceID is a Dupe, and ...
Cancel = True
InvoiceID.Undo
--
hth
Al Campagna
Microsoft Access MVP 2007-2009
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top