Results 1 to 4 of 4
  1. #1
    New.User is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    37

    Text box undo not working if there is a duplicate.

    Hello All,
    I am having a form with a textbox and a list box. I want to enter a new record into the textbox to populate the table and listbox (listbox row source is the table). I have written a code to prevent duplicate entry into the table. When there is a duplicate entry I am getting a pop up to alert the user. What is not working is the undo option to clear the textbox. The code is pasted below with some required info.Any help with the code will be appreciated.
    Cross posted by my partner https://stackoverflow.com/questions/...is-a-duplicate.

    Table name: tblNewComponents
    Field name: NewComponents
    Textbox name: TextCOMPONENTS

    Private Sub TextCOMPONENTS_AfterUpdate()
    Dim NewComponent As String
    Dim stLinkCriteria As String
    Dim custNo As Integer


    'Assign the entered customer name to a variable NewCustomer


    NewComponent = Me.TextCOMPONENTS.Value
    stLinkCriteria = "[NewComponents] = " & "'" & NewComponent & "'"
    If Me.TextCOMPONENTS = DLookup("[NewComponents]", "tblNewComponents", stLinkCriteria) Then
    MsgBox "This Component, " & NewComponent & ", has already been entered in database." _
    & vbCr & vbCr & "Please check the component name again.", vbInformation, "Duplicate information"

    Me.Undo
    'undo the process and clear all fields
    End If
    End Sub

    Regards
    New User


  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,979
    Prevention is better than cure
    Use the before update event to check for and block any duplicates BEFORE these are written to the database

    Or you could also index your NewComponent field and set as No Duplicates
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    New.User is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    37
    Hello again Ridders,
    I tried repeating for Before Update but it doesnt work, gives the same result as After Update. I wonder if I have made any mistake in the code. I am a novice and experimenting so apologize if there is any silly mistake in the code.

  4. #4
    New.User is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    37
    It is working now after I added the below code instead of undo.
    Me.TextCOMPONENTS.Value = Null
    Me.TextCOMPONENTS.SetFocus

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

Similar Threads

  1. Remove Duplicate VBA code not working
    By jyellis in forum Programming
    Replies: 3
    Last Post: 02-08-2018, 05:54 PM
  2. Replies: 4
    Last Post: 08-27-2017, 08:21 AM
  3. Replies: 13
    Last Post: 01-22-2015, 05:27 PM
  4. Group duplicate text fields as one?
    By jset818 in forum Queries
    Replies: 13
    Last Post: 10-24-2014, 01:05 PM
  5. Duplicate Text
    By Tlattimer in forum Forms
    Replies: 2
    Last Post: 07-17-2013, 08:32 AM

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