Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2011

    Unanswered: Prevent Adding Duplicates to Listbox

    Hi there,

    I have a combobox named "cboAuditors" and a listbox named "lstAuditors". When I choose an auditor from the combobox, I click a button to add them to the listbox. However, I am allowed to add duplicates right now. Is there a vba code to prevent the duplication?

    Thank You!!

  2. #2
    Join Date
    Dec 2004
    Madison, WI
    Setting the primary key on the field you don't want to duplicate.

    Otherwise, error trapping (perhaps write a function that passed the autonumber and sees if it's returned (true/false)

    Function CheckForDuplicate(IDAutoNumberField as variant) as boolean
    dim rs as adodb.recordset
    set rs = new adodb.recordset
    dim strSQL as string
    strSQL = "Select * from MyTableName where MyIDAutonumberField = " & IDAutoNumberField & "" strsql,currentproject.connection, adopenkeyset, adlockreadonly
    if rs.eof and rs.bof the
    msgbox "There are no records."
    CheckForDuplicate = fase
    msgbox "There are records."
    CheckForDuplicate = true
    end if
    set rs = nothing
    End Function
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

Posting Permissions

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