Results 1 to 8 of 8
  1. #1
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47

    Stopping duplicate entry in a field on a form

    I have used the code below to try and bring up a message box when I enter a duplicate number in the 'TFN' field but it stops me at the 'If' line.


    Code:
    Private Sub TFN_AfterUpdate()
    Dim NewTFN As String
    Dim stLinkCriteria As String
    NewTFN = Me.TFN.Value
    stLinkCriteria = "[TFN] = " & "'" & NewTFN & "'"
    If Me.TFN = DLookup("[TFN]", "tbl_SAlert", stLinkCriteria) Then
    MsgBox "This TFN " & NewTFN & ", has already been entered in the database." _
    & vbCr & vbCr & "Please check TFN again.", vbInformation, "Duplicate TFN entered"
    Me.Undo
    End If
    End Sub
    The table used with the form is titled "tbl_SAlert" and the field on the form is entitled "TFN".
    Could someone please advise where I am going wrong?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,437
    if it stops at the if line it comes up with an error message - what is the error message?

    And just to confirm (because I suspect this is where the problem is) what datatype is the field TFN in the table tbl_SAlert

  3. #3
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,808
    I have to ask:
    - why not just prevent this by setting table field properties to not allow dupes and forget the code?
    - if sticking with the approach anyway, this sort of thing belongs in a BeforeUpdate event so it can be cancelled, not AfterUpdate. IMHO, it is better to not write to a table than write to it and undo the entry. Also, assigning the form control data to a variable (thereby having two string variables) then having the variable in the lookup function is a bit superfluous
    Code:
    Private Sub TFN_BeforeUpdate(Cancel As Integer)
    If Me.TFN = DLookup("[TFN]", "tbl_SAlert", "[TFN]='" & [TFN] & "'" ) Then
      MsgBox "This TFN " & Me.TFN & ", has already been entered in the database." _
      & vbCr & vbCr & "Please check TFN again.", vbInformation, "Duplicate TFN entered"
      Cancel = True
    End If
    
    End Sub
    It also appears that poster has controls and fields with the same name. This can cause issues when Access doesn't properly resolve which is being referred to. What usually causes this is building forms/reports with design wizards (which is fine) but not using conventional names to re-name these controls.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47
    Sorry for the late response.
    The error message is Run-time ERROR '3464': Data type mismatch in criteria expression

    The datatype is 'Number'

    I wanted the code because I don't want the operative entering in other data after the TFN field and then be told the number is a duplicate.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,437
    As I thought

    this is the line casue the problem - you are trying to compare a text value to a numeric value

    stLinkCriteria = "[TFN] = " & "'" & NewTFN & "'"

    it should be

    stLinkCriteria = "[TFN] = " & NewTFN

  6. #6
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47
    Thank you so much Ajax.

    This has helped me so much.

  7. #7
    Micron is online now Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,808
    Quote Originally Posted by coach32 View Post
    I wanted the code because I don't want the operative entering in other data after the TFN field and then be told the number is a duplicate.
    FYI: table or control level validation would accomplish the same thing without code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    coach32 is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2011
    Posts
    47
    Thank-you Micron. Appreciate the guidance.
    That's why I ask you guys, you know your stuff.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 12-21-2015, 10:57 PM
  2. Replies: 3
    Last Post: 05-28-2015, 10:01 AM
  3. Replies: 5
    Last Post: 03-09-2014, 07:16 PM
  4. Duplicate data entry on form level by user
    By Kananelo in forum Forms
    Replies: 1
    Last Post: 02-10-2012, 01:09 AM
  5. Replies: 3
    Last Post: 06-04-2010, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums