PC Review


Reply
Thread Tools Rate Thread

Adding to a ListBox

 
 
=?Utf-8?B?UGZsdWdz?=
Guest
Posts: n/a
 
      21st Jun 2007
I have a userform that contains a listbox. Users can add values by entering
number in a textbox and clicking an "add" button. The problem is once a
certain number of entries have been added, the next values appear at the end
of the listbox.

Using the .AddItem method, how do I add values to the beginning of the list?
The code keeps saying it expects an "=" sign.

Code:
Private Sub cbCustomCodeAdd_Click()
With Me
If Not .tbCustomCode.Value = "" Then
For Each item In .lbGathCodes.List
If item = .tbCustomCode.Value Then
.tbCustomCode.Value = ""
Exit Sub
End If
Next item
If Len(.tbCustomCode) > 3 Then .tbCustomCode =
Left(.tbCustomCode, 3)
.lbGathCodes.AddItem (.tbCustomCode.Value)
.tbCustomCode.Value = ""
.lbGathCodes.Selected(.lbGathCodes.ListCount - 1) = True
End If
End With
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      21st Jun 2007
You can specify the index number to add an item in a certain position within
a listbox. However, there are two methods, depending on where you created
your listbox from. I suggest you review the VBA help file on AddItem method
for each type listbox.

"Pflugs" wrote:

> I have a userform that contains a listbox. Users can add values by entering
> number in a textbox and clicking an "add" button. The problem is once a
> certain number of entries have been added, the next values appear at the end
> of the listbox.
>
> Using the .AddItem method, how do I add values to the beginning of the list?
> The code keeps saying it expects an "=" sign.
>
> Code:
> Private Sub cbCustomCodeAdd_Click()
> With Me
> If Not .tbCustomCode.Value = "" Then
> For Each item In .lbGathCodes.List
> If item = .tbCustomCode.Value Then
> .tbCustomCode.Value = ""
> Exit Sub
> End If
> Next item
> If Len(.tbCustomCode) > 3 Then .tbCustomCode =
> Left(.tbCustomCode, 3)
> .lbGathCodes.AddItem (.tbCustomCode.Value)
> .tbCustomCode.Value = ""
> .lbGathCodes.Selected(.lbGathCodes.ListCount - 1) = True
> End If
> End With
> End Sub
>

 
Reply With Quote
 
=?Utf-8?B?UGZsdWdz?=
Guest
Posts: n/a
 
      21st Jun 2007
I looked through the help files, and for Userforms the syntax is:
Variant = object.AddItem [item,[varIndex]]

Without the "variant = " part, I can make the code work as below. However,
when I specify an index number, I get an error message saying "Expected =".
Then, when I declare a variant 'test' and set it equal to .ListBox.AddItem
(item, #), the compiler highlights the AddItem part and says "Expected
Function."

I don't know what it expects of me! Any further ideas?

Thanks, Pflugs

"JLGWhiz" wrote:

> You can specify the index number to add an item in a certain position within
> a listbox. However, there are two methods, depending on where you created
> your listbox from. I suggest you review the VBA help file on AddItem method
> for each type listbox.
>
> "Pflugs" wrote:
>
> > I have a userform that contains a listbox. Users can add values by entering
> > number in a textbox and clicking an "add" button. The problem is once a
> > certain number of entries have been added, the next values appear at the end
> > of the listbox.
> >
> > Using the .AddItem method, how do I add values to the beginning of the list?
> > The code keeps saying it expects an "=" sign.
> >
> > Code:
> > Private Sub cbCustomCodeAdd_Click()
> > With Me
> > If Not .tbCustomCode.Value = "" Then
> > For Each item In .lbGathCodes.List
> > If item = .tbCustomCode.Value Then
> > .tbCustomCode.Value = ""
> > Exit Sub
> > End If
> > Next item
> > If Len(.tbCustomCode) > 3 Then .tbCustomCode =
> > Left(.tbCustomCode, 3)
> > .lbGathCodes.AddItem (.tbCustomCode.Value)
> > .tbCustomCode.Value = ""
> > .lbGathCodes.Selected(.lbGathCodes.ListCount - 1) = True
> > End If
> > End With
> > End Sub
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding to listbox without refreshing.. JJ Microsoft ASP .NET 5 4th May 2007 01:34 PM
Adding item in listbox =?Utf-8?B?c3Jpa2FudGg=?= Microsoft Access Form Coding 5 27th Feb 2007 12:42 PM
Adding into listbox =?Utf-8?B?bGty?= Microsoft C# .NET 2 26th Apr 2005 11:32 AM
Adding a Control to a ListBox Just Me Microsoft VB .NET 2 21st Aug 2004 03:32 PM
Adding To A Listbox Robert Wainwright Microsoft Access Form Coding 2 28th Jan 2004 04:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:43 AM.