Check bound textbox for duplicates (1 Viewer)

Atthe

Member
Local time
Today, 16:13
Joined
Oct 26, 2021
Messages
57
Hi All

I have a bound text box (Assembly_NumberSpec) and field (AssemblyNumberSpec) in tblAssemblies.

What I need to happen is after the user has enter data into (Assembly_NumberSpec) to check for any duplicates and in (AssemblyNumberSpec) in tblAssemblies
If duplicates are found I need to clear the textbox and re set the focus and display error msg 'This Assembly/Spec already exists'.

I have tried this but not had any success

Code:
If DCount("AssemblyNumberSpec", "[tblAssemblies]", "Assembly_NumberSpec = me.assembly_NumberSpec") > 0 Then
MsgBox "This Assembly/Spec already exists."

Any advice appreciated
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:13
Joined
Aug 30, 2003
Messages
36,125
Try

If DCount("AssemblyNumberSpec", "[tblAssemblies]", "Assembly_NumberSpec = " & me.assembly_NumberSpec) > 0 Then

This presumes that data type of the spec field is numeric
 

Atthe

Member
Local time
Today, 16:13
Joined
Oct 26, 2021
Messages
57
Try

If DCount("AssemblyNumberSpec", "[tblAssemblies]", "Assembly_NumberSpec = " & me.assembly_NumberSpec) > 0 Then

This presumes that data type of the spec field is numeric
Sorry forgot to specify the data field is short text
 

Atthe

Member
Local time
Today, 16:13
Joined
Oct 26, 2021
Messages
57
Tried this but getting couldn't find the field referred to in your expression

Code:
If DLookup("AssemblyNumberSpec", "tblAssemblies", "AssemblyNumberSpec = '" & forms!frmToolGroupManager!Assembly_NumberSpec & "'") Then

Also should I be putting the code in the before update event?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 08:13
Joined
Aug 30, 2003
Messages
36,125
You've switched from DCount() to DLookup(), and previously there was an underscore in the field name, so double check table/field/control spellings.

Yes, typically the before update event of a control or the form is the place to do validation.
 

LarryE

Active member
Local time
Today, 08:13
Joined
Aug 18, 2021
Messages
589
Tried this but getting couldn't find the field referred to in your expression

Code:
If DLookup("AssemblyNumberSpec", "tblAssemblies", "AssemblyNumberSpec = '" & forms!frmToolGroupManager!Assembly_NumberSpec & "'") Then

Also should I be putting the code in the before update event?
If you are looking for dup AssemblyNumberSpec in tblAssemblies then you should check dups when they are entered. So on the Before Update event:
Code:
If DCount("[AssemblyNumberSpec]", "tblAssemblies", "[AssemblyNumberSpec] = Forms![frmToolGroupManager]![Assembly_NumberSpec]") = 1 Then
    Msgbox "This is a duplicate Assembly Number"
    Me.AssemblyNumberSpec = Null
End If
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:13
Joined
Feb 19, 2002
Messages
43,264
Use the dCount() in the control's BeforeUpdate event so you can cancel the event. For some reason, Me.ControlName.Undo doesn't seem to be backing out the value in a control any more so you can use Me.ControlName = Me.ControlName.OldValue instead.
 

Users who are viewing this thread

Top Bottom