How to load the values of a recordset in a combo box
Posted on September 15, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 'Database - How to load the values of a recordset in a combo box 'This code is used for filling your combo boxes with the values of a recordset. 'you include this function in your module and just pass the combobox name and 'query which will fetch the data from recordset. Public Sub FillListBox(ByRef lstControl As Object, ByRef strQry As String) 'this routine is used to fill up a list box by passing 'the lstbox name and 'query Dim lstControlObj As Object Dim Rst As ADODB.Recordset On Error GoTo VBError Set Rst = New ADODB.Recordset With Rst .Source = strQry .Open , AdoComp, adOpenDynamic, adLockReadOnly End With Set lstControlObj = lstControl lstControlObj.Clear If Rst.EOF = False Then Rst.MoveFirst While Rst.EOF = False lstControlObj.AddItem Rst.Fields(1) lstControlObj.ItemData(lstControlObj.NewIndex) = Rst.Fields(0) Rst.MoveNext Wend itsRecCount = 1 itsProcessClickFlag = True lstControlObj.ListIndex = 0 End If Done: Rst.Close Set Rst = Nothing Exit Sub VBError: ' Non-ADO error handler DisplayVBError GoTo Done End Sub |