Share via

Prevent adding duplicate values from text box to listbox

Anonymous
2018-02-24T13:50:49+00:00

Hello,

Would you please support me to find vba code to prevent adding duplicate values from textbox to listbox so after print enter if this value in the listbox to give me massage and stop the funcation and if not to add it in the listbox .

thx,

Sherif

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
2018-02-25T05:43:45+00:00

I created a generic function ListboxHasValue that you can add to your toolbox. Needs more testing, but this is the basics.

Private Sub TXTWO_AfterUpdate()

    If ListboxHasValue(Me.LstWOs, Me.TXTWO) Then

        MsgBox "You Can't Add Same WO Twice", vbCritical, "Duplicate Records"

    Else

        Me.LstWOs.AddItem Me.TXTWO

        Me.TXTWO = Null

    End If

End Sub

Public Function ListboxHasValue(ByRef lst As ListBox, ByVal varValue As Variant) As Boolean

    Dim i               As Integer

    Dim blnReturn       As Boolean

    For i = 0 To lst.ListCount - 1

        If lst.ItemData(i) = varValue Then

            blnReturn = True

            Exit For

        End If

    Next i

    ListboxHasValue = blnReturn

End Function

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

10 additional answers

Sort by: Most helpful
  1. Anonymous
    2018-02-24T22:55:02+00:00

    as you see in this picture for the form I'm working on , I want to prevent adding duplicate values,  I add this value through entry in above text box .

    thanks for your valued time

    Was this answer helpful?

    0 comments No comments
  2. ScottGem 68,830 Reputation points Volunteer Moderator
    2018-02-24T22:26:46+00:00

    Sorry, but I'm not following what you need. Could you explain in more detail.

    A listbox is generally based on a table. So if you don't want to add a value entered into a textbox into the listbox, then set the field to No duplicates.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2018-02-24T20:07:49+00:00

    Unfortunately, Nothing

    Was this answer helpful?

    0 comments No comments
  4. Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
    2018-02-24T15:46:09+00:00

    What do you have so far?

    Was this answer helpful?

    0 comments No comments