DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Combo Boxes

  1. #1
    Join Date
    Feb 2004
    Posts
    43

    Combo Boxes

    I have a table with one field in it. This field is basically a list of troupes i.e:

    Yorkshire Supremes
    East Leeds Comets
    etc

    What is the best way to make a combo box list these records?

    Cheers
    MKayHavoc

  2. #2
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    145
    "Best" is a relative term. What do you mean by it? Are you looking for the easiest way, the one that performs best, the one that requires less resources? Sometimes these are conflicting goals. Even within these specifiacations you would need to give an idea of how your application is constructed now. How are you currently communicating with your database? Are you using data binding or recordset manipulation?

  3. #3
    Join Date
    Feb 2004
    Posts
    43
    I'm using a Data Control tied to an Access Database. I have one table with one field that contains troupe names and I want a bit of code that will add these troupes from the database into a Combo Box. The easiest way as it is only a simpe program. Does this help?

  4. #4
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    145
    In that case,

    1. Add a data control to your form and name it "dcTroupes".
    2. Set the DatabaseName property of your data control to your MDB.
    3. Set the RecordSource property to the table of troupes in your database.
    4. Add a combo box and name it cboTroupes
    5. Set the DataSource property to dcTroupes
    6. Set the DataField property to the field you desire from troups to show on the combo box.
    7. Run the form.

  5. #5
    Join Date
    Feb 2004
    Posts
    43
    I've tried that but it doesn't list the records from the table. It just displays the current record that is set through the data control.

    The idea I had was creating a txt box and setting it so that it displays the field I want. Then writing some code a bit like this:

    Dim AddCombo As String

    AddCombo = txtbox.text

    datDatabase.Recordset.MoveFirst

    Do
    cboTroupe.AddItem AddCombo
    datDatabase.Recordset.MoveNext
    Loop Until datDatabase.Recordset.EOF = True

    And stick this in the Form_Load. The only problem is that this doesn't work. What happens is when you drop down the combo box I have several entries of the same record i.e:

    Yorkshire Supremes
    Yorkshire Supremes
    Yorkshire Supremes

    Instead of:

    Yorkshire Supremes
    East Leeds Comets
    YCA

    Any ideas?

  6. #6
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    145
    There are many logical problems in your code. You need to review documentation on Data Binding, the data control and recordsets (DAO in this case). Also, I recommend you with ADODC (the ado data control) instead. It is much more current technology.

    Private Sub Form_Load()
    ' if you're going to do this in form_load you need to
    ' ensure the recordset has been created.
    Data1.Refresh

    ' check for no records before you MoveFirst
    If Not Data1.Recordset Is Nothing Then
    Data1.Recordset.MoveFirst
    Do While Not Data1.Recordset.EOF
    ' Get the field from recordset and add it to
    ' the combo box.
    Combo1.AddItem Data1.Recordset("Name")
    ' get the next record.
    Data1.Recordset.MoveNext
    Loop
    ' Position it back to the first record.
    ' You need this is using the Data control for
    ' another op.
    Data1.Recordset.MoveFirst
    End If
    End Sub

  7. #7
    Join Date
    Jan 2004
    Location
    Alexandria, VA
    Posts
    145
    Also, if you use a DataCombo control and ADODC instead, you don't need to AddItem and go through the recordset this way.

  8. #8
    Join Date
    Feb 2004
    Posts
    43
    Thanks a lot. You code you've given me works perfectly. Unfortunatly I'm working on a College Project and don't have time to switch to and learn ADODC. Next time thought that will be my prefered choice.

    Cheers again
    MKayHavoc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center