12-16-2003, 09:27 AM
what i am trying to do is to have a list of items that correspond to a fields in a database. i can get the list box to display the fields fine but i am struggling to get the next part working
what u want to do is to let the user select an item from the list and then the corresponding record in the database is shown in labels in the form
any help would be grateful as i am really stuck at the mo!
12-16-2003, 09:34 AM
Which control are you using to show the Records ???
ListView, ListBox, DataGrid, MSFlexGrid....
12-16-2003, 09:35 AM
this is the code used to fill the list box. have to get rid of Null values as well thou
Public Sub fillList()
While Not table.EOF
If IsNull(table("TopicID")) Then
List1.Text = ""
12-16-2003, 09:39 AM
If you are trying to identifed the Record added to the listbox you can use ItemData to store some ID from the DB (Record)
List1.ItemData(List1.NewIndex) = RS!ID
so, when you select (CLicks) on some item, check the ItemData of the Selected item ( List1.ItemData(List1.ListIndex) ), the ID value was stored there, so you can get all data from the DB using de ID (If you have one)
I hope helps
12-16-2003, 09:42 AM
sorry but i dont really undertand
12-16-2003, 09:44 AM
Do you have some Field that identifies the Table Record ????
ID | Name | FirstName | Address
ID its the and number that never repeats in the table
do you have one in you table ???
12-16-2003, 09:46 AM
yes, i have an ID field which are unique numbers
12-16-2003, 09:55 AM
ok, we will call that ID as MyID, ok ?
when you fill the listbox with records you add some text, right, well there is other data called ItemData, its not a user visible info, its a parallel data with the items.
Do While not Rs.Eof
List1.AddItem Rs!TextField '< --- Visible Text
List1.ItemData(List1.NewIndex) = Rs!MyID '<- Parallel data Not User Visible
Supouse these fields
MyID | Name | Address
1 | Mike | ......
2 | Peter | ......
3 | Jimmy | .....
the list1 will fill whit these values
when I selects Jimmy and I get the ItemData from List1.ItemData(List1.ListIndex), the result will be the MyID value = 3
so you can use this value to get more data from the table (ie Address) because the ID its unique in the table records.
I hope understand :)
12-16-2003, 10:31 AM
cool, i ve got that bit now but still not sure how i would use this to display the rest of the record in the labels?
12-16-2003, 10:32 AM
well, in the Click Event of the ListBox
open your Table and get the record where the ID = ItemData(...)
... "SELECT * FROM Table WHERE MyID=" & List1.ItemData(List1.ListIndex) ...
12-16-2003, 11:16 AM
i've been reading your advices on the list...
have a few questions
List1.ItemData(List1.NewIndex) = Rs!MyID
'it's working for me...
... "SELECT * FROM Table WHERE MyID=" & List1.ItemData List1.ListIndex) ...
'I have a msg saying "Invalid property array index"
but what does the "list1.newindex" do?
and what about the "list1.listIndex"?
12-16-2003, 11:19 AM
When you add a new Item in the ListBox the NewIndex property point to the last index (New Index added)
the ListIndex, returns the Selected Item Index, if theres no a selected item the ListIndex Value will be -1, so if this value is got it, can't use it in the ItemData() [ItemData(-1) = ERROR]
12-16-2003, 11:37 AM
Got it !
12-16-2003, 11:38 AM
No, the index starts on 0 and ends in (List1.ListCount-1), so the index would be greater that -1 ;)
12-16-2003, 11:39 AM
jeessh...you were faster than me!
my mistake last time...thanks for the explanation!