Results 1 to 8 of 8

Thread: how to use DAO library to fill combo box

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2017
    Location
    Paraguay
    Posts
    7

    how to use DAO library to fill combo box

    hello every body

    i want to ask how to load combo box from database access 2000 using DAO library code?

    best regards

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: how to use DAO library to fill combo box

    What a loaded question.

    One, do you know how to query your database and return items in a recordset?

    If so, then you simply cycle through the recordset and use the combobox additem feature:

    ex: Combo1.Additem(recordsetname!fieldname)

    Two, if you don't know how to return items from a database, I'd need a whole lot more information to help you.

    Sammi

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,936

    Re: how to use DAO library to fill combo box

    Yep, Sam pretty much nailed it. Hananfathy, I'm going to assume you're talking about an unbound ComboBox, as that's always the way I use the DAO (and I use the DAO extensively).

    Basically, you've got to get your records accessible in a recordset. You can do that by simply opening a table or possibly running a SQL query. Or, there are yet other ways.

    Once you get your recordset object created, you can use RecordCount, MoveFirst, MoveNext, and EOF members to spin through the recordset. I mentioned RecordCount because I always check to see if there are records before I start spinning through them. (Also, RecordCount isn't always accurate, but it always works as a boolean check.)

    If there are records, go MoveFirst, put the record in the ComboBox as Sam suggested, check EOF, and then MoveNext until all done.

    That's the gist of it.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    Banned
    Join Date
    Nov 2017
    Location
    Paraguay
    Posts
    7

    Re: how to use DAO library to fill combo box

    Quote Originally Posted by SamOscarBrown View Post
    What a loaded question.

    One, do you know how to query your database and return items in a recordset?

    If so, then you simply cycle through the recordset and use the combobox additem feature:

    ex: Combo1.Additem(recordsetname!fieldname)

    Two, if you don't know how to return items from a database, I'd need a whole lot more information to help you.

    Sammi

    i used this code but it didn't work

    vn.MoveFirst
    Combo1.Clear
    Do While Not vn.EOF
    Combo1.AddItem ("vn_nm")
    vn.MoveNext
    Loop

    thanks a lot for your help

  5. #5

    Thread Starter
    Banned
    Join Date
    Nov 2017
    Location
    Paraguay
    Posts
    7

    Re: how to use DAO library to fill combo box

    i used the code

    vn.MoveFirst
    Combo1.Clear
    Do While Not vn.EOF
    Combo1.AddItem ("vn_nm")
    vn.MoveNext
    Loop

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: how to use DAO library to fill combo box

    Hi Hanan,

    here is one way of doing it.
    you will have to change the SQL to your Table in the Database.






    Code:
    'Public Mydb As Database is declared in a Modul
    
    Sub LoadCallTypes(objCombo As Object)
       
       Dim SQL As String
       Dim rsListing
       
       SQL = "SELECT CallTypeID, CallDescription FROM CallType "
       SQL = SQL & "ORDER BY CallDescription"
       
       Set rsListing = Mydb.OpenRecordset(SQL)
       
       With rsListing
          If (.RecordCount > 0) Then
             .MoveFirst
          Do While Not .EOF
                If (Not IsNull(!CallTypeID)) Then
                   If (Not IsNull(!CallDescription)) Then objCombo.AddItem Trim(!CallDescription)
                   objCombo.ItemData(objCombo.NewIndex) = !CallTypeID
                End If
                .MoveNext
             Loop
          End If
       End With
       
       rsListing.Close
       Set rsListing = Nothing
       
       Exit Sub
    End Sub
    then in Form..
    Code:
     
    Private Sub Form_Load()
    
    Call LoadCallTypes(Combo1)
        Combo1.ListIndex = 0
    
    End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Banned
    Join Date
    Nov 2017
    Location
    Paraguay
    Posts
    7

    Re: how to use DAO library to fill combo box

    Quote Originally Posted by ChrisE View Post
    Hi Hanan,

    here is one way of doing it.
    you will have to change the SQL to your Table in the Database.






    Code:
    'Public Mydb As Database is declared in a Modul
    
    Sub LoadCallTypes(objCombo As Object)
       
       Dim SQL As String
       Dim rsListing
       
       SQL = "SELECT CallTypeID, CallDescription FROM CallType "
       SQL = SQL & "ORDER BY CallDescription"
       
       Set rsListing = Mydb.OpenRecordset(SQL)
       
       With rsListing
          If (.RecordCount > 0) Then
             .MoveFirst
          Do While Not .EOF
                If (Not IsNull(!CallTypeID)) Then
                   If (Not IsNull(!CallDescription)) Then objCombo.AddItem Trim(!CallDescription)
                   objCombo.ItemData(objCombo.NewIndex) = !CallTypeID
                End If
                .MoveNext
             Loop
          End If
       End With
       
       rsListing.Close
       Set rsListing = Nothing
       
       Exit Sub
    End Sub
    then in Form..
    Code:
     
    Private Sub Form_Load()
    
    Call LoadCallTypes(Combo1)
        Combo1.ListIndex = 0
    
    End Sub
    regards
    Chris
    i will try this code

    thank you very much

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: how to use DAO library to fill combo box

    Quote Originally Posted by hananfathy View Post
    i used the code

    vn.MoveFirst
    Combo1.Clear
    Do While Not vn.EOF
    Combo1.AddItem ("vn_nm")
    vn.MoveNext
    Loop
    So that you can LEARN (and not just copy code---i trust you wouldn't do that anyway), when you add something to your combobox, you need to reference what is in your recordset, vn.

    Instead of

    Code:
    Combo1.AddItem ("vn_nm")
    it would be something like this:

    Code:
    Combo1.AddItem (vn!fieldname)
    where 'fieldname' is the name of the field in your table that you want to add to the combo box.

    What you had would add (if there were at least one record in vn), "vn_nm" to your combobox (as many times as there were records returned).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width