Results 1 to 11 of 11

Thread: How do i fill Combobox using ADO from access database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Location
    PUNE
    Posts
    222

    Lightbulb How do i fill Combobox using ADO from access database

    How do i fill Combobox usiing ADO from access database

    When i will select the the particular value from combo box corresponding value (Multicolumn )against that column should be populates

    thanks

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: How do i fill Combobox using ADO from access database

    Hi!!!

    How do i fill Combobox usiing ADO from access databa

    If your using the ado data control (adodc), specifiy the data source to point to the ado data control and simply change the DataField property to select to a particular field..

    Or you may use the ADODB.Connection and a recordset to populate the combo box...What I mean is you have code this and use the AddItem method to populate the combo box...

    Example:

    VB Code:
    1. cboSupplier.AddItem(rstSupplier.Fields("Supplier_Name"))


  3. #3
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: How do i fill Combobox using ADO from access database

    There are a lot of examples, tutorials on the web how to do that.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim cn As ADODB.Connection
    4. Dim rsPlaats As ADODB.Recordset
    VB Code:
    1. Private Sub Form_Load()
    2. On Error GoTo ErrHandler
    3.  
    4.     Set cn = New ADODB.Connection
    5.         cn.Provider = "Microsoft.Jet.OLEDB.4.0"
    6.         cn.ConnectionString = "Data Source = C:\Test.mdb;" & "Persist Security Info = False"
    7.         cn.Open
    8.  
    9. Set rsPlaats = New ADODB.Recordset
    10.         rsPlaats.Open "SELECT DISTINCT Fieldnumber from NameDatabase", cn, adOpenForwardOnly, adLockReadOnly
    11.  
    12. Do Until rsPlaats.EOF
    13.             Combo1.AddItem (rsPlaats!Fieldnumber)
    14.             rsPlaats.MoveNext
    15.         Loop
    16.  
    17.         rsPlaats.Close
    18.     Set rsPlaats = Nothing
    19. Exit Sub
    20.  
    21. ErrHandler:
    22.     MsgBox Err.Description
    23. End Sub
    VB Code:
    1. Private Sub Combo1_Click()
    2. On Error Resume Next
    3.  
    4.     Set rsPlaats = New Recordset
    5.         rsPlaats.Open "SELECT * FROM NameDatabase WHERE Fieldnumer = '" & Combo1.Text & "'", cn, adOpenForwardOnly, adLockReadOnly
    6.  
    7. If Not rsPlaats.EOF And Not rsPlaats.BOF Then Text1.Text = rsPlaats.Fields("Field2").Value
    8.         If Not rsPlaats.EOF And Not rsPlaats.BOF Then Text2.Text = rsPlaats.Fields("Field3").Value
    9.  
    10.         rsPlaats.Close
    11.     Set rsPlaats = Nothing
    12. End Sub
    This is just a very simple example. Hope you can use it.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: How do i fill Combobox using ADO from access database

    Here's my function...

    VB Code:
    1. Public Sub PopulateData(objObject As Object, ByVal pSQL As String, ByVal FieldName As String, Optional ByVal WithNull As Boolean = True)
    2.     objObject.Clear
    3.     Dim rsRecords   As ADODB.Recordset
    4.     Set rsRecords = New ADODB.Recordset
    5.     With rsRecords
    6.         .Open pSQL, connImport, adOpenForwardOnly, adLockReadOnly
    7.         If Not (.EOF And .BOF) Then
    8.             Do While Not .EOF
    9.                 objObject.AddItem .Fields(FieldName)
    10.                 .MoveNext
    11.             Loop
    12.         End If
    13.         If WithNull = True Then
    14.             objObject.AddItem vbNullString
    15.         End If
    16.         .Close
    17.     End With
    18.     Set rsRecords = Nothing
    19. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: How do i fill Combobox using ADO from access database

    Quote Originally Posted by vaishali
    When i will select the the particular value from combo box corresponding value (Multicolumn )against that column should be populates

    thanks
    Multicolumn combo box?

  6. #6
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: How do i fill Combobox using ADO from access database

    I think he means a listbox or somesort of control.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  7. #7
    Member
    Join Date
    Jan 2006
    Posts
    62

    Re: How do i fill Combobox using ADO from access database

    hi,
    I am using adodb.connection. I am able to fill the combo with the data.
    My Query:
    I have State_Code,State_Name.
    In the combo, i want to display state_name and store the state_code of the selected state_name in the combo because only showing the state_code will not have a meaning.
    pl suggest the best way.

    regards

  8. #8
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: How do i fill Combobox using ADO from access database

    I think that you need a grid for that, 'cause you have to Fields. One for the State_Name and one for the State_Code. Unless you know how to add a new row in a Listbox. Do not use a Combobox for this problem. Well..... that wouldn't be my solution.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How do i fill Combobox using ADO from access database

    If the State_Code is numeric, you can put it into the hidden "ItemData" property of the combo, which works in the same way as the "List" property.

    To add ItemData (associated with the latest 'AddItem'), you can use this:
    Code:
    With combo1
      .AddItem "Number 1"
      .ItemData(.NewIndex) = 1
    End With
    ..and to read it back again you can use this:
    Code:
    With combo1
      MsgBox "List data: " & .List(.ListIndex)
      MsgBox "ItemData: " & .ItemData(.ListIndex)
    End With

  10. #10
    Junior Member
    Join Date
    Nov 2017
    Posts
    21

    Re: How do i fill Combobox using ADO from access database

    Any idea how to do this for .accdb database? Access 2010...

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: How do i fill Combobox using ADO from access database

    It doesn't matter if the database is .mdb or .accdb , the methods are the same... assuming of course that you are using the same language.

    However, based on your thread inserting data in combo box I see that you are using a different language to that above. While VBA is similar to VB6 in many cases, controls are one of the areas where things are different, so the same code is unlikely to work.

    If the VB6 code above doesn't work for you then the best thing to do is ask in the Office Development (VBA) forum, as you are already doing.

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