+ Reply to Thread
Results 1 to 4 of 4

How to avoid duplicate entry in userform

Hybrid View

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    How to avoid duplicate entry in userform

    Hello

    My userform displays the records when loaded or while navigating the records.Problem is if user clicks on the Save button the form enters the record again. I mean that record is already there in the sheet but as it displays the records from sheet. it also saves them again if the Save button is clicked How can I avoid to enter a duplicate record.The code is bellow:
    Option Explicit
    'Flag used to Enable or Disable UserForm Events
    Private bGblInhibitUserForm1Events As Boolean
    Private CurrentRow As Long
    
    Private Sub cmdAdd_Click()
    
    Dim iRow As Long
    Sheets("sheet1").Activate
    iRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
                    Cells(iRow, 1) = TextBox1.Value
                    Cells(iRow, 2) = TextBox2.Value
                    Cells(iRow, 3) = TextBox3.Value
                                    TextBox1.Value = ""
                                    TextBox2.Value = ""
                                    TextBox3.Value = ""
    End Sub
    
    Private Sub cmdClose_Click()
    Unload Me
    End Sub
    
    Private Sub cmdNew_Click()
    
                    TextBox1.Value = ""
                    TextBox2.Value = ""
                    TextBox3.Value = ""
                    ImgData.Visible = False
                    LabelStatus.Caption = ""
    End Sub
    Private Sub SpinButton1_Change()
    If SpinButton1.Value = 0 Then SpinButton1.Value = 10
                    ListBox1.ListIndex = ListBox1.ListCount - SpinButton1.Value
                    Label11.Caption = SpinButton1.Value
    
    End Sub
    
    Private Sub UserForm_Initialize()
                    SpinButton1.Max = ListBox1.ListCount
                    SpinButton1.Min = 0
                    SpinButton1.Value = 10
                    CurrentRow = 2
    Call Update_Form
                    LabelStatus.Caption = ""
                         
    End Sub
    Private Sub cmdPre_Click()
            Dim iMinimumAllowedRowNumber As Long
            iMinimumAllowedRowNumber = 2
            CurrentRow = CurrentRow - 1
    If CurrentRow <= iMinimumAllowedRowNumber Then
                    CurrentRow = iMinimumAllowedRowNumber
                    MsgBox "This is the first Record."
            End If
            Call Update_Form
            
            ImgData.Visible = True
    End Sub
    
    Private Sub cmdNext_Click()
    
    Dim iMaximumAllowedRowNumber As Long
            iMaximumAllowedRowNumber = Range("list").Rows.Count
            ImgData.Visible = True
            CurrentRow = CurrentRow + 1
    If CurrentRow >= iMaximumAllowedRowNumber Then
            CurrentRow = iMaximumAllowedRowNumber
            MsgBox "This is the Last Available Record."
    End If
            Call Update_Form
    End Sub
    
    Private Sub cmdFirst_Click() 'FIRST RECORD BUTTON
    'Calculate the First Row Number
            CurrentRow = Range("list").Row + 1
            ImgData.Visible = True
            Call Update_Form
            LabelStatus.Caption = ""
    
    End Sub
    
    Private Sub cmdLast_Click() 'LAST RECORD BUTTON
            'Calculate the Last Row Number
            CurrentRow = Range("list").Rows.Count
            ImgData.Visible = True
    Call Update_Form
            LabelStatus.Caption = "This is the Last Available Record."
    End Sub
    
    
    Private Sub ListBox1_Click()
            Dim iListIndex As Long
            'Do nothing if 'UserForm Events' are disabled
    If bGblInhibitUserForm1Events = False Then
            iListIndex = ListBox1.ListIndex
            ImgData.Visible = True
    If iListIndex <> -1 Then
            CurrentRow = iListIndex + 1
    Call Update_Form
    End If
    End If
    End Sub
    Sub Update_Form()
            Dim fPath As String
            Dim sClass As String
            Dim sFullName As String
            Dim sRoll As String
            sFullName = Worksheets("Sheet1").Cells(CurrentRow, "A").Value
            sClass = Worksheets("Sheet1").Cells(CurrentRow, "B").Value
            sRoll = Worksheets("Sheet1").Cells(CurrentRow, "C").Value
    If Len(sFullName) = 0 Then
        MsgBox "Data Integrity Error - No Name in Column 'A' of Row " & CurrentRow
        Debug.Assert False
        Exit Sub
        End If
    TextBox1.Value = sFullName
    TextBox2.Value = sClass
    TextBox3.Value = sRoll
        'Calculate the ListIndex (inhibit 'UserForm' Events when the 'ListIndex' Changes)
        bGblInhibitUserForm1Events = True
        UserForm1.ListBox1.ListIndex = CurrentRow - 1
        bGblInhibitUserForm1Events = False
        fPath = ThisWorkbook.Path & "\"
    If Dir(fPath & sFullName & ".jpg") <> "" Then
        ImgData.Picture = LoadPicture(fPath & sFullName & ".jpg")
    Else
        ImgData.Picture = LoadPicture(fPath & "Capture.jpg")
    End If
    End Sub
    Best Regards
    Imran Bhatti
    Teach me Excel VBA

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: How to avoid duplicate entry in userform

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.
    If posting code please use code tags, see here.

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: How to avoid duplicate entry in userform

    Excel file is attached here. You need to put some JPG photos (name accrding to column A in order to view pictures in the image control.
    Attached Files Attached Files

  4. #4
    Forum Expert gmr4evr1's Avatar
    Join Date
    11-24-2014
    Location
    Texas
    MS-Off Ver
    Office 2010 and 2007
    Posts
    3,448

    Re: How to avoid duplicate entry in userform

    You could maybe disable (hide) the Save button when certain codes run. I do that with my Edit and Update buttons.
    1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG
    You don't have to add Rep if I have helped you out (but it would be nice), but please mark the thread as SOLVED if your issue is resolved.

    Tom

+ 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. Replies: 4
    Last Post: 08-21-2015, 03:59 PM
  2. Replies: 0
    Last Post: 02-04-2014, 12:36 AM
  3. how to avoid duplicate entry in excel from list entries
    By azeemsarwar in forum Excel General
    Replies: 3
    Last Post: 01-02-2013, 06:22 AM
  4. avoid duplicate when entry from list
    By corescript in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-05-2012, 02:45 AM
  5. Excel 2007 : How to avoid duplicate entry
    By suhail in forum Excel General
    Replies: 4
    Last Post: 10-24-2010, 10:17 PM
  6. [SOLVED] i maintain DB of URLs in Excel. how can i avoid duplicate entry?
    By Ajesh in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-04-2006, 12:55 PM
  7. Replies: 1
    Last Post: 03-25-2006, 09:45 PM

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