Solved

Access 2007 VBA - Change values of listbox items

Posted on 2011-02-28
3
1,904 Views
Last Modified: 2012-05-11
I have a listbox that is populated with multiple items, each having several columns worth of data.  I create the listbox items originally through code as is shown below.

Later, based on user actions I would like to be able to change some of the values of the different listbox items.

I've tried using code like this...
selectedIndex = lstbxAttachments.ItemsSelected.item(0)
lstbxAttachments.Column(1, selectedIndex) = "Removed"
lstbxAttachments.Column(3, selectedIndex) = ""
lstbxAttachments.Column(5, selectedIndex) = -1

But I end up throwing an exception when I try to change them like this.  Is there a better way to do this?
Set rs = DataService.Contract_GetContractAttachmentsByContractID(editID)
While Not rs.EOF
    strItem = CStr(rs("DocID")) + ";" + rs("DocDesc") + ";" + rs("Path") + ";" + rs("DocType") + ";" + CStr(rs("DocTypeID")) + ";1"
    lstbxAttachments.AddItem (strItem)
    rs.MoveNext
Wend
rs.Close

Open in new window

0
Comment
Question by:JosephEricDavis
  • 2
3 Comments
 
LVL 10

Expert Comment

by:conagraman
ID: 34999709
this will loop throught a listbox and remove an item if it ='s  "hello world"

Dim i As Integer
For i = Listbox.ListCount - 1 To 0 Step -1
If Listbox.ItemData(i) = "hello world" Then
Listbox.RemoveItem i
Next i
0
 
LVL 7

Author Comment

by:JosephEricDavis
ID: 34999727
I don't want to remove the item, I want to change the item.  So the same item will look different to the user.
0
 
LVL 10

Accepted Solution

by:
conagraman earned 500 total points
ID: 34999903
yes that is how you do it first you remove the item then you add it back in. there isnt a way to "change"  it per say.

Dim i As Integer
For i = Listbox.ListCount - 1 To 0 Step -1
If Listbox.ItemData(i) = "hello world" Then
Listbox.RemoveItem i  '<<remove it
Listbox.AddItem "yourstring, i   '<<add it back in
Next i

   
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now