Previous Thread

6/30/2006 12:36:27 PM    check for duplicates upon cmd button click
Typically this would be easy for me to do, but i have a situation that 
 
is a bit confusing and would require some help: 
 
I have a form that allows a user to enter data (document information 
 
such as file name, unique file code, created by, etc) to be added to an 
 
existing table. The purpose of this form is to enter info for a NEW 
 
document (i.e. a new file code that doesnt' exist in the current db). 
 
I also have a feature that stores versions of the same file (and thus, 
 
repeated file codes). For reason that might go off topic, I require a 
 
single table that contains the information on the documents, where 
 
there can be multiple records with the same File Code due to newer 
 
versions (so, the File Code field is NOT a primary key). So let's just 
 
assume that I need this type of table set up...so my file code field 
 
has the property of "Indexed (Duplicates OK)". 
 
now, once the user inputs the data onto this form and clicks a button, 
 
I would like it to check if the file code that was input exists in the 
 
table. if it does that give an error since it is not a brand new file 
 
code. (as well i need to check that the file code field is not null). 
 
ordinarily I would just set the field as the primary key and access 
 
would automatically provide errors for duplicates/null input, but this 
 
case is different. 
 
I hope that is clear enough for you and someone can help me with this 
 
problem? 
 
Herman



6/30/2006 1:18:04 PM    Re: check for duplicates upon cmd button click
In the On Click event of your button put: 
 
If IsNull(Me.FileCode) Then 
 
MsgBox "You need to enter the file code.", vbOKonly 
 
Else 
 
IF DCount("FileCode", "YourTableName", "FileCode = '" & 
 
Me.FileCode & "'") > 0 then 
 
'FileCode already exists 
 
MsgBox "File code already exists.", vbOKOnly 
 
End IF 
 
End IF 
 
Hope that helps!