selecting items in lists

dabwang
12-16-2003, 08: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!

thanks

nige

Mikecrosoft
12-16-2003, 08:34 AM
Which control are you using to show the Records ???

ListView, ListBox, DataGrid, MSFlexGrid....

dabwang
12-16-2003, 08: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()
table.MoveFirst

While Not table.EOF
If IsNull(table("TopicID")) Then
List1.Text = ""
Else
List1.AddItem table!TopicID
End If

table.MoveNext
Wend
End Sub

Mikecrosoft
12-16-2003, 08: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)

Example:

List1.AddItem RS!Name
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

dabwang
12-16-2003, 08:42 AM
sorry but i dont really undertand

Mikecrosoft
12-16-2003, 08:44 AM
Do you have some Field that identifies the Table Record ????

Example:

ID | Name | FirstName | Address

ID its the and number that never repeats in the table

do you have one in you table ???

dabwang
12-16-2003, 08:46 AM
yes, i have an ID field which are unique numbers

Mikecrosoft
12-16-2003, 08: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.

Example

Do While not Rs.Eof
List1.AddItem Rs!TextField '< --- Visible Text
List1.ItemData(List1.NewIndex) = Rs!MyID '<- Parallel data Not User Visible

rs.movenext
loop


Supouse these fields
MyID | Name | Address
1 | Mike | ......
2 | Peter | ......
3 | Jimmy | .....

the list1 will fill whit these values

Mike
Peter
Jimmy

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 :)

dabwang
12-16-2003, 09: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?

nige

Mikecrosoft
12-16-2003, 09: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) ...

:D

VPower
12-16-2003, 10:16 AM
Hello ppl,
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"?

Thanks!

Mikecrosoft
12-16-2003, 10: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]

VPower
12-16-2003, 10:37 AM
Got it !

Thanks!

Mikecrosoft
12-16-2003, 10:38 AM
No, the index starts on 0 and ends in (List1.ListCount-1), so the index would be greater that -1 ;)

VPower
12-16-2003, 10:39 AM
jeessh...you were faster than me!

my mistake last time...thanks for the explanation!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum