Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking for duplicate records within a table, from form

Status
Not open for further replies.

Delboy14

Programmer
Jun 21, 2001
213
0
0
GB
In my form I am trying to add a new record, the code to add a record works correctly. I am trying to add a check which displays a message box, if the user atempts to add a record which already exists.
I have attempted to do this using the code below, the code I have added (which does not work properly) is within the While loop, the error I recieve is "Invalid or Unqualified reference" in the 6th line of code at the if statement section, where the "[PROJECT_NAME]" is not recognised


Code:
 Set rst = dbs.OpenRecordset("tblPROJECTS")
           NumRecords = DCount("*", "tblPROJECTS")
           rst.MoveFirst
           While Not rst.EOF
           
           If ![PROJECT_NAME] = Me![Project_Name_Text] Then
           MsgBox "A project with this name already exists, please enter a unique name"
                     
           rst.MoveNext
           Wend
           
           
           
                With rst
                .AddNew
                ![PROJECT_THEME_ID] = Me![Project_Theme_Choice]
                ![PROJECT_ID] = Nz(DMax("[PROJECT_ID]", "tblPROJECTS"), 0) + 1
                ![PROJECT_NAME] = Me![Project_Name_Text]
                ![PROJECT_DESCRIPTION] = Me![Project_Description_Text]
                ![PROJECT_START_DATE] = Me![Project_Start_Date_Value]
                ![PROJECT_END_DATE] = Me![Project_End_Date_Value]
                ![PROJECT_LEADER_ID] = Me![Project_Lead_Choice]
                .Update
                .Close
                End With
            MsgBox "* New project added to list. *"
 
Try
If rst![PROJECT_NAME] = Me![Project_Name_Text] Then
 
Hi,

Try this:

If DCount ("*", "tblProjects", "ProjectName='" & me!Project_Name_Text & "'") = 1 Then
MsgBox "A project with this name already exists, please enter a unique name", vbInformation+vbOkOnly
Else
Set rst = dbs.OpenRecordset("tblPROJECTS")

With rst
.AddNew
![PROJECT_THEME_ID] = Me![Project_Theme_Choice]
![PROJECT_ID] = Nz(DMax("[PROJECT_ID]", "tblPROJECTS"), 0) + 1
![PROJECT_NAME] = Me![Project_Name_Text]
![PROJECT_DESCRIPTION] = Me![Project_Description_Text]
![PROJECT_START_DATE] = Me![Project_Start_Date_Value]
![PROJECT_END_DATE] = Me![Project_End_Date_Value]
![PROJECT_LEADER_ID] = Me![Project_Lead_Choice]
.Update
.Close
End With
MsgBox "* New project added to list. *"
End If

----- end code -----
I presume all your fieldnames are correct.
Regards

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top