1

I have a list box that is populated based on a previous filter (a text box). This means there is no predefined number of items in the listbox so using the index is impossible.

What I am looking for is a way of selecting the value of column(0), as there are 3 columns in my list box, of the selected line that is clicked.

Private Sub listSource_Click()

Dim strSource As String

strSource = "SELECT [Product Code],[Stock Level],[Description] FROM [products/stock] " & _
"WHERE Mid([Product Code],1,5) = " & Me.listSource.Column(0).Value & ";"

Me.listResult.RowSource = strSource
Me.listResult = vbNullString

End Sub

What I think I need is just the small piece of code that is supposed to be where "Me.listSource.Column(0).Value" is.

Thanks in advance, Bob P

Bob P
  • 237
  • 5
  • 18

3 Answers3

3

I reckon you want:

strSource = "SELECT [Product Code],[Stock Level],[Description] " & _
"FROM [products/stock] " & _
"WHERE Mid([Product Code],1,5) = '" & Me.listSource & "';"

Note the single quotes before and after Me.listSource.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
1

Me.listresult gives the bound column

you then have to append .Column(0),.Column(1) ... for other columns.

to find out which one of the list you need to be looking at, .ItemsSelected.Count will tell you how many are selected (1 for a simple list box), and .ItemsSelected(0) for the index to the first item selected, so, the complete line for the first column of the first selected line would be:

Me.listSource.ItemData(Me.ListSource.ItemsSelected(0))
SeanC
  • 15,695
  • 5
  • 45
  • 66
0

You should be able to use Me.ListSource.Column(0) to get the value of the first column of the selected row.

Also see: Retrieve column values of the selected row of a multicolumn Access listbox

Community
  • 1
  • 1
KFleschner
  • 499
  • 3
  • 13
  • Still not working guys, here is the code I am using. strSource = "SELECT [Product Code],[Stock Level],[Description] FROM [products/stock] " & _ "WHERE Mid([Product Code],1,5) = Mid(" & Me.listSource.Column(0) & ",1,5);" – Bob P Sep 27 '12 at 13:59