Results 1 to 7 of 7

Thread: Inserting values from a database to combo box

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    2

    Unhappy Inserting values from a database to combo box

    Hi

    I have linked the combo box to the data control and the combo reads values from the database however dont know how to make the values list within the combo box. Any suggestions would be really appreciated!?!?!


    Thanks

    Roshny

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Inserting values from a database to combo box

    Welcome to the forums.

    I don't use bound controls, so I don't know that much about them, but I always thought the purpose of a bound control was to automatically populate controls, so I'm guessing that linking isn't the same as binding.

    When you say you have a combo box "linked" to a database [field], what do you mean?

    Do you know how to create and run a query and build a recordset from the results?

  3. #3
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Inserting values from a database to combo box

    If I am not mistaken, bound fields only apply to the current record, not all the records in the data set. I think you will need to use an unbound combox box and fill it with a select statement and a loop.

  4. #4
    New Member
    Join Date
    Apr 2005
    Location
    Jacksonville, FL
    Posts
    2

    Re: Inserting values from a database to combo box

    I think this might be what you are looking for...

    Set recordSet = New ADODB.recordSet
    SQLQuery = "select * from TABLE"
    recordSet.Open SQLQuery, connection, adOpenStatic

    If (recordSet.EOF = True And recordSet.BOF = True) Then
    Print "No records"
    Else
    Do Until recordSet.EOF = True
    data = recordSet.Fields("TABLE_DATA")
    comboBox1.AddItem data
    recordSet.MoveNext
    Loop
    End If
    recordSet.Close
    Last edited by manbeast7; Apr 14th, 2005 at 11:37 PM.

  5. #5
    Hyperactive Member
    Join Date
    Mar 2005
    Location
    Manila, Philippines
    Posts
    486

    Re: Inserting values from a database to combo box

    put this in form activate
    with data1.recordset
    .movefirst
    while not .eof
    combo1.additem .fields (0)
    .movenext
    wend
    end with

  6. #6
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: Inserting values from a database to combo box

    G'day,

    I am working on the same thing at the moment and here is my subroutine. Maybe you can work out something to suit your case.

    cmbContact is a combobox
    Contact is a field in my database called tblContacts

    VB Code:
    1. [color=#0d5692][color=#0d5692]Private Sub cmbContact_GotFocus(Index As Integer)
    2.   Dim Temp As Integer
    3.  
    4.     dtaContacts.RecordSource = "SELECT * FROM tblcontacts where Contact like '" & CustName & "*' "
    5.  
    6.   dtaContacts.RecordSource = "select * from tblContacts"
    7.   With dtaContacts.Recordset
    8.     .MoveLast
    9.     Temp = dtaContacts.Recordset.RecordCount
    10.     .MoveFirst
    11.     For i = 1 To Temp
    12.       cmbContact.Item(Index).AddItem .Fields!Contact
    13.       .MoveNext
    14.     Next
    15.   End With
    16.    
    17.    
    18. End Sub[/color][/color]
    19. [color=#0d5692][/color]


    Peter

  7. #7
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    Re: Inserting values from a database to combo box

    G'day,

    Don't know what happened to previous post. Try this.



    Private Sub cmbContact_GotFocus(Index As Integer)
    Dim Temp As Integer



    dtaContacts.RecordSource = "select * from tblContacts"

    With dtaContacts.Recordset
    .MoveLast
    Temp = dtaContacts.Recordset.RecordCount
    .MoveFirst
    For i = 1 To Temp
    cmbContact.Item(Index).AddItem .Fields!Contact
    .MoveNext
    Next

    End With

    End Sub




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