Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

populating a combo box

metal_manmetal_man Posts: 9Member
vb begginer needs help to populate a combo box from a locations table.
the table is locationinfodb.mdb all i need is the location codes populated. thank you for any help on this.

Comments

  • SanganakSakhaSanganakSakha Posts: 8Member
    Hello metal_man,

    What you need to do is to get the values of individual location codes and add them to the combo box using its list property in the Form_load event of the form that contains the combo.


    I would be glad to provide any more help you may need in this regard.


    with best compliments,

    SanganakSakha.
    Your Friend In Need.

  • DroopyADroopyA Posts: 175Member
    I might be a little late with this but here's a Sub I use all the time to populate my combo boxes. This works really well if you need to populate many combo boxes with diffrent fields from diffrent Tables on the same form. You will have to call it for every combo box passing the Table Name, The Filed You Want Populated From The Table and The name of the combo box.

    IE: Call PopulateForm(Employee, FirstName, cboEmployee)
    Call PopulateForm(Office, RoomNumber, cboRoom)

    Private Sub PopulateForm(strRecordName As String, strFieldName As String, cboCombo As ComboBox)
    'N473 *************************************************** N473
    'N473 * Populates All The Combo Boxes On The Form * N473
    'N473 *************************************************** N473

    Dim strDBLocation As String ' Holds The Database Location
    Dim cn As ADODB.Connection ' Active X Connection
    Dim rs As ADODB.Recordset ' Active X Recordset

    ' Clear Old Data
    cboCombo.Clear

    ' Connect To The Database
    strDBLocation = GetDatabase ' modDatabase
    Set cn = New ADODB.Connection
    cn.Open strDBLocation

    ' Open A Recordset
    Set rs = New ADODB.Recordset
    rs.CursorType = adOpenKeyset
    rs.LockType = adLockOptimistic
    rs.Source = strRecordName
    rs.ActiveConnection = cn
    rs.Open

    If rs.BOF = False Or rs.EOF = False Then
    rs.MoveFirst
    End If

    While rs.EOF = False
    cboCombo.AddItem (rs.Fields(strFieldName))
    rs.MoveNext
    Wend

    rs.Close ' Close The Recordset
    cn.Close ' Close The Connection

    Set rs = Nothing ' Set The Recordset To Nothing
    Set cn = Nothing ' Set The Connection To Nothing

    End Sub


    Hope That Helps
    -N473



Sign In or Register to comment.