Solved

Override Access's default error message when a duplicate value is entered

Posted on 2014-02-04
6
621 Views
Last Modified: 2014-02-05
I have a users form based on a users table where the primary key is the username. I'd like to add code to give my own error message when a duplicate username is entered instead of Access's default error message.

Is it preferable to put this code in the BeforeUpdate event of the control or the form? Or somewhere else?

Can anyone provide me with sample code?

Thank you!
0
Comment
Question by:jrmcanada2
6 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 39834700
I would put on the before insert of form (and also before update if your users can change the username of an existing record.

Normally control events looks for things like format and form events look for things like duplicates, so put it on the form level.

Hope it helps. Regards.
0
 
LVL 75
ID: 39834705
The Duplicate Error is only rendered in the Form Error event. Consequently, you will need to trap that error number there, wherein you can display your own error message.
Note that Errors rendered in the Form Error event occur before the Before Update event.

mx
0
 

Author Comment

by:jrmcanada2
ID: 39834730
Thanks for the suggestions so far. Allow me to be a little clearer about what I'm looking for.

If someone duplicates a username (either by entering a new duplicate or changing an existing one), I'd like to pop up an error message of my own. I currently have no preference if the warning pops up as soon as the duplicate is entered or if it pops up when Access tries to update the table (either through a save button, switching records, or closing the form).

At no point do I want to see Access's error message.

So my questions are:

1. Is it better to display the warning as soon as the duplicate is entered or to display it when the form attempts to update the record?

2. Can someone provide me with an appropriate code sample?

Thanks again!
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 19

Assisted Solution

by:MINDSUPERB
MINDSUPERB earned 200 total points
ID: 39834750
Hello jrmcanada2,

You may use a similar code below on the BeforeEvent in your username textbox:

If DCount("Username", "tblSample", "[Username]= '" & Me.Username & "'") > 1 Then

MsgBox "Username is duplicated."
DoCmd.CancelEvent

Else

"Your Code"

End If

Sincerely,

Ed
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 200 total points
ID: 39834763
"as soon as the duplicate is entered"
Yes.

The code posted above should, except ... the count will never be >1 because no duplicates will be allowed, so the code s/b:

If DCount("Username", "tblSample", "[Username]= " & Chr(34) & Me.Username & Chr(34) ) = 1 Then

MsgBox "Username is duplicated."
DoCmd.CancelEvent

Else

"Your Code"

End If
0
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 39834782
Thanks, Joe.

You are right.

Sincerely,

Ed
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now