.ADDITEM

A

Alex

Hi...ok I am stump. I have two list boxes let's call
them, lstVendors and lstAddedVendors. I want to add items
in my lstVendors to my lstAddedVendors. I have the
command button cmdAddVendor. Now the current code I have
is Private Sub cmdAddVendor_Click()
Dim frm As Form, ctl As Control
Dim varItm As Variant
Set frm = Forms!ByCourse
Set ctl = frm!lstCourseName
Dim count As Integer
' loop through each item they've selected and add it
to 'added courses'
For Each varItm In ctl.ItemsSelected
lstAddedVendors.AddItem (ctl.ItemData(varItm))
Next varItm
ctl.Requery
End Sub

Now this is from another database, for some reason when I
click on the button..it goes error .AddItem, the method
doesn't exist. Now I believe that the method is private
stored in some other database or something. How would I
create my own function off AddItem?? I just want to add
one item from one list box to the next. I know it's
asking a lot, but I'm stumped would be great help.

-Thanks
..
 
R

Roger Carlson

Prior to Access XP (2002) there was no AddItem method for the Listbox
control. The only two ways to fill the listbox was to 1) bind it to a table
or query and 2) programmatically modify the RowSource property.

The following MS Knowledgebase article describes how to do it in Access 97
or 2000:
http://support.microsoft.com/default.aspx?kbid=124344
 
G

Guest

Thanks for the reply Roger, but unfortunately I have no
idea what those codes are saying. I'm not an expert. I
just want to be able to select the items in my list box 1
and click the command button to transfer it in list box
2. I already have the items in list box one..that's fine
it's just transferring it to list box 2....hmmmm? If
anyone can help me out with some simple coding that would
be great..ANYBODY =)
 
G

gandalf

You could try to set the value/text of listbox2 instead of
additem.
listbox2.value=...
If necessary use the before_update of after_update event
of listbox 2
 
B

Brendan Reynolds

The following example assumes that the Rowsource Type of the target list box
is Value List. List2 is the target list box (the list box we're copying to)
List0 is the source list box (the list box we're copying from).

Private Sub Command4_Click()

Dim varItm As Variant

Me.List2.RowSource = vbNullString
For Each varItm In Me.List0.ItemsSelected
Me.List2.RowSource = Me.List2.RowSource & Me.List0.ItemData(varItm)
& ";"
Next varItm
Me.List2.Requery

End Sub


--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
A

Alex

Say what...I tried a few things what you are suggesting.
but coming up with no results....ok can someone just do
out the code for me or attach word or something, instead
of hinting..that would be GREAT...lstVendors is my first
list box...I multi select and click on cmdAdd to add the
highlighted items into lstAddedVendors...Thanks.
 
G

Guest

THAT IS GREAT...THANK YOU SO MUCH...I'm guessing that
REMOVE ITEM works similarly?...THANKS AGAIN
 
G

Guest

Also...it's working but how would I add for example..say I
ransfer value Red to listbox 2...and then I want to add
Green...it replaces Red with Green..instead of just Adding
Green to the list box
 
B

Brendan Reynolds

The code I posted empties the target list box and fills it with all of the
items selected in the source list box. I did it that way so that the same
item would not get added twice. This way, if your users add Red and then
want to add Green, they have to hold down Ctrl while selecting Green so that
both Red and Green are selected. When they click the button, both Red and
Green will be added to the target listbox. Each time the user clicks the
button, any item not selected in the source listbox will be removed from the
target listbox, and any item that is selected will be added. Thus there is
no need for a separate 'RemoveItem' method - to remove an item, just
unselect it in the source listbox and click the button again.

If you don't empty the target list box, but add newly selected items to
those already added, then you'll have to write a bunch of code to check for
duplicates as well as writing a RemoveItem method. Me, I'm off home in about
five minutes, so if you really want to do it that way I'm afraid someone
else will have to help you with that! :)

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
G

Guest

Nevermind...somone suggested this
Private Sub Command4_Click()

Dim varItm As Variant

Me.List2.RowSource = vbNullString
For Each varItm In Me.List0.ItemsSelected
Me.List2.RowSource = Me.List2.RowSource &
Me.List0.ItemData(varItm)
& ";"
Next varItm
Me.List2.Requery

End Sub
Only problem is..If I add say RED...and then Want to Add
Green..it will replace red with green instead of Adding it
to the list box.
 
A

ALEX HELP

ANYONE WILLING TO HELP with the SECOND OPTION..reason is
the first list box..I have over 15,000 to chose from..and
I want the user to be able to search and added and
reserach ..I've done the search function for listbox
one..its just adding the item to listbox 2...on how he
suggested with the remove item..that would BE GREAT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top