• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

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
0
ishadowme
Asked:
ishadowme
  • 4
  • 3
  • 3
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now