 |

12-16-2003, 08:27 AM
|
|
Regular
|
|
Join Date: Nov 2003
Posts: 79
|
|
selecting items in lists
|
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
|
|

12-16-2003, 08:34 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
|
Which control are you using to show the Records ???
ListView, ListBox, DataGrid, MSFlexGrid....
|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 08:35 AM
|
|
Regular
|
|
Join Date: Nov 2003
Posts: 79
|
|
|
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
|
|

12-16-2003, 08:39 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
|
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
|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 08:42 AM
|
|
Regular
|
|
Join Date: Nov 2003
Posts: 79
|
|
|
sorry but i dont really undertand
|
|

12-16-2003, 08:44 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
|
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 ???
|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 08:46 AM
|
|
Regular
|
|
Join Date: Nov 2003
Posts: 79
|
|
|
yes, i have an ID field which are unique numbers
|
|

12-16-2003, 08:55 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
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
Code:
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 
|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 09:31 AM
|
|
Regular
|
|
Join Date: Nov 2003
Posts: 79
|
|
|
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
|
|

12-16-2003, 09:32 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
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) ...

|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 10:16 AM
|
|
Freshman
|
|
Join Date: Dec 2003
Location: Montreal, Canada
Posts: 40
|
|
|
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!
|
|

12-16-2003, 10:19 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
|
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]
|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 10:37 AM
|
|
Freshman
|
|
Join Date: Dec 2003
Location: Montreal, Canada
Posts: 40
|
|
|

12-16-2003, 10:38 AM
|
 |
Mexican Coder
|
|
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
|
|
No, the index starts on 0 and ends in (List1.ListCount-1), so the index would be greater that -1 
|
__________________
Mikecrosoft.NET
* If I stop to ask I will stop to learn
* Just I know that I don't know nothing
|

12-16-2003, 10:39 AM
|
|
Freshman
|
|
Join Date: Dec 2003
Location: Montreal, Canada
Posts: 40
|
|
|
jeessh...you were faster than me!
my mistake last time...thanks for the explanation!
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|