Listbox .additem (1 Viewer)

trackdaychamp

Access Guy
Local time
Today, 22:08
Joined
Jan 22, 2007
Messages
26
Hi,

How do I populate 2 columns to the listbox with the AddItem function?

This is what I have been doing. I know that it prob has something to do with the list item but I haven't been able to crack it.



lstProjects.AddItem (rs.Fields("ProjectID"))
lstProjects.AddItem (rs.Fields("ProjectName"))

Any suggestions much appreciated

Thanks,

Mark
 

boblarson

Smeghead
Local time
Today, 14:08
Joined
Jan 12, 2001
Messages
32,059
The listbox in Access doesn't work the same way as the listbox in VB. To set the listbox values in VBA, you need to either set your rowsource to a query (preferred method for dynamic data) or a value list. I'm not sure exactly how you would set multiple columns for a value list, but with a query you can set however many columns you want, within limits. You can select the columns you want in the query and then you can set the number of columns and column widths to set whether they show or not, but even if hidden (by setting the column width to 0") they can be referenced by using Me.YourListBoxName.Column(1) {the numeral one means the 2nd column, in this case as it is zero based}.
 

trackdaychamp

Access Guy
Local time
Today, 22:08
Joined
Jan 22, 2007
Messages
26
When I tried to put in the listindex, like you said, 0 for the first column and 1 for the second I got the following

Compile error: Expected =

Me.LstProjects.AddItem(rs.Fields("ProjectID"), 0)
Me.LstProjects.AddItem(rs.Fields("ProjectName"), 1)

Any ideas?? :confused:

Mark
 

boblarson

Smeghead
Local time
Today, 14:08
Joined
Jan 12, 2001
Messages
32,059
As mentioned, you can't use AddItem like you can in VB. It just doesn't work. In order to use AddItem you need:
Quote from the Help File
The combo box control must be a custom control and must be a drop-down list box or a combo box.
 

RoyVidar

Registered User.
Local time
Today, 23:08
Joined
Sep 25, 2000
Messages
805
For .AddItem, I think you need to give it the "whole row", delimited by semicolon (check out the Remarks section in the help file for the AddItem method), i e

Me!LstProjects.AddItem rs.Fields("ProjectID") & ";" & rs.Fields("ProjectName"), 0

Passing the index, is optional. If you don't specify index, it will add at the end of the list.

Note, though that the .AddItem/.RemoveItem methods became available in the 2002 (xp) version, same goes for the below recordset method, I think.

You can also assign the recordset directly to the list

With Me!LstProjects
.RowSourceType = "Table/Query"
set .Recordset = rs
end with

For ADO, I think the recordset must be clientside (which would coerce the cursor to static, I believe), using ADO, you could also use the .GetString method (should work also with the 2000 version, though the 2048 character limit of rowsources might sometimes provide a bit of inconvenience)

With Me!LstProjects
.RowSourceType = "Value list"
.RowSource = rs.GetString(adClipString, , ";", ";")
end with

Is there any reason you can't use a query/dynamic SQL as rowsource?
 

armychick86

Registered User.
Local time
Today, 16:08
Joined
Feb 11, 2007
Messages
25
While we are on the topic....

I am trying to do something similiar to trackdaychamp.

I am building a computer tracking database which includes the software/hardware it is equiped with. In my Software tab I have included a listbox, unbound, with buttons underneath, delete and add. Next to those buttons is a text box. When I type a value in the text box I would like to click the add button to add that value into the listbox. I have tryed using the .AddItem method but I can't seem to get it to work, because of my lack of knowledge, i'm stuck.

There might be a better way to achieve my goal however... I don't have to work with Access very often and only know how to code what I need, and that is basically cut, paste, edit. If anybody has anyother suggestions to list the software on each computer I would be happy to hear (read) them.

This is what I have:

Private Sub cmdAddSoftware_Click()

'add comment/task to list box
lstSoftware.AddItem (txtAddSoftware)

'clear AddComment box since you just added it
txtAddSoftware = ""

Exit Sub

End Sub
 

RoyVidar

Registered User.
Local time
Today, 23:08
Joined
Sep 25, 2000
Messages
805
"I have tryed using the .AddItem method but I can't seem to get it to work, because of my lack of knowledge, i'm stuck." - doesn't give much to work on.

On my setup, 2002 and higher version, a one column listbox, with rowsourcetype "value list", no controlsource, this works. The parenthesis aren't necessary, though I don't think they constitute a problem.

What is the problem, what happens, what does not happen, is there an errormessage, if so, which, what are the relevant properties of the listbox (column count, rowsource, rowsourcetype, controlsource, bound column, datatype of bound column field...)...
 

armychick86

Registered User.
Local time
Today, 16:08
Joined
Feb 11, 2007
Messages
25
The problem fixed itself in my previous post... woke up in the morning and it worked... go figure...

I am still wondering if this the best way to achieve my goal... I just need something to list all the software on each computer... Eventually I am going to have add an "Add Template" button to have it add the list of software that is installed by an image. I haven't attempted this yet... I guess you could say i'm still in design mode.

I wouldn't be able to search for specific software... I doubt this would be a problem, but I can't predict the future.
 

proxide

New member
Local time
Today, 22:08
Joined
Jun 10, 2011
Messages
1
Hi,
I know this post is a bit old but have just fixed this myself

if you are adding to a 2 column list box set to Value List the syntax is:

Combo1.AddItem ("43;Item1")

where 43 would be in column 1 and Item1 would be in column 2
 

jaybird0730

New member
Local time
Today, 17:08
Joined
Feb 15, 2010
Messages
4
I, too, found this thread refreshing. Adding to it because in my case I was trying to add string variables to a list box in a userform.

The string variables were returned from a Web Services xml query that I wanted to have listed for the user.

To add a list of string variables across a set of columns (in my case 4), I set the Row Source Type property = Value List, and separated the list of variables with a set of quotes around the semicolon, which is the delimiter for the list.

e.g.

Me.List31.AddItem myName & ";" & myMasID & ";" & myNamID & ";" & myNormTxt
 
Last edited:

Users who are viewing this thread

Top Bottom