Link to home
Start Free TrialLog in
Avatar of hallpett
hallpett

asked on

Retrieve value from second column of selected item in a MS Access listview


From the dbl click event of a listview I retrieve the value from first column of the selected item like this: intProductId = Forms!frmInventory.lstListView1.SelectedItem.Text

This works fine, but how can i retrieve the value from the second column?
Appreciate any suggestions.
Avatar of Si Ball
Si Ball
Flag of United Kingdom of Great Britain and Northern Ireland image

not sure if there is a more direct way, I always have to iterate the items selected collection to find the relevant items,

then use column(x,y) to get to the 2nd column.

i've got some code i'll dig out a sample
Avatar of TinTombStone
TinTombStone

intProductID = Forms!frmInventory.lstView1.Value

will give you the value of the bound column

for other columns (column 1 = 0)

strProductName = Forms!frmInventory.lstView1.Column(1)
dblProductPrice = Forms!frmInventory.lstView1.Column(2)
Dim x As Integer

    For x = 0 To List7.ListCount
    
        If List7.Selected(x) Then
        
        MsgBox List7.Column(1, x) & " - " & List7.Column(2, x)
         end if

next

Open in new window


Avatar of hallpett

ASKER

Get an error when I try to implement both solutions:
"Object doesn't support this property or method".
Would be best if i could get TinTombStones solutions to work: "strProductName = Forms!frmInventory.lstView1.Column(1)". I'm a novice on this field but shouldn't we somehow say that it's the selected item we want to retrieve value from?
did you change my list7 to lstView1?

Dim x As Integer

    For x = 0 To lstView1.ListCount
   
        If lstView1.Selected(x) Then
       
        MsgBox lstView1.Column(1, x) & " - " & lstView1.Column(2, x)
         end if

next
Yes I changed to lstListView1. Could my reference be a problem (see image)?
2011-04-28-104018.jpg
aaah, sorry, I had wrongly assumed you were using a default ms access list control.

not used the activex listview control.

will do some research and mock up a test
ListView0.SelectedItems(0).SubItems(1).Text should work according to vb forums:

http://www.vbforums.com/showthread.php?t=573387

but its not working in my mockup...

the listview0 does not have a selected items collection...
i cannot even get it to work to pick up the main selection

Forms!Form1!ListView0.SelectedItems.Text gives me an error.

i tried amending the multiselect, hide selection and fullrowselect options too, to no avail.

i think we need someone who knows how to use this control successfully to help...
I still get error: "Object doesn't support this property or method".

intProductId = Forms!frmInventory.lstListView1.SelectedItem.Text
strProductName=Forms!frmInventory.lstListView1.SelectedItems(0).SubItems(1).Text

The first of this two lines of code workes and i get the ProductId, but second line gives error. Since I have the ProductId I think I just have to write  me a small function to get the ProductName directly from the table instead. Thanks for the effort!  
ASKER CERTIFIED SOLUTION
Avatar of Si Ball
Si Ball
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
We did not find the solution I was looking for, but problem can be solved with the proposed workaround.
nice one.

i thnik maybe its just not possible with that control.

do you have to use that list control?  if you use the ordinary access one its much easier.
This is the solution.
If you for instance want to get the value from selected item column 3 you can use this syntaks:
intProductId = Forms!frmInventory.lstListView1.SelectedItem.SubItems(2)