Code review needed - please

Hi,
I have a property called LangSel (String).

I have the following code in the change event of my listbox:

// SET LangSel TO THE TEXT CONTAINED IN THE SELECTED ROW LangSel = (Me.Cell(Me.ListIndex, 0)) MsgBox ("LangSel")

I am trying to put the text content of the selected row, into the LangSel property.
Here is what happens:

When I click on test row - MsgBox = test
When I click on test2 row - MsgBox = test2
When I click on an empty row - the MsgBox displays 2 lines of text test (on the top line) followed by test2 (underneath)???

Could someone please advise me where I have gone wrong??

Thank you.

Your msgbox code line is wrong.

Sorry - that was a typo - I have no quote marks surrounding the word LangSel :slight_smile:

Here is my code:

// SET LangSel TO THE TEXT CONTAINED IN THE SELECTED ROW LangSel = (Me.Cell(Me.ListIndex, 0)) MsgBox (LangSel)

It’s when Me.ListIndex gives -1 or 0. When ListIndex>0, right values are shown.

Do a

if Me.ListIndex < 1 then return true

just before your LangSel = … line and you’ll be fine.

Try this:

if me.ListIndex <> -1 then
  // SET LangSel TO THE TEXT CONTAINED IN THE SELECTED ROW
LangSel = (Me.Cell(Me.ListIndex, 0))
else
  LangSel = ""
end if
MsgBox (LangSel)

(Thomas, ListIndex = 0 means first row.)

Oops, sorry. My bad! :slight_smile:

If you do use Cell( -1, col ), you will get back the entire column.

Kem - your modified code worked fine - thank you.
Just out of curiosity - why was my code showing ALL the values when I clicked on an empty row??

I would have imagined that each time a row was selected - the LangSel would have been overwritten?

It was being overwritten. With the contents of the entire column. If you pass -1 to Listbox.Cell, it will give you all the values in the column or row. Listbox.Cell(-1,-1) returns the entire contents of the listbox.