Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 22nd, 2006, 12:20 PM
Friend of Wrox
 
Join Date: Oct 2006
Location: Northampton, Northants, United Kingdom.
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default Populate ListBox

I want to populate a ListBox (List1) with the details from a collection, but it seems that VBA is missing some things that I am accustomed to with VB:

1: For nLoop = 1 To MyObject.Collection.Count
2: List1.AddItem MyObject.Collection.Item(nLoop)
3: Next

I read somewhere that I should have used (on #2):

2: List1.AddItem Item:=MyObject.Collection.Item(nLoop)

Also noted that the "Row Source Type" has to be set to "Value List", which I have checked and it is.

Neither seems to work and I am getting an error stating "Compile error: Method or data memebr not found"

Any ideas how I achieve this?

Regards,

Sean Anderson
__________________
Regards,

Sean Anderson
Reply With Quote
  #2 (permalink)  
Old November 22nd, 2006, 03:45 PM
Authorized User
 
Join Date: Nov 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

found some notes in the help file:

Adds a list item to the specified command bar combo box control. The combo box control must be a custom control and must be a drop-down list box or a combo box.

Note This method will fail if it's applied to an edit box or a built-in combo box control.

Syntax

expression.AddItem(Text, Index)

expression Required. An expression that returns a CommandBarComboBox object.

Text Required String. The text added to the control.

Index Optional Variant. The position of the item in the list. If this argument is omitted, the item is added to the end of the list.
Reply With Quote
  #3 (permalink)  
Old November 22nd, 2006, 03:46 PM
Authorized User
 
Join Date: Nov 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

no time to work on it now will try when i can

cheers
scott
Reply With Quote
  #4 (permalink)  
Old November 23rd, 2006, 05:51 AM
Friend of Wrox
 
Join Date: Oct 2006
Location: Northampton, Northants, United Kingdom.
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As per my opening message, I had tried the method that's found in the help. This would be the common format for populating a listbox in VB, but for some reason, Access seems to work a little different (or I am missing something).

Regards,

Sean Anderson
Reply With Quote
  #5 (permalink)  
Old November 23rd, 2006, 07:58 AM
Friend of Wrox
 
Join Date: Sep 2003
Location: Salisbury, Wiltshire, United Kingdom.
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

Sean,

I may be right off the mark with this, but anyway, never stopped me before!

A list box in ValueList stylee needs to be populated using a comma seperated list.
So a two column list box populated with rowsource = 1, "One", 2, "Two", 3, "Three" should display:

1 One
2 Two
3 Three

So to get your listBox working you need to populate it (the rowsource property) with text formatted in that manner.

So something like:

0: strTemp = me.list1.rowsource
1: For nLoop = 1 To MyObject.Collection.Count
2: strTemp = strTemp & ", " & MyObject.Collection.Item(nLoop)
3: Next
4: me.list1.rowsource = strTemp

You will obviously need to tidy this up for strings etc, but you should get the idea.

Hopefully useful in some respect?

Lee
Reply With Quote
  #6 (permalink)  
Old November 23rd, 2006, 09:53 AM
Authorized User
 
Join Date: Feb 2005
Location: Frankston South, Victoria, Australia.
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've been watching this thread for a while and I believe that it hasn't evolve much. Let me share my experience with list boxes.

I use listboxes extensively in my apps because they are simple to understand from a user point of view. I even used a listbox once to play the role of a subform with a one to many relationship with the main form because it is the only way to have an black horizontal bar across the data.

I always use queries to populate my listboxes. The idea of leehambly is interesting but it lacks flexibility if you need to insert an item somewhere in the list. Also how can you sort such comma delimited string.

If your data is in a table you can easily use SQL statements in the Row Source property of the list or a query you have created earlier. The list has a Bound Column allowing you to (for example) read the ID value of the selected item (through the ItemData property).

To insert a new item in the list use DAO to add it in the underlying table and requery the list.

Use Access help and look up for : ListCount, ListIndex, Selected and ItemData (along with a few more).

The first time you program a list box it is a hassle but VBA offers a rich set of tools to examine, select and query a list.

Conclusion of all things: Keep the data in a table, use a query to read what you need for the list and you will have fun.


Daniel
Reply With Quote
  #7 (permalink)  
Old November 23rd, 2006, 10:15 AM
Friend of Wrox
 
Join Date: Sep 2003
Location: Salisbury, Wiltshire, United Kingdom.
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

Daniel,

You're not wrong about the inflexibility of the listbox using Value List... it requires a set of user-defined rowsource functions to extract or add data, as well as sorting etc... certainly wouldnt be the way I would work it... though having said that: we don't live in ideal worlds.

Sean appears to be trying to populate a listbox (with Value list) with basically (although i may be wrong!) a totally mismatched technique - it is probably a feature of VB, dunno - don't use it!

I agree with everything you have to say on it though!

Lee
Reply With Quote
  #8 (permalink)  
Old November 23rd, 2006, 10:28 AM
Friend of Wrox
 
Join Date: Oct 2006
Location: Northampton, Northants, United Kingdom.
Posts: 115
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Both points (from <b>leehambly</b> and <b>dlamarche</b>) are very helpful.

So the net result would be (of course) to work from a table if possible and for short lists that need little (or better still, no) manipulation then work with the comma separated string in the RowSource, which is the case for me as I am dealing with a collection that is output by a 3rd party DLL.

Basically:

List1 = List of Available Choices (from DLL)

Select something from List1 and run

myobjected.selectedChoice = List1.text




Thanks very much for the help on this.

Regards,

Sean Anderson
Reply With Quote
  #9 (permalink)  
Old November 23rd, 2006, 11:29 AM
Authorized User
 
Join Date: Nov 2006
Location: , , .
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I wont pretend to know everything but Ill say I learned alot today and yes is it alot different then vb...

what i learned is:
1)access wants combos and listboxes to be bound
2)row source is the way to do it manually
3)using queries to populate seems to be the way to go
4)any of you remember or been to a webstie called accessvba.com? doesnt seem to be there anymore... this place is very similar
5)i think im addicted to vba for some strange reason and I should focus more on more powerful application development like sql server or my sql and php
6)thank you all, you are prefect gentlemen
Reply With Quote
  #10 (permalink)  
Old February 21st, 2007, 04:48 PM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

There is programmatical a fix for this, although it is quite lengthy... so before I type it out I want to make sure this thread isn't dead.

In essence the problem is in how Microsoft automates the building of the Access RowSource property for List Boxes. I created a workaround to this as a need for a project I was working on not too long ago.

In essence I think it is an Access bug the way it works but the workaround I have come up with has worked for me across several machines with different patch levels.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
populate listbox from SQL server Stanny Access 1 May 30th, 2005 08:11 AM
How to: Populate Filename to listbox? mdtcao Beginning VB 6 1 May 11th, 2005 10:25 PM
active directory populate listbox new_bie C# 1 March 17th, 2005 10:31 AM
populate combo-boxs and listbox ez BOOK: Beginning Access VBA 1 June 8th, 2004 12:59 PM
Listbox doesn't populate all the records edcaru Access 1 April 25th, 2004 10:32 AM



All times are GMT -4. The time now is 05:40 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.