Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > selecting items in lists


Reply
 
Thread Tools Display Modes
  #1  
Old 12-16-2003, 08:27 AM
dabwang dabwang is offline
Regular
 
Join Date: Nov 2003
Posts: 79
Default 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
Reply With Quote
  #2  
Old 12-16-2003, 08:34 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #3  
Old 12-16-2003, 08:35 AM
dabwang dabwang is offline
Regular
 
Join Date: Nov 2003
Posts: 79
Default

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
Reply With Quote
  #4  
Old 12-16-2003, 08:39 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #5  
Old 12-16-2003, 08:42 AM
dabwang dabwang is offline
Regular
 
Join Date: Nov 2003
Posts: 79
Unhappy

sorry but i dont really undertand
Reply With Quote
  #6  
Old 12-16-2003, 08:44 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #7  
Old 12-16-2003, 08:46 AM
dabwang dabwang is offline
Regular
 
Join Date: Nov 2003
Posts: 79
Default

yes, i have an ID field which are unique numbers
Reply With Quote
  #8  
Old 12-16-2003, 08:55 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #9  
Old 12-16-2003, 09:31 AM
dabwang dabwang is offline
Regular
 
Join Date: Nov 2003
Posts: 79
Default

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
Reply With Quote
  #10  
Old 12-16-2003, 09:32 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #11  
Old 12-16-2003, 10:16 AM
VPower VPower is offline
Freshman
 
Join Date: Dec 2003
Location: Montreal, Canada
Posts: 40
Default

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!
Reply With Quote
  #12  
Old 12-16-2003, 10:19 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #13  
Old 12-16-2003, 10:37 AM
VPower VPower is offline
Freshman
 
Join Date: Dec 2003
Location: Montreal, Canada
Posts: 40
Default

Got it !

Thanks!
Reply With Quote
  #14  
Old 12-16-2003, 10:38 AM
Mikecrosoft's Avatar
Mikecrosoft Mikecrosoft is offline
Mexican Coder
 
Join Date: Jun 2002
Location: Monterrey, N.L., Mexico
Posts: 2,793
Default

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
Reply With Quote
  #15  
Old 12-16-2003, 10:39 AM
VPower VPower is offline
Freshman
 
Join Date: Dec 2003
Location: Montreal, Canada
Posts: 40
Default

jeessh...you were faster than me!

my mistake last time...thanks for the explanation!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
selecting items from listbox eric317 General 2 04-02-2004 11:53 AM
Selecting Items from Listbox? rat General 5 11-12-2003 10:55 PM
Combobox - Selecting items with keystrokes MarkS Interface and Graphics 2 04-03-2003 09:40 PM
Programmatically selecting Listview items in VBA evanking Word, PowerPoint, Outlook, and Other Office Products 4 03-20-2003 02:22 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->