I am trying to update the data by clicking the Listbox,but getting error, please see my code below.
Using below code to display the list box value to text
Unlike ComboBoxes, you can't edit values in a ListBox on the fly like this. You have to delete the entry and insert one back in, with your new values. I'm not sure this is the most elegant way to do it, but the following works:
Private Sub btnUpdate_Click()
Dim values(3)
Dim u As Long
u = ListBox1.ListIndex
If u <> -1 Then
values(0) = TextBox1.Value
values(1) = TextBox2.Value
values(2) = TextBox3.Value
values(3) = TextBox4.Value
With ListBox1
.RemoveItem u
.AddItem values(0), u
.List(u, 1) = values(1)
.List(u, 2) = values(2)
.List(u, 3) = values(3)
End With
End If
End Sub
Private Sub ListBox1_Click()
Dim i As Long
With Me
i = .ListBox1.ListIndex
.ListBox1.Selected(i) = True
.TextBox1.Value = .ListBox1.Column(0, i)
.TextBox2.Value = .ListBox1.Column(1, i)
.TextBox3.Value = .ListBox1.Column(2, i)
.TextBox4.Value = .ListBox1.Column(3, i)
End With
End Sub
As I say, not elegant: I had to store the values of TextBox1 etc. in the array as when the RemoveItem runs, the Index changes which causes the ListBox1_Click to run - resetting all the Textboxes.