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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox does not have AddItem method

Status
Not open for further replies.

andzejek

MIS
Sep 1, 2007
154
US
My listbox has RowSourceType = "Value List" , why than it does not support AddItem method?????

Andrew
 
This works for me:
Code:
    Me.lstColors.AddItem ("Green")

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
I don't have AddItem on this small popup menu, which is popping up after dot, when you type me.mylisbox. !!! Does anyone knows why?
 
I might be wrong but I believe the additem method is not in older versions of Access.
 
I don't think Access 97 had an AddItem. You were forced to use code like:
Code:
    Me.lstColors.RowSource = Me.lstColors.RowSource & ";""Green""")

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
andzejek said:
It was in "MSDN Office Development" in 1997 !
The UI controls in the different flabours of VBA are quite often different. So perhaps AddItem was supported in the listbox for Excel97, but not Access97.


 
Here it is!

MSDN, Office Development - Office 97 Documentation :

ListBox Control
Description

Displays a list of values and lets you select one or more.

Remarks

If the ListBox is bound to a data source, then the ListBox stores the selected value in that data source.

The ListBox can either appear as a list or as a group of OptionButton controls or CheckBox controls.

The default property for a ListBox is the Value property.

The default event for a ListBox is the Click event.

Note You can't drop text into a drop-down ListBox.

Properties

BackColor property, BorderColor property, BorderStyle property, BoundColumn property, BoundValue property, Column property, ColumnCount property, ColumnHeads property, ColumnWidths property, ControlSource property, ControlTipText property, Enabled property, Font object, ForeColor property, Height, Width properties, HelpContextID property, IMEMode property, IntegralHeight property, LayoutEffect property, Left, Top properties, List property, ListCount property, ListIndex property, ListStyle property, Locked property, MatchEntry property, MouseIcon property, MousePointer property, MultiSelect property, Name property, Object property, OldHeight, OldWidth properties, OldLeft, OldTop properties, Parent property, RowSource property, Selected property, SpecialEffect property, TabIndex property, TabStop property, Tag property, Text property, TextColumn property, TopIndex property, Value property, Visible property.

Methods

AddItem method, Clear method, Move method, RemoveItem method, SetFocus method, ZOrder method.

Events

AfterUpdate event, BeforeDragOver event, BeforeDropOrPaste event, BeforeUpdate event, Change event, Click event, DblClick event, Enter, Exit events, Error event, KeyDown, KeyUp events, KeyPress event, MouseDown, MouseUp events, MouseMove event.

See Also

CheckBox control, ComboBox control, OptionButton control.

Example

The following example adds and deletes the contents of a ListBox using the AddItem, RemoveItem, and SetFocus methods, and the ListIndex and ListCount properties.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

A ListBox named ListBox1.
Two CommandButton controls named CommandButton1 and CommandButton2.
Dim EntryCount As Single
Private Sub CommandButton1_Click()
EntryCount = EntryCount + 1
ListBox1.AddItem (EntryCount & " - Selection")
End Sub

Private Sub CommandButton2_Click()
ListBox1.SetFocus

'Ensure ListBox contains list items
If ListBox1.ListCount >= 1 Then
'If no selection, choose last list item.
If ListBox1.ListIndex = -1 Then
ListBox1.ListIndex = ListBox1.ListCount - 1
End If
ListBox1.RemoveItem (ListBox1.ListIndex)
End If
End Sub

Private Sub UserForm_Initialize()
EntryCount = 0
CommandButton1.Caption = "Add Item"
CommandButton2.Caption = "Remove Item"
End Sub
 
That's [red]not[/red] a standard Access listbox. There is no Row Source Type listed and I have never seen a listbox that can appear "as a group of OptionButton controls or CheckBox controls".

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
Well I'm beginner here.... Text above I copied from MSDN cd-rom.
Thnak you anyway!
 
It was in "MSDN Office Development" in 1997 !
[/code]

Like I said:

Getting items into a list or combo box from a data source is elementary in Access. Sometimes, though, you need to put things into a list box that you don't have stored in a table. In Visual Basic and other implementations of VBA-hosted environments, and in Access 2002 and later, this is simple: you just use the AddItem method. But Access list boxes in versions prior to 2002 don't support this method. How can you add to a list box items that aren't stored in a table?
7.5.2. Solution

Access list boxes (and combo boxes) in versions prior to Access 2002 didn't support the AddItem method that Visual Basic programmers are used to using. To make it easy for you to get bound data into list and combo boxes, the Access developers originally didn't supply a simple technique for loading unbound data. To get around this limitation, there are two methods you can use to place data into an Access list or combo box: you can programmatically build the RowSource string yourself, or you can call a list-filling callback function. Providing the RowSource string is easy, but it works in only the simplest of situations. A callback function, though, will work in any situation. This solution demonstrates both methods. In addition, this solution demonstrates using the AddItem method of ListBox and ComboBox controls, added in Access 2002.
[/code]
Hopes this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top