473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Error message - unique index created on two different fields

Hello,

I create 2 unique indexes on 2 fields: Field1 and Field2.

What is the way generate custom error message independent of each
field?ex.:
"Duplicate value in Field1" or "Duplicate value in Field2".

If "OnError" event is used (error code 3022 in this case) the same
error
message will be displayed for both fields.

Thank you for response.

OS:win xp
MS Access 2003
Nov 4 '08 #1
8 3106
Not sure I understand.

If you want both field to be indepently unique, you would need 2 indexes
(one on each field.)

For a multi-field index, there's a huge number of possible combinations to
examine, as it's not limited to 2 fields. Even for just 2 fields, the cases
could be:
- A has no duplicate, and neither does B
- A has a duplicate and B does not
- B has a duplicate, and A does not
-Both A and B have a duplicate
But that's before you consider:
- the could be more than 2 fields;
- the could be several multi-field indexes in the table;
- there could be several tables involved (if the form is based on a query.

Perhaps you could use the Error event of the form. If the duplicate error
occurs, examine each index in turn, using a DLookup() to see which field(s)
have the duplicate(s.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"zyzolus" <zy*****@o2.plwrote in message
news:70**********************************@1g2000pr d.googlegroups.com...
>
I create 2 unique indexes on 2 fields: Field1 and Field2.

What is the way generate custom error message independent of each
field?ex.:
"Duplicate value in Field1" or "Duplicate value in Field2".

If "OnError" event is used (error code 3022 in this case) the same
error
message will be displayed for both fields.

Thank you for response.

OS:win xp
MS Access 2003
Nov 5 '08 #2

Thank you for prompt reply.
Not sure I understand.
If you want both field to be indepently unique, you would need 2 indexes
(one on each field.)
That is correct. I create 2 indexes (one on each field)

************************************************** ***********
- A has no duplicate, and neither does B
This is the combination I am interested in.

************************************************** ***********
Perhaps you could use the Error event of the form. If the duplicate error
occurs, examine each index in turn, using a DLookup() to see which field(s)
have the duplicate(s.)
What is the way to distinguish 2 (or more) fields with unique indexes
with DLookup() function in Error event and as a result display 2 (or
more) different error messages?
Nov 8 '08 #3
Okay, so you have 2 unique indexes, and you are asking how to tell which one
triggered the form's Error event.

This example shows how to use DLookup() to see if a field has the duplicate:

Dim strWhere As String
Dim strMsg As String
Dim varResult As Variant

With Me.[YourNumberField]
If (.Value = .OldValue) OR (IsNull(.Value)) Then
'do nothing
Else
strWhere = .Name & " = " & .Value
varResult = DLookup(.Name, "Table1", strWhere)
If Not IsNull(varResult) Then
strMsg = strMsg & "Duplicate in " & .Name & vbCrLf
End If
End If
End With
If strMsg <vbNullString Then
MsgBox strMsg
End If

Note that if it is a Text field, you need extra quotes as delimiters:
strWhere = .Name & " = """ & .Value & """"
or if it is a Date field, use # as the delimiter.

Also, the code is designed so you can repeat the With block for the other
field.

HTH

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"zyzolus" <zy*****@o2.plwrote in message
news:92**********************************@f40g2000 pri.googlegroups.com...
>
Thank you for prompt reply.
>Not sure I understand.
If you want both field to be indepently unique, you would need 2 indexes
(one on each field.)
That is correct. I create 2 indexes (one on each field)

************************************************** ***********
>- A has no duplicate, and neither does B
This is the combination I am interested in.

************************************************** ***********
>Perhaps you could use the Error event of the form. If the duplicate error
occurs, examine each index in turn, using a DLookup() to see which
field(s)
have the duplicate(s.)

What is the way to distinguish 2 (or more) fields with unique indexes
with DLookup() function in Error event and as a result display 2 (or
more) different error messages?
Nov 9 '08 #4
Thank you for help.
Works OK.
Nov 9 '08 #5
Hello,
The hints you gave work perfectly in case indexes are created with
separete fields.

What is the way to modify the code you wrote in your message so it can
be used with an index created with multiple fields?
Assume, indexes(unique) are created with the following combination of
fields:

Index_name1 Field1
Field2
Index_name2 Field3
Field4

So, if a combination of above fields is repeated a custom error
message is displayed (separete for "Index_name1" and "Index_name2").

Thank you.
Nov 11 '08 #6
Put 2 phrases into the Criteria, e.g.:
strWhere = ("[MyNumberField] = " & Me.MyNumberField & ") AND
([MyTextField] = """ & Me.MyTextField & """)"

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"zyzolus" <zy*****@o2.plwrote in message
news:4b**********************************@l33g2000 pri.googlegroups.com...
Hello,
The hints you gave work perfectly in case indexes are created with
separete fields.

What is the way to modify the code you wrote in your message so it can
be used with an index created with multiple fields?
Assume, indexes(unique) are created with the following combination of
fields:

Index_name1 Field1
Field2
Index_name2 Field3
Field4

So, if a combination of above fields is repeated a custom error
message is displayed (separete for "Index_name1" and "Index_name2").

Thank you.
Nov 12 '08 #7
Put 2 phrases into the Criteria, e.g.:
* * strWhere = ("[MyNumberField] = " & Me.MyNumberField & ") AND
([MyTextField] = """ & Me.MyTextField & """)"
I appreciate your help.
The criteria look pretty interesting.
However, the suggested earlier code:
varResult = DLookup(.Name, "Table1", strWhere)
doesn't allow to use "DLookup" function with multiple fields ie:
wrong: varResult = DLookup(MyNumberField AND MyTextField, "Table1",
strWhere)

How can the above code be modified so it can be used in the code
proposed in the post written on Nov 9th?
Or should it be written in a different way?
Nov 12 '08 #8
If it's a duplicate, you already know the 2 values (they're in the form), so
the actual field you ask DLookup() to find doesn't matter.

If you really want you can ask DLookup() to return 2 fields as a
concatenated string, like this:
DLookup("[Surname] & "", "" & [FirstName]", "tblCustomer")

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"zyzolus" <zy*****@o2.plwrote in message
news:69**********************************@n33g2000 pri.googlegroups.com...
Put 2 phrases into the Criteria, e.g.:
strWhere = ("[MyNumberField] = " & Me.MyNumberField & ") AND
([MyTextField] = """ & Me.MyTextField & """)"
I appreciate your help.
The criteria look pretty interesting.
However, the suggested earlier code:
varResult = DLookup(.Name, "Table1", strWhere)
doesn't allow to use "DLookup" function with multiple fields ie:
wrong: varResult = DLookup(MyNumberField AND MyTextField, "Table1",
strWhere)

How can the above code be modified so it can be used in the code
proposed in the post written on Nov 9th?
Or should it be written in a different way?
Nov 13 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Morten Gulbrandsen | last post by:
C:\mysql\bin>mysql -u elmasri -pnavathe company Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 29 to server version: 4.1.0-alpha-max-debug Type...
0
by: Morten Gulbrandsen | last post by:
mysql> USE company; Database changed mysql> mysql> DROP TABLE IF EXISTS EMPLOYEE; -------------- DROP TABLE IF EXISTS EMPLOYEE -------------- Query OK, 0 rows affected (0.00 sec)
5
by: S.Patten | last post by:
Hi, I have a problem with updating a datetime column, When I try to change the Column from VB I get "Incorrect syntax near '942'" returned from '942' is the unique key column value ...
7
by: Bercin Ates via SQLMonster.com | last post by:
I?m getting an error when I execute a stored procedure which is try to insert a row to a table. The error is: Server: Msg 1934, Level 16, State 1, Procedure SRV_SP_IS_EMRI_SATIRI_EKLE, Line 32...
8
by: DraguVaso | last post by:
Hi, I want my application do different actions depending on the exception it gets. For exemple: I have an SQL-table with a unique index. In case I try to Insert a record that's alreaddy in it I...
2
by: CSDunn | last post by:
Hello, I need some assistance with error handling in an Access 2003 Project form. The project is has a data source connection to a SQL Server 2000 database. The main form is named...
3
by: Nathan Sokalski | last post by:
When I view any page in my application a second time, I recieve the following error: System.Web.TraceContext.AddNewControl(String id, String parentId, String type, Int32 viewStateSize) +313...
0
by: zyzolus | last post by:
I create 2 unique indexes on 2 fields: Field1 and Field2. What is the way generate custom error message independent of each field?ex.: "Duplicate value in Field1" or "Duplicate value in Field2"....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.