Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Items to a Multiple Column ListBox 2

Status
Not open for further replies.

snyperx3

Programmer
May 31, 2005
467
US
Hey guys,

I'm trying to add items to a listbox that has 2 columns. They are related to each other and must scroll with each other, ive done this with a Table/Query row source type, but now im doing it on a Value List row source type. The only information I've got is
Code:
lstDist.AddItem("item",[index])
with the index being the ROW. I need to add items to a specific column on a specific row. How would i go about doing this? Thanks for any help.


~(>- Pete -<)~
 
From the VBA Help files on AddItem:

[tt]For multiple-column lists, use semicolons to delimit the strings for each column (for example, "1010;red;large" for a three-column list). If the Item argument contains fewer strings than columns in the control, items will be added starting with the left-most column. If the Item argument contains more strings than columns in the control, the extra strings are ignored.[/tt]

HTH,

Ken S.
 
How are ya snyperx3 . . . . .

Unless there's some difference in versions, to my knowledge [blue]AddItem[/blue] is for comboboxes on CommandBars (aka menu & toolbars). If I'm right you'll get an error relation to [blue]Can't use this method[/blue]. If so, you can use the code below ([blue]You![/blue] substitute proper names in [purple]purple[/purple]):
Code:
[blue]Public Sub AddToListBox(Col1 As String, Col2, Idx As String)
   [green]'Col1 = Colum1 Data
   'Col2 = Colum2 Data
   'Idx = Position in Listbox (base 1)[/green]
   Dim LB As ListBox, Pack As String, Ary, n As Integer
   
   Set LB = Me![purple][b]ListboxName[/b][/purple]
   Ary = Split(LB.RowSource, ";")
   
   For n = LBound(Ary) To UBound(Ary) - 1 Step 2
      If Idx = (n / 2) + 1 Then
         Pack = Pack & "'" & Col1 & "';'" & Col2 & "';"
      End If
      
      Pack = Pack & "'" & Ary(n) & "';'" & Ary(n + 1) & "';"
   Next
   
   LB.RowSource = Left(Pack, Len(Pack) - 2)
   
   Set LB = Nothing
   
End Sub
[/blue]
To run the routine from VBA:
Code:
[blue] Call AddToListBox("Col1Dat", "Col2Dat", PositionInListbox)[/blue]

Calvin.gif
See Ya! . . . . . .
 
Hi, AceMan -

Must be a version difference. Not sure which version it appeared in, but AddItem is now a method of the combo and listbox controls (I'm using 2002 at home, 2003 at the office).

So for snyperx3's scenario,
Code:
lstDist.AddItem("red;apples", 6)
would add a 2-column entry at the 7th position in the list.

Ken S.
 
Eupher is correct.

.AddItem and .RemoveItem methods of native Access combos and lists became available in xp (2002) version.

Also - in that version, the limit on value list .rowsourcetype increased from 2048 characters (2000 and previous versions) to 32 750 characters.

Isn't this perhaps more a forms question, than VBA coding question? and could be addressed in the forms forum (forum702)

Roy-Vidar
 
no roy-vidar this is a vba question. how are you going to add the items just using forms and not vba?

thanks eupher. works fine.



~(>- Pete -<)~
 
Here are some VBA thingies
thread705-1034400, thread705-1032299, thread705-1034952, thread705-1028386, thread705-1027508

Consider the name of this forum; "Microsoft Access Modules (VBA Coding)"

A tad more than using some form event to utilize a basic/standard method of a form control

Same as with the Reports forum. That's a forum where report controls, report coding and anything relevant to reports are discussed.

Here are some form stuff, starting with my little form faq faq702-5010
then thread702-1077678, thread702-1078349 (and link within), thread702-966550, thread702-1077464

So - is there any difference between "Microsoft Access Modules VBA Coding" and "Microsoft Access Forms"

My say is - No - this is not a VBA issue, it's a form issue.

Roy-Vidar
 
Eupher & RoyVidar . . . . .

Thanks guys. Good to know. I'm using 2002 on one of my machines and never got around to checking . . .

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top