Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2005
    Posts
    113

    Unanswered: How to add into an unbound listbox

    I need to add values that a user has choosen to a listbox. I cant find any function to do that. Somebody knows?

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    You understand that if it's an unbound listbox that the added values will be lost on closing the form? Having said that, with a textbox named txtNuItem and a command button named cmdAddNuItem this code will add an item if it's entered in the text box and the button pressed:

    Code:
    Private Sub cmdAddNuItem_Click()
    Dim strRowSrc As String
    strRowSrc = Me.MyListBox.RowSource
        If Not IsNull(Me.txtNuItem.Value) Then
             Me.MyListBox.RowSource = strRowSrc & ";'" & Me.txtNuItem.Value & "'"
        End If
    End Sub
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Nov 2005
    Posts
    113
    Thanks Missinglinq it nearly works.

    I have a problem with the listbox to show all recordsource. I do listbox.requery but it overwrites the value. I have checked by msgbox listbox.RowSource and thats ok, all stuff is there. I cant understand why the listbox dont show proper RowSource when I know i have it.

  4. #4
    Join Date
    Nov 2005
    Posts
    113
    Thanks Missinglinq it works. I modify like that:

    Code:
    Private Sub cmdAddNuItem_Click()
    If Not IsNull(Me.txtNuItem.Value) Then
    If Me.MyListBox.ListCount -1 < o Then
             Me.MyListBox.RowSource = Me.txtNuItem.Value
             Me.MyListBox.Requery
    Else
             Me.MyListBox.RowSource = Me.MyListBox.RowSource & ";" &          Me.txtNuItem.Value
             Me.MyListBox.Requery
        End If
    End Sub

  5. #5
    Join Date
    Nov 2005
    Posts
    113
    Somebody now how to delete from the list?

  6. #6
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Have a look at the "AddItem" and "RemoveItem" methods of your listbox.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    All Visual Basics are not equal! Unlike in standard VB, "AddItem" and "RemoveItem" only works in VBA with command bar combo boxes.

    From Access Help:
    Adds a list item to the specified command bar combo box control.
    Note This method will fail if it's applied to an edit box or a built-in combo box control.
    I only know this because it's come up half a dozen times in the past week here and on other fora.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    Works fine for me.

    From access help:

    AddItem Method
    See AlsoApplies ToExampleSpecificsAdds a new item to the list of values displayed by the specified list box control or combo box control.

    expression.AddItem(Item, Index)
    expression Required. An expression that returns one of the objects in the Applies To list.

    Item Required String. The display text for the new item.

    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.

    Remarks
    The RowSourceType property of the specified control must be set to "Value List".

    This method is only valid for list box or combo box controls on forms.

    List item numbers start from zero. If the value of the Index argument doesn't correspond to an existing item number, an error occurs.

    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.

    Use the RemoveItem method to remove items from the list of values.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  9. #9
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    I'm fascinated, Teddy! What version of Access? This is the help from Access 2000:

    AddItem Method
    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
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •