Get data from Listbox

Hi,

I have the code below:

Private Sub cmdCopy_Click()
    Dim strCopy As String
    Dim i As Integer

     For i = 0 To lstCopy.ListCount
        If lstCopy.Selected(i) Then
            strCopy = lstCopy.Column(0, i)
            Debug.Print tmp
        End If
    Next i    
End Sub
Everytime I ran this code, the resolve turned out to be  strCopy = "" . I may miss something that I don't know? By the way, the listbox was linked to a table.

Thanks in advance
Andrew
ishadowmeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flavoCommented:
I dont see your proplem?

How do you get the value from strCopy??

Its decalred only under this sub, so you wont be able to access its value from another sub / function

This works a treat

    Dim strCopy As String
    Dim i As Integer

     For i = 0 To lstCopy.ListCount
        If lstCopy.Selected(i) Then
            strCopy = lstCopy.Column(0, i)
            Debug.Print strCopy
        End If
    Next i
    MsgBox strCopy

Dave
0
ishadowmeAuthor Commented:
Hi Flavo,

Actually this code is only the first part. The second part I will take the value from strCopy and import into clipboard. But that is not an issue. My problem right now is I can't even transfer the data from lstCopy.Column(0, i) to strCopy. I set the breakpiont in the code and I can see the value from the lstCopy.Column(0, i) but when I check on strCopy is shown = "" . Even if I add in a msgbox like you said, the msgbox show nothing in there. I think there's something has to do with the line:

strCopy = lstCopy.Column(0, i)

but I don't know what cause the problem :( , I also put the debug line to test out if strCopy can print out into immediate window but there's nothing. Do you have any idea?

Regards,
Andrew
0
GreymanMSCCommented:
Try this and tell us the result

    Dim strCopy As String
    Dim Item As Integer

     For Item = IIf(lstCopy.ColumnHeading, 1, 0) To lstCopy.ListCount - 1
        If lstCopy.Selected(Item) Then
            strCopy = strCopy & ", " & lstCopy.ItemData(Item)
            Debug.Print strCopy
        End If
    Next i
    MsgBox strCopy

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

GreymanMSCCommented:
By the way, your list box is multiselect, is it not? If it is single select, none of the above will work.
0
flavoCommented:
I tested this :

    Dim strCopy As String
    Dim i As Integer

     For i = 0 To lstCopy.ListCount
        If lstCopy.Selected(i) Then
            strCopy = lstCopy.Column(0, i)
            Debug.Print strCopy
        End If
    Next i
    MsgBox strCopy


with a single select listbox in Access 97.  Worked fine and dandy.  GreymanMSC is correct if you have a multi select.

Dumb question, but does column 1 actuall have data in it (ie not Nulls or "")??
0
flavoCommented:
byt column 1, i mean Column(0,i)
0
ishadowmeAuthor Commented:
Hi,

Greyman, I tried your code and I got a compile error: method or data member not found and it pointed to this line:

For Item = IIf(lstCopy.ColumnHeading, 1, 0) To lstCopy.ListCount - 1; It may have somthing to do with ColumnHeading ?

I use access 2k by the way and Yes, the listbox was set to simple multi-selected. Should it be extented? The listbox only has one column.

Flavo, I did set the breakpoint on this line : strCopy = lstCopy.Column(0, i)

I saw the data in lstCopy.Column(0, i) but not in strCopy.

Regards,
Andrew
0
ishadowmeAuthor Commented:
Hi,

I think I know what is going on now. There's nothing wrong with the original code that I posted eariler. In order to make it works I have to manually select all the data inside the listbox and then click on the command above. Doh, my bad and I'm sorry for that. But now my question is there's a way to select all the data in the listbox by VBA? I mean without touching the listbox because I will make it invisible.

Regards,
Andrew
0
ishadowmeAuthor Commented:
Hi,

Ok, I got it worked by doing some modifications base on greyman's code:

   Dim strCopy As String
   Dim i As Integer

     strCopy = ""
     For i = 0 To lstCopy.ListCount        
            strCopy = strCopy & lstCopy.Itemdata(i)          
     Next i
    MsgBox strCopy

Thanks
Andrew
0
GreymanMSCCommented:
Greyman, I tried your code and I got a compile error: method or data member not found and it pointed to this line:

   For Item = IIf(lstCopy.ColumnHeading, 1, 0) To lstCopy.ListCount - 1; It may have somthing to do with ColumnHeading ?

----
Yes, with appologies.  It should be ColumnHeads, not ColumnHeading.  This is the property which determines if the column headings are displayed.  If you know what the property is set to beforehand, you can just use 0 or 1 as the beginning value as appropriate.
 
Note that the ending value of the for-loop _must_ be (lstCopy.ListCount - 1), because that is the last index in an option-base-0 array. ( I have no idea how you get the above code to work without getting an indexing error on the last loop. )

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.