Results 1 to 20 of 20

Thread: ADO and comboboxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    ADO and comboboxes

    I am working on a Visual Basic program using adodc to write and read from an Access database.
    I have the following code:

    Private Sub Form_Load()

    With Adodc1
    .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    App.Path & "\DataBase.mdb;Persist Security Info=False"

    .RecordSource = "select FName from Table1 order by Fname"
    End With

    End Sub


    I want to fill the list of a combobox with the names that the query returns. Please help!
    Thanks in advance!

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

    Re: ADO and comboboxes

    Welcome to the forums.

    It has been years beyond count since I've used bound controls, so this is something of a shot in the dark, but combo boxes have a datasource property and I think you need to bind that to your data control that does the query.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: ADO and comboboxes

    Nice to meet you.
    Yes, I know that there is a datasource property but I don't know the way to bind it...

  4. #4
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: ADO and comboboxes

    You actually want rowsource rather than datasource. Since you've got a reference to ADODC1 in your code I assume you've put a ADO Data Controller on your form and called it ADODC1. Select your datacombo and click on it's row source property, which should turn into a dropdown. Drop it down and you should see that your ADO data control is in the list, select it and you're done.

    You'll probably have to call ADODC1.refresh to get the data control to pick up the data though.

    BTW the datasource property is basically the same except it filters by the value in the bindid column and is used for showing information from child tables etc.

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

    Re: ADO and comboboxes

    Quote Originally Posted by FunkyDexter
    You actually want rowsource rather than datasource. Since you've got a reference to ADODC1 in your code I assume you've put a ADO Data Controller on your form and called it ADODC1. Select your datacombo and click on it's row source property, which should turn into a dropdown. Drop it down and you should see that your ADO data control is in the list, select it and you're done.

    You'll probably have to call ADODC1.refresh to get the data control to pick up the data though.

    BTW the datasource property is basically the same except it filters by the value in the bindid column and is used for showing information from child tables etc.
    I don't think he is using a datacombo, but rather a regular, standard VB combo. Will this still work with that control?

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: ADO and comboboxes

    Yes I use the standard VB combo and it doesn't work...
    I think that the solution is through code but I don't know the code I have to write...
    Maybe using a loopin additem way? Really I don't know...

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

    Re: ADO and comboboxes

    Now writing code to do this is something I know how to do.
    VB Code:
    1. Dim ADOCn As ADODB.Connection
    2. Dim ConnString As String
    3. Dim adoRS As ADODB.Recordset
    4. Dim sSQL As String
    5.  
    6. ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    7. App.Path & "\DataBase.mdb;Persist Security Info=False"
    8.  
    9. Set ADOCn = New ADODB.Connection
    10. ADOCn.ConnectionString = ConnString
    11. ADOCn.Open ConnString
    12. Set adoRS = New ADODB.Recordset
    13. sSQL = "select FName from Table1 order by Fname"
    14. adoRS.Open sSQL, ADOCn
    15. Do Until adoRS.EOF
    16.     Combo1.AddItem adoRS.Fields.Item("fname").Value
    17.     adoRS.MoveNext
    18. Loop
    19. adoRS.Close
    20. ADOCn.Close
    21. Set ADOCn = Nothing
    22. Set adoRS = Nothing

  8. #8
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: ADO and comboboxes

    I don't think he is using a datacombo, but rather a regular, standard VB combo. Will this still work with that control?
    Ooops, my mistake . No I don't think it'll work with a standard combo. I think you can bind a standard combo but I've always used code instead - much more control.

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    4

    Re: ADO and comboboxes

    Well done! It worked! Thanks a lot guys!

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

    Re: ADO and comboboxes

    Quote Originally Posted by FunkyDexter
    Ooops, my mistake . No I don't think it'll work with a standard combo. I think you can bind a standard combo but I've always used code instead - much more control.
    No need to be embarrassed Funky. I don't use bound controls so I actually didn't know the answer to that and was curious as it could be asked by someone else at some point.

    Thanks for the info.
    Quote Originally Posted by Volrath1979
    Well done! It worked! Thanks a lot guys!
    Good deal!

  11. #11
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: ADO and comboboxes

    Quote Originally Posted by Hack
    Now writing code to do this is something I know how to do.
    VB Code:
    1. Dim ADOCn As ADODB.Connection
    2. Dim ConnString As String
    3. Dim adoRS As ADODB.Recordset
    4. Dim sSQL As String
    5.  
    6. ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
    7. App.Path & "\DataBase.mdb;Persist Security Info=False"
    8.  
    9. Set ADOCn = New ADODB.Connection
    10. ADOCn.ConnectionString = ConnString
    11. ADOCn.Open ConnString
    12. Set adoRS = New ADODB.Recordset
    13. sSQL = "select FName from Table1 order by Fname"
    14. adoRS.Open sSQL, ADOCn
    15. Do Until adoRS.EOF
    16.     Combo1.AddItem adoRS.Fields.Item("fname").Value
    17.     adoRS.MoveNext
    18. Loop
    19. adoRS.Close
    20. ADOCn.Close
    21. Set ADOCn = Nothing
    22. Set adoRS = Nothing
    Will similar code work with excel?

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

    Re: ADO and comboboxes

    Quote Originally Posted by lesthan60
    Will similar code work with excel?
    How do you mean?

    What do you have and what do you need done with it?

  13. #13
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: ADO and comboboxes

    The form I will be building will need to read and write data to an excel file.
    Last edited by lesthan60; Sep 17th, 2005 at 02:34 PM. Reason: more info needed

  14. #14
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: ADO and comboboxes

    Quote Originally Posted by lesthan60
    The form I will be building will need to read and write data to an excel file.
    So, will a similar code work with excel?

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

    Re: ADO and comboboxes

    You are not connecting to a database, but a Excel spreadsheet. The approach is completely different.

    The first thing you would need to do is set a reference to the Microsoft Excel Object Library, and open your sheet up. Have you ever done that before?

  16. #16
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: ADO and comboboxes

    No, I've not.

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

    Re: ADO and comboboxes

    Ok. From the VB IDE, click on Project/References

    Locate the Microsoft Excel Object Library (x.x) (where x.x is the version you have)

    Click on the Checkbox, then click OK.

    Now that you have a reference to Excel, you can open your worksheet.
    VB Code:
    1. Private Sub cmdOpenWorkSheet_Click()
    2. Dim xls As Excel.Application
    3.          Dim xlBook As Excel.Workbook        
    4.           Set xls = New Excel.Application
    5.          Set xlBook = xls.Workbooks.Open(App.Path & "\yourworksheetname.xls")
    6. End Sub

  18. #18
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: ADO and comboboxes

    Thanks Hack, OK - I've located and clicked reference to Excel, I've created a form and I've pasted your code to that form and I've created a Excel file and added that name to your code.
    When I click text1, readwritetest.xls then open readwritetest.xls (read only) cell A1 has not changed. After exiting VB readwritetest.xls remains as read only.

    visual basic code:-------------------------------------------------------------------------------

    Private Sub cmdOpenWorkSheet_Click()
    Dim xls As Excel.Application
    Dim xlBook As Excel.Workbook
    Set xls = New Excel.Application
    Set xlBook = xls.Workbooks.Open(App.Path & "\readwritetest.xls")
    xlBook.Sheets("sheets").Cells(1, 1) = Me.Text1
    End Sub

    Private Sub Text1_Click()
    Call cmdOpenWorkSheet_Click
    End Sub
    Last edited by lesthan60; Sep 19th, 2005 at 11:27 AM.

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

    Re: ADO and comboboxes

    Cool.

    If you have questions on the reading and writing part, I would suggest you create a new thread in either Office Development or ClassicVB in which you could post your specific questions about your Excel Code.

    This is, after all, Volrath1979's thread and dealth with ADO and Combo boxes. We are going off in a whole new direction here, and I think you need to start your own thread with your own specific questions.

  20. #20
    Junior Member
    Join Date
    Sep 2005
    Location
    Camden, Tennessee
    Posts
    19

    Re: ADO and comboboxes

    Yes, I'll start a new thread in Office Development. Thanks.

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