Connecting Tech Pros Worldwide Forums | Help | Site Map

Code for Duplicate Records Warning

Peter
Guest
 
Posts: n/a
#1: Nov 13 '05
Can anyone advise me what is wrong with this code? I want it to tell me if
I am entering a duplicate record. However, I always get the "Dupe" error
message, whether I have entered a duplicate or not.

The field REGISTRATION1 is in the form [Aircraft Registrations] and
Registration is the Control Source in the table [Aircraft Registrations]
(Can't remember why I named the two fields differently, but there was a
reason at the time)
thanks
Peter

Private Sub REGISTRATION1_AfterUpdate()

Dim strWhere As String

With Me.REGISTRATION1
If .Value = .OldValue Then
'do nothing
Else
strWhere = "Registration1 = """ & .Value & """"
If Not IsNull(DLookup("Registration", "[Aircraft
Registrations]", strWhere)) Then
MsgBox "Dupe!"
End If
End If
End With
End Sub



Bob Quintal
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Code for Duplicate Records Warning


"Peter" <peter@_nospam_exeter23.freeserve.co.uk> wrote in
news:10vast2s8gj31fd@corp.supernews.com:
[color=blue]
> Can anyone advise me what is wrong with this code? I want it
> to tell me if I am entering a duplicate record. However, I
> always get the "Dupe" error message, whether I have entered a
> duplicate or not.
>
> The field REGISTRATION1 is in the form [Aircraft
> Registrations] and Registration is the Control Source in the
> table [Aircraft Registrations] (Can't remember why I named the
> two fields differently, but there was a reason at the time)
> thanks
> Peter
>
> Private Sub REGISTRATION1_AfterUpdate()
>
> Dim strWhere As String
>
> With Me.REGISTRATION1
> If .Value = .OldValue Then
> 'do nothing
> Else
> strWhere = "Registration1 = """ & .Value & """"
> If Not IsNull(DLookup("Registration", "[Aircraft
> Registrations]", strWhere)) Then
> MsgBox "Dupe!"
> End If
> End If
> End With
> End Sub
>[/color]
strWhere = "Registration1 = """ & .Value & """"
should be
strWhere = "Registration = """ & .Value & """"


--
Bob Quintal

PA is y I've altered my email address.
Chris2
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Code for Duplicate Records Warning





"Peter" <peter@_nospam_exeter23.freeserve.co.uk> wrote in message
news:10vast2s8gj31fd@corp.supernews.com...[color=blue]
> Can anyone advise me what is wrong with this code? I want it to tell me[/color]
if[color=blue]
> I am entering a duplicate record. However, I always get the "Dupe" error
> message, whether I have entered a duplicate or not.
>
> The field REGISTRATION1 is in the form [Aircraft Registrations] and
> Registration is the Control Source in the table [Aircraft Registrations]
> (Can't remember why I named the two fields differently, but there was a
> reason at the time)
> thanks
> Peter
>
> Private Sub REGISTRATION1_AfterUpdate()
>
> Dim strWhere As String
>
> With Me.REGISTRATION1
> If .Value = .OldValue Then
> 'do nothing
> Else
> strWhere = "Registration1 = """ & .Value & """"[/color]

I don't think this is written correctly . . . but am not sure. It looks
like you're trying to construct a string that will evaluate to
"Registration1 = ########", where ######## is some unknown number (or maybe
characters, I've no idea). Is that actually what the contents of the
Registration column of table [Aircraft Registrations] looks like?
[color=blue]
> If Not IsNull(DLookup("Registration", "[Aircraft
> Registrations]", strWhere)) Then[/color]

Let's see if I understand correctly. If DLookup returns Data, IsNull
evaluates True, which causes the If Not to evaulate False, falling out. If
DLookup returns Null, IsNull evaluates False, which cases the If Not to
evaluate True, hitting the MsgBox?

The question then becomes, does strWhere actually have data in it when it
hits DLookup, and (based on the above), does that data actually look like
what is in the table?


Sincerely,

Chris O.




[color=blue]
> MsgBox "Dupe!"
> End If
> End If
> End With
> End Sub
>
>[/color]


Chris2
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Code for Duplicate Records Warning


> >[color=blue][color=green]
> > With Me.REGISTRATION1
> > If .Value = .OldValue Then
> > 'do nothing
> > Else
> > strWhere = "Registration1 = """ & .Value & """"[/color]
>
> I don't think this is written correctly . . . but am not sure. It[/color]
looks[color=blue]
> like you're trying to construct a string that will evaluate to
> "Registration1 = ########", where ######## is some unknown number[/color]
(or maybe[color=blue]
> characters, I've no idea). Is that actually what the contents of[/color]
the[color=blue]
> Registration column of table [Aircraft Registrations] looks like?[/color]

Nevermind, of course a DLookup is supposed to appear that way.


Tim Marshall
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Code for Duplicate Records Warning


Peter wrote:
[color=blue]
> Can anyone advise me what is wrong with this code? I want it to tell me if
> I am entering a duplicate record.[/color]

Why don't you just take the field in question and put an index on it
with no duplicates permitted? YOu can then trap the error which would
be, I think, 3022. In the on error event of your form, do this:

Private Sub Form_Error(DataErr As Integer, Response As Integer)

Select Case DataErr

Case 3022 'duplicate value in index/PK

MsgBox "You're an idiot, you've already entered this " & _
"registration number!", vbExclamation, "Twit!"

Response = acDataErrContinue

Case Else

Response = acDataErrDisplay 'shows normal error message

End Select

End Sub

It truly is the least worrisome approach to have as much of your data
constraints as possible as part of your table design (in this case,
indexing the registration number). If you have to depend a lot on code
in your forms to constrain your data, you will have loops holes and
bugs, plus anyone who, for whatever reason, may have to enter data
directly to the tables (perhaps you, as the system administrator) is
bound to forget about form based constraints eventually and end up with
bad data. With constraints on your tables, the limitations are
fundamental to your design.

Thus Spake E.F.Codd
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Peter
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Code for Duplicate Records Warning


Thanks.
Yes you were right. Works fine now.
Peter
"Bob Quintal" <rquintal@sPAmpatico.ca> wrote in message
news:1106609575.8c0f86030d5a1589fa4a83efd6f18332@t eranews...[color=blue]
> "Peter" <peter@_nospam_exeter23.freeserve.co.uk> wrote in
> news:10vast2s8gj31fd@corp.supernews.com:
>[color=green]
>> Can anyone advise me what is wrong with this code? I want it
>> to tell me if I am entering a duplicate record. However, I
>> always get the "Dupe" error message, whether I have entered a
>> duplicate or not.
>>
>> The field REGISTRATION1 is in the form [Aircraft
>> Registrations] and Registration is the Control Source in the
>> table [Aircraft Registrations] (Can't remember why I named the
>> two fields differently, but there was a reason at the time)
>> thanks
>> Peter
>>
>> Private Sub REGISTRATION1_AfterUpdate()
>>
>> Dim strWhere As String
>>
>> With Me.REGISTRATION1
>> If .Value = .OldValue Then
>> 'do nothing
>> Else
>> strWhere = "Registration1 = """ & .Value & """"
>> If Not IsNull(DLookup("Registration", "[Aircraft
>> Registrations]", strWhere)) Then
>> MsgBox "Dupe!"
>> End If
>> End If
>> End With
>> End Sub
>>[/color]
> strWhere = "Registration1 = """ & .Value & """"
> should be
> strWhere = "Registration = """ & .Value & """"
>
>
> --
> Bob Quintal
>
> PA is y I've altered my email address.[/color]


Bob Quintal
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Code for Duplicate Records Warning


"Peter" <peter@_nospam_exeter23.freeserve.co.uk> wrote in
news:10vda7f1n4fbna1@corp.supernews.com:
[color=blue]
> Thanks.
> Yes you were right. Works fine now.
> Peter[/color]

See Tim Marshal's reply. He makes a valid point about table level
constraints.

Bob
[color=blue]
> "Bob Quintal" <rquintal@sPAmpatico.ca> wrote in message
> news:1106609575.8c0f86030d5a1589fa4a83efd6f18332@t eranews...[color=green]
>> "Peter" <peter@_nospam_exeter23.freeserve.co.uk> wrote in
>> news:10vast2s8gj31fd@corp.supernews.com:
>>[color=darkred]
>>> Can anyone advise me what is wrong with this code? I want
>>> it to tell me if I am entering a duplicate record. However,
>>> I always get the "Dupe" error message, whether I have
>>> entered a duplicate or not.
>>>
>>> The field REGISTRATION1 is in the form [Aircraft
>>> Registrations] and Registration is the Control Source in the
>>> table [Aircraft Registrations] (Can't remember why I named
>>> the two fields differently, but there was a reason at the
>>> time) thanks
>>> Peter
>>>
>>> Private Sub REGISTRATION1_AfterUpdate()
>>>
>>> Dim strWhere As String
>>>
>>> With Me.REGISTRATION1
>>> If .Value = .OldValue Then
>>> 'do nothing
>>> Else
>>> strWhere = "Registration1 = """ & .Value & """"
>>> If Not IsNull(DLookup("Registration", "[Aircraft
>>> Registrations]", strWhere)) Then
>>> MsgBox "Dupe!"
>>> End If
>>> End If
>>> End With
>>> End Sub
>>>[/color]
>> strWhere = "Registration1 = """ & .Value & """"
>> should be
>> strWhere = "Registration = """ & .Value & """"
>>
>>
>> --
>> Bob Quintal
>>
>> PA is y I've altered my email address.[/color]
>
>[/color]



--
Bob Quintal

PA is y I've altered my email address.
Closed Thread