limiting one item being chosen at one time

goldstar19821982

Registered User.
Local time
Yesterday, 16:28
Joined
Feb 17, 2008
Messages
78
Hello All,

I have been trying to resolve a running query

what is happenin is i currently have an order form where i can select an item from a list and press ok this would drop the item within a listbox.
Then if i want to select another item i select the drop down list again and this would show me all the items again to be selected.
I want to limit it so each item can only be selected once, for example
itemID= 01 then next time i select on the list this item would not be ther for this order but would appear for the next order again;

Any help would be much appreciated
 
Simple Software Solutions

There are two approaches you could take

1. either make the list box a multi select list and the user selects all the
items they need. Then use the ListBox.Selected(nIndex) For Next loop to add mutiple items in one sweep.

Or

2. use the Me.ListBox.RemoveListItem(nIndex) facility after they have selected an item.


Code Master::cool:http://www.icraftlimited.co.uk
 
Hi,

what i have is an orderline where items are selected and when select ok this displays in the listbox.
What i want to happen is say item 1 is selected, this will then display within the listbox. if i want to select another item 1 should not allow to be selected. just the remaining items.

Me.ListBox.RemoveListItem(nIndex)

ive never used this before and im a bit confused. removelistitem what does that refer to? and (nindex)?

Thanks
 
Simple Software Solutions

Right

Try this for size...

You have one listbox/combobox that contains ALL the items available for selection.

You then have a list box containing all the items the user has selected.

When the user selects an item from the first list/combo they click on a button > to confirm that this is the item I want.

On the OnClick Event of the button it loops through all the items in the selected list

(This list must be a value list with two columns, one for the code and one for the description)


For X = 0 to Me.ListBox.ListCount -1

If Me.ListBox.Column(0) = Me.Combo.Column(0) Then

Msgbox "Cannot select an item more than once"
Else

Me.ListBox.RowSource = Me.ListBox.Rowsource & Me.ComboBox.Column(0) & "," & Me.ComboBox.Column(1)

End If

Next


Essentially it compares the code of the one chosen against all the ones selected and if it encounters a duplicate it tells the user, otherwise it adds the code and description to the row source string.

Syntax may refining but the logix is correct.


Alternatively as I suggested make the first list a multi select and loop through all the selected items and populate the selected list box with the results
 
Hi,

what i have is an orderline where items are selected and when select ok this displays in the listbox.
What i want to happen is say item 1 is selected, this will then display within the listbox. if i want to select another item 1 should not allow to be selected. just the remaining items.

Me.ListBox.RemoveListItem(nIndex)

ive never used this before and im a bit confused. removelistitem what does that refer to? and (nindex)?

Thanks

Basically, if you have a list box that has 1 column, and multiple values like;
Red
Blue
Green

you can use RemoveListItem to take out whatever was clicked
Me.ListBox.RemoveListItem("Blue")
and using your existing code move that list item over to the other list.

But it would be just as easy to make your list box a multi-select, then cycle through each item and see if it is selected or not.

Code:
Dim varItem As Variant

For Each varItem In Me.ListBox1.ItemsSelected
    Me.ListBox2.AddItem Item:=Me.lst_Files.ItemData(varItem)
    Me.ListBox1.RemoveItem varItem
Next varItem

This will likely do what you're wanting - provided you are using 2 listboxes, rather than having a combo box out there some where.

Hi,

what i have is an orderline where items are selected and when select ok this displays in the listbox.
What i want to happen is say item 1 is selected, this will then display within the listbox. if i want to select another item 1 should not allow to be selected. just the remaining items.

Me.ListBox.RemoveListItem(nIndex)

ive never used this before and im a bit confused. removelistitem what does that refer to? and (nindex)?

Thanks

Basically, if you have a list box that has 1 column, and multiple values like;
Red
Blue
Green

you can use RemoveListItem to take out whatever was clicked
Me.ListBox.RemoveListItem("Blue")
and using your existing code move that list item over to the other list.

But it would be just as easy to make your list box a multi-select, then cycle through each item and see if it is selected or not.

Code:
Dim varItem As Variant

For Each varItem In Me.ListBox1.ItemsSelected
    Me.ListBox2.AddItem Item:=Me.lst_Files.ItemData(varItem)
    Me.ListBox1.RemoveItem varItem
Next varItem

This will likely do what you're wanting - provided you are using 2 listboxes, rather than having a combo box out there some where.

Update: I just ran this code in a project I've been doing and never bothered removing the items from my list before now, but when I did try this code that I posted, it only moves the first item selected. If your 1st listbox is NOT set to multi-select then this won't be a problem ... if anyone out there has any ideas as to how to get the code to cycle through all selected items, if you see what I'm missing, I'd greatly appreciate the help.
 
Last edited:
can yuo really use removeitem to edit list boxes in this way?

even if the data is bound?
 
can yuo really use removeitem to edit list boxes in this way?

even if the data is bound?

I actually just received a sample database where the person sent me a bound listbox and no, RemoveItem has to be used on a value list. What you can do is create a RecordSet in VBA code and populate your listbox with the same records that are bound to the control. Then if you want to remove items but not delete them, no big deal, but if you want to also delete items from the underlying recordset you could do that as well with a docmd.runsql "Delete * from someTable where somefield = '" & listbox.selecteditem(IndexValue) & "'"

Then do a lstbox.refresh (unless you do the removeitem, then a refresh isn't neessary).
 

Users who are viewing this thread

Back
Top Bottom