Hi,
I would make one suggestion off subject just a bit. "Function" is a
reserved word in Access, you might consider changing the field name as
sometime in the future after you built everything it might eat your
lunch.
Duff
Sorry about being late with this reply, and yes, the form, ACCOUNTS, is
bound
to a table with the same name. Both having the following fields:
ACCOUNTS_ID ----- AutoNumber (primary key)
FUNCTION ---------- Text
OBJECT ------------- Text
ACCOUNT_NAME --- Text
Sort of in summary here --- in the table, ACCOUNTS, the fields FUNCTION
and
OBJECT have been "assigned" as a "unique index". In the form, ACCOUNTS,
the
text boxes FUNCTION and OBJECT are the ones that appear in that
BeforeUpdate
event here: (Note: I've replaced that 2nd Chr(13) with Chr(10) as you
had
recommended earlier)
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not IsNull(DLookup("[FUNCTION]", "ACCOUNTS", _
"[FUNCTION] = '" & Me.FUNCTION & "'AND [OBJECT] = '" & Me.OBJECT &
"'"))
Then
Cancel = True
strMsg = "This Account Already Exist!" & Chr(13) _
& Chr(10) & "TRY AGAIN!"
MsgBox strMsg
Me.FUNCTION.SetFocus
End If
End Sub
RC
:
Is it a bound form?
Steve
:
Thanks Steve for that advice with Chr(13) and Chr(10), and I did go
ahead and
use the following On Error event:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conDuplicateKey = 3022
Dim strMsg As String
If DataErr = conDuplicateKey Then
Response = acDataErrContinue
strMsg = "This Account Already Exist " & Chr(13) _
& Chr(13) & "Record Entry Will Not Be Saved."
MsgBox strMsg
Me.Undo
End If
End Sub
Plus there is a Unique Index set on the ACCOUNTS table for the
FUNCTION
and
OBJECT fields --- hope this isn't overkill.
After "testing" that DLookup code in the "If ...Then" statement
that's
being
used as a Before Update event, I'm now running into some "quirky
behavoir".
When I go back to edit any other field than the FUNCTION and OBJECT
fields,
on a record in that ACCOUNTS form, such as the ACCOUNT_NAME field,
the
code
returns the message as though a duplication has been made in the
FUNCTION and
OBJECT fields on that same record . I'm having to close to form to
break
free. It's as though when any editing to an older record starts to
update the
code sees the existing values in the FUNCTION and OBJECT fields as
being
"duplicated". Any ideas?
Thanks,
RC
:
It's good practice to restrict such things at the lowest level
first.
Then,
you can add code to trap it in the form if you wish. Your code is
fine but a
couple of things to mention...
You should use Chr(13) in combination with Chr(10) and in that
order.
A replacement for this is vbCrLf.
You may want to consider adding error handling to your procedure.
Steve
:
Here is something that I kept tinkering with until stumbling
across
the code
below appears to be working --- it appears to be stopping the
duplicated data
in it tracks at the from level before it updates at the table
level:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not IsNull(DLookup("[FUNCTION]", "ACCOUNTS", _
"[FUNCTION] = '" & Me.FUNCTION & "'AND [OBJECT] = '" &
Me.OBJECT
& "'"))
Then
Cancel = True
strMsg = "This Account Already Exist!" & Chr(13) _
& Chr(13) & "TRY AGAIN!"
MsgBox strMsg
Me.FUNCTION.SetFocus
End If
End Sub
I'm short on experience with code, and if this needs to be
"cleaned
up",
please holler back.
:
Thanks Steve, that works fine!
I was hoping that maybe there was a way to stop, or capture,
the
process of
entering any "duplicated data" at the form level before any
updating takes
place at the table level and uses up one of the AutoNumbers of
the Primary
Key --- sort of keeping the AutoNumbers matching the Record
Numbers, which is
probably being somewhat unrealistic here --- guess a fellow
can
always hide
that ID field that holds the AutoNumber and relieve undue
concern,huh
Thanks again Steve,
RC
:
The best way to prevent duplicates is to do it in the table.
At
least, that
is the first level you should look at...
Open your table in design view, then open your indexes.
Create
a new index
based on both fields and set the 'unique' property to 'Yes'.
To do this, in the first column, type a name for your new
index
then in the
column to the right select one of your fields, then in the
field immediately
below that, select your other field. Go back to the name of
the
new index
(select it) and look at the properties at the base of the
index
form. Set the
unique property.
Once you have done that, you will not be able to enter
duplicates of the
combination of those two fields.
Steve
:
I have a form named ACCOUNTS that is bound to a table that
is
also named
ACCOUNTS. The form is used only for entering data into the
ACCOUNTS table. In
the table there are two text fields, one named FUNCTION,
and
the other named
OBJECT. The field named FUNCTION can contain repetitions
of a
four character
code, and the field named OBJECT can contain repetitions
of a
five character
code. The two fields together should not contain the same
pair of codes such
as in rows 1 and 3 in the example below:
FUNCTION OBJECT
1) abcd wxyz
2) adcd hijk
3) abcd wxyz
I've tried DLookup and DCount in several attempts with If
...
Then
statements as a Before Update event, but keep getting
either
mismatch, or
runtime, or "End If without block If" type errors. Is
there
code that will
prevent such duplicates?