+ Reply to Thread
Results 1 to 3 of 3

Detect Duplicate Entry as soon as its typed into the Userform

  1. #1
    Registered User
    Join Date
    09-14-2015
    Location
    New Delhi
    MS-Off Ver
    2013
    Posts
    35

    Post Detect Duplicate Entry as soon as its typed into the Userform

    Dear All,

    I have developed a user form with multiple text boxes and combo boxes. Request your assistance in writing the VBA code, which detects and pops message of DUPLICATE ENTRY, as soon as something (say ID number) is typed into the first text box or when tab is pressed to move to the next text box.

    I am new to VBA coding, so I am referring and using codes published on different forums and websites. I have come across many codes, which requires a person to click on SUBMIT button to actually detect and pop out message of duplicate. But in case, there are several text boxes, then it becomes tedious to fill all the data and then be told that a duplicate entry exists. Its not a good user experience.

    Kindly help me out. Thanks in advance.

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Detect Duplicate Entry as soon as its typed into the Userform

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  3. #3
    Registered User
    Join Date
    09-14-2015
    Location
    New Delhi
    MS-Off Ver
    2013
    Posts
    35

    Re: Detect Duplicate Entry as soon as its typed into the Userform

    Hello Skywriter,

    I have attached the screen shot of the user form page and also for the database sheet, where the data inputted through user form will be updated.

    Due to company policies, I couldn't attach the entire file, as it has company data populated.

    Below I am pasting the commands which I have added for the user form functionality -



    The VBA code for adding new entry is --

    Private Sub cmdaddnewentry_Click()

    Dim nextrow As Range

    'set the next row in the database
    Set nextrow = Sheet2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0)

    'check for values in all the 25 controls
    For X = 1 To 25
    If Me.Controls("Reg" & X).Value = "" Then
    MsgBox "You must add all data"
    Exit Sub
    End If
    Next

    'check for duplicate service request numbers
    If WorksheetFunction.CountIf(Sheet2.Range("B:B"), Me.reg1.Value) > 0 Then
    MsgBox "This service request already exists"
    Exit Sub
    End If

    'number of controls to loop through
    cNum = 25

    'add the data to the database
    For X = 1 To cNum
    nextrow = Me.Controls("Reg" & X).Value
    Set nextrow = nextrow.Offset(0, 1)
    Next

    'clear the controls
    For X = 1 To cNum
    Me.Controls("Reg" & X).Value = ""
    Next


    Exit Sub

    End Sub



    The VBA code to search for data is as given below. The search result is displayed in a ListBox and then on double clicking, it gets filled in respective text and combo boxes.

    Private Sub cmdsearchsr_Click()

    Lookup

    End Sub

    Sub Lookup()

    'declare the variables
    Dim rngFind As Range
    Dim strFirstFind As String

    'clear the listbox
    lbsearchsr.Clear

    'look up based on service request number
    With Sheet2.Range("B:B")
    Set rngFind = .Find(txtsearch.Text, LookIn:=xlValues, lookat:=xlPart)

    'if value found then set a variable for the address
    If Not rngFind Is Nothing Then
    strFirstFind = rngFind.Address

    'add the values to the listbox
    Do
    If rngFind.Row > 1 Then
    lbsearchsr.AddItem rngFind.Value
    lbsearchsr.List(lbsearchsr.ListCount - 1, 1) = rngFind.Offset(0, 1)
    lbsearchsr.List(lbsearchsr.ListCount - 1, 2) = rngFind.Offset(0, 2)
    lbsearchsr.List(lbsearchsr.ListCount - 1, 3) = rngFind.Offset(0, 3)
    lbsearchsr.List(lbsearchsr.ListCount - 1, 4) = rngFind.Offset(0, 4)
    lbsearchsr.List(lbsearchsr.ListCount - 1, 5) = rngFind.Offset(0, 5)
    End If

    'find the next address to add
    Set rngFind = .FindNext(rngFind)
    Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstFind
    End If

    End With

    'disable data editing
    Me.reg1.Enabled = False
    Me.cmdeditentry.Enabled = False

    Exit Sub


    Private Sub lbsearchsr_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

    'declare the variables
    Dim cServiceRequestNumber As String
    Dim I As Integer
    Dim findvalue


    'get the select value from the listbox
    For I = 0 To lbsearchsr.ListCount - 1
    If lbsearchsr.Selected(I) = True Then
    cServiceRequestNumber = lbsearchsr.List(I, 0)
    End If
    Next I

    'find the service request number
    Set findvalue = Sheet2.Range("B:B").Find(What:=cServiceRequestNumber, LookIn:=xlValues).Offset(0, 0)

    'add the database values to the userform
    cNum = 25
    For X = 1 To cNum
    Me.Controls("Reg" & X).Value = findvalue
    Set findvalue = findvalue.Offset(0, 1)
    Next

    'disable adding
    Me.cmdaddnewentry.Enabled = False
    Me.cmdeditentry.Enabled = True

    Exit Sub

    End Sub


    Thanks for your support

    Note - The source of these codes are from - http://www.onlinepclearning.com by Trevor Easton
    Attached Images Attached Images

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Prevent duplicate entry in a userform by matching record to multiple columns.
    By rsbuslon in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-05-2023, 07:44 PM
  2. Replies: 4
    Last Post: 08-21-2015, 03:59 PM
  3. Replies: 0
    Last Post: 02-04-2014, 12:36 AM
  4. [SOLVED] Prevent duplicate entry in userform
    By vijaynadiad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-01-2013, 02:29 PM
  5. [SOLVED] How to avoid duplicate characters if only typed once?
    By bonniep53 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-01-2013, 07:09 AM
  6. userform duplicate entry check writing to sheet
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-06-2013, 06:21 AM
  7. [SOLVED] show message box when a duplicate entry is entered into a userform textbox
    By reggie1000 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-04-2012, 05:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1