|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Display Query in List Box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|