Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Prevent duplicates from being entered into a listbox in Microsoft Access form

Hi all.

I have a textbox (myTextBox) where the user enters a number. They then click a button (myButton) and that in turns enters it into a listbox (myListBox). How do I prevent duplicates from being entered into the listbox?

So if the user enters: A
then B then A. It should not allow A to be entered a second time.

How can I do this? In Access 2003?

Thank you in advance.
0
printmedia
Asked:
printmedia
  • 3
  • 2
1 Solution
 
GRayLCommented:
You would be better to use a table to drive the listbox, rather than the semi-colon-separated list of variables.  You can make the field fldChoice in table tblChoices the primary key, and use your button to insert the value in myTextBox into the table.  If a duplicate is attempted, you will be warned accordingly and the insert will not happen.
0
 
Rey Obrero (Capricorn1)Commented:
try this codes

Private Sub myTextBox_AfterUpdate()
If InStr(Me.myListBox.RowSource, Me.myTextBox.Text) = 0 Then
    Me.myListBox.AddItem Me.myTextBox
End If
End Sub
0
 
Rey Obrero (Capricorn1)Commented:
better use this


Private Sub myTextBox_AfterUpdate()
If InStr(";" & Me.myListBox.RowSource & ";", ";" & Me.myTextBox.Text & ";") = 0 Then
    Me.myListBox.AddItem Me.myTextBox
   
End If

End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
printmediaAuthor Commented:
Thanks capricorn. What difference do the ";" make?
0
 
Rey Obrero (Capricorn1)Commented:
that will prevent excluding a part of the string to not be added to the list.
i.e., if you have added  ABC to the list, the ";" will alllow AB or BC to be added to the list
0
 
GRayLCommented:
It never ceases to amaze me why you and others would prefer a bunch of code when the inherent characteristics of the database are there to do what you want?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now