My Assistant
|
|
Mar 23 2009, 04:55 AM
Post
#1
|
|
|
UtterAccess Enthusiast Posts: 50 |
Hi everyone,
I have a form (frmAll) that populates the current date (text box = CurrentDate). This form is built off of a table called tblProjects. What I'm trying to do is prevent duplicate dates for an associate. The associate names are populated automatically on the form open procedure in a text box called AssociateName (same name in the table) and the CurrentDate (again, same name in the table) populates from a combo box called cmboProject. Let's say the associate fills out frmAll for 3/23/2009. What I'd like to do is if they tried to create a record again for the same date, a msg box would appear saying the date has already been used for that associate and that particular record would not be saved. I searched for quite a while on the forum and didn't see an answer to this specific issue so any assistance with this situation would be greatly appreciated. Thanks in advance. |
|
|
|
![]() |
Mar 23 2009, 06:32 AM
Post
#2
|
|
|
UtterAccess VIP Posts: 4,582 From: From Hawaii - Now in Wisconsin...Am I Nuts? |
You could handle it a few different ways but the best way would be to set a primary key on the associate and date fields, which would prevent duplicate values from being entered.
|
|
|
|
Mar 23 2009, 07:03 AM
Post
#3
|
|
|
UtterAccess Enthusiast Posts: 50 |
Thanks for the response. Unfortunately I need the PK set on something else. Is there a way to do this successfully w/o setting the field as the primary?
|
|
|
|
Mar 23 2009, 07:53 AM
Post
#4
|
|
|
UtterAccess VIP Posts: 4,582 From: From Hawaii - Now in Wisconsin...Am I Nuts? |
You could try something like this.
CODE Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim intCount As Integer intCount = DCount("*", "Table1", "[Associate] = '" & Me!Associate & "' AND " & "[DateEntered] = #" & Me.DateEntered & "#") If intCount > 1 Then MsgBox "This record has already been added." DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70 End If End Sub |
|
|
|
Mar 23 2009, 10:18 AM
Post
#5
|
|
|
UtterAccess VIP Posts: 23,583 From: Mississippi |
You could use the two fields in question as a Unique Values index.
|
|
|
|
Mar 23 2009, 11:08 AM
Post
#6
|
|
|
UtterAccess VIP Posts: 4,582 From: From Hawaii - Now in Wisconsin...Am I Nuts? |
Hi Frank,
I recommended that earlier but the user already has a field in that table that he needs to have set as the PK. |
|
|
|
Mar 23 2009, 12:03 PM
Post
#7
|
|
|
UtterAccess VIP Posts: 23,583 From: Mississippi |
But you can still do it, can't you Dan? It won't be the PK, but you can still use the fields in a unique index, can't you? Or am I all wet?
|
|
|
|
Mar 23 2009, 12:16 PM
Post
#8
|
|
|
UtterAccess VIP Posts: 5,278 From: Upstate NY, USA |
You're dry as a bone. Just about every table that uses an autonumber
for a primary key would benefit from having a unique index defined on the natural primary key. |
|
|
|
Mar 23 2009, 12:20 PM
Post
#9
|
|
|
UtterAccess VIP Posts: 4,582 From: From Hawaii - Now in Wisconsin...Am I Nuts? |
Frank,
You can assign each individual field as a unique field by itself but I don't know of any way that you would be able to combine those 2 fields as a unique index while you still have the existing PK. You would have to combine all of them together. I'm by no means an expert in all aspects of Access 2007. I don't get the chance to develop with it too often, so, I'm not saying that it might not be possible, I just don't know of a way to do it. Maybe, one of the MVPs or someone more familiar with Access 2007 then myself can help to answer that one. |
|
|
|
Mar 23 2009, 04:23 PM
Post
#10
|
|
|
UtterAccess VIP Posts: 23,583 From: Mississippi |
Well, it is possible of course that the current data will not allow it. But if you open the table in design view, and click the Indexes button, I think you can do it, even if data has been entered into the table.
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 18th May 2013 - 05:52 PM |