Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 21st, 2003, 06:23 AM
leefear1 leefear1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 2 leefear1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Display Query in List Box

I am in desperate need of some help. I have been set a task of developing a small application in which users will select an item from an sql query and record the fact that they have selected the item. The problem is that I have never used VB before and I am not allowed to ask any one I work with for help. I have so far managed to create a connection to the SQL tables and create a query. I need to display the query in a list box or listview so that the users can find and select one of the records. I seem am able to display the data in a datalist (although only one column) and a datagrid. The only clue I have been given is, "You will need to open a recordset and loop through the records. You need to set the ItemData and Additem methods of the list box." Obviously as I have never used VB before, this is slightly criptic to me.

Can anyone help explain how I do this as I have looked through around 15 books and spent over 24 hours on the internet and nothing explains this.

Oh and I have only been given a week to do this and it needs to be completed by Thursday.

Regards,

Lee

Last edited by leefear1 : July 21st, 2003 at 06:26 AM.

Reply With Quote
  #2  
Old July 22nd, 2003, 12:09 AM
shafan's Avatar
shafan shafan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 123 shafan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 51 m 51 sec
Reputation Power: 7
Hi,
Since populating a list (or combo) with items along with their Long ID (notice that ItemData can only be a Long value!) is a common task I use this sub:

Public Sub PopulateListWithItemData(ByVal sSQL As String, _
ByRef ctl As Control, _
ByVal sIDField As String, _
ByVal sDataField As String)
Dim rst As ADODB.Recordset

On Error GoTo ErrHndl

Set rst = New ADODB.Recordset

Set rst = GetDisconnectedRS(sSQL) 'My function - use rst.open instead

'Clear the combo
ctl.Clear

'Add the items from the recordset
Do Until rst.EOF
'Avoid nulls and empty strings
If Trim(rst(sDataField) & "") <> "" Then ctl.AddItem rst(sDataField)
ctl.ItemData(ctl.NewIndex) = rst(sIDField)
rst.MoveNext
Loop

CleanExit:
'Clean up
If rst.State Then rst.Close
Set rst = Nothing

Exit Sub

ErrHndl:
MsgBox "Error in [PopulateListWithItemData] in mdlGlobals." & vbNewLine & "Description: " & Err.Description
GoTo CleanExit

End Sub

To obtain a value selected by the user:
LongValue = control.ItemData(control.ListIndex)
(Make sure the ListIndex <> -1 : that an entry was selected)

Hope this helps

Last edited by shafan : July 22nd, 2003 at 12:51 AM.

Reply With Quote
  #3  
Old July 22nd, 2003, 03:43 AM
leefear1 leefear1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 2 leefear1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you. But where do I put this code and Which bits do I swap for my field names, connection commands etc. (I literally have never programmed in VB before).

Regards,

Lee

Reply With Quote
  #4  
Old July 22nd, 2003, 04:15 AM
shafan's Avatar
shafan shafan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 123 shafan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 51 m 51 sec
Reputation Power: 7
Hi Lee,
That's too much for my answer's scope but have a look in these sites:
ADO
ADO 2
VB

Search for tutorials on the Net.

Good Luck!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Display Query in List Box


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway