Results 1 to 5 of 5

Thread: Populating combo box with DB entries

  1. #1
    Member
    Join Date
    Apr 02
    Location
    Galway,Ireland
    Posts
    63

    Populating combo box with DB entries

    Hi. Can anyone tell me whats wrong with this code??

    VB Code:
    1. Private Sub Form_Activate()
    2.   Dim stSQL As String
    3.  
    4.     stSQL = "SELECT DISTINCT [Focus_Area_of_System] FROM [List_of_Current_Practices_SQE]"
    5.   With Adodc1
    6.       .CommandType = adCmdText
    7.       .RecordSource = stSQL
    8.       .Refresh
    9.      
    10.      Do Until .Recordset.EOF
    11.            .Recordset.MoveNext
    12.            If .Recordset![Focus_Area_of_System] <> "" Then
    13.               cboFocus_Area_of_System.AddItem Adodc1.Recordset![Focus_Area_of_System]
    14.           End If
    15.          
    16.           Loop
    17.   End With
    18.  
    19.   End Sub


    It keeps deleting entries from my DB and doesn't populate the combo box at all. I was using DAO with this code before and it worked ok but im changing to ADO and now i'm fetting errors everywhere. Any ideas?
    Cheers, Triona
    Caitriona

  2. #2
    Fanatic Member
    Join Date
    May 02
    Posts
    746
    Nothing jumped out at me. I assume you get the right resultset when you post the SQL statement in Access? FWIW, I'd ditch the bound controls - to include the ADO data control. I used to use them and found too many squirrely issues like yours. Just use the ADO objects and regular VB controls.

    How is Galway this time of year?

  3. #3
    Member
    Join Date
    Apr 02
    Location
    Galway,Ireland
    Posts
    63
    Thanks for that, no result as yet though!
    the SQL is workin fine in Access, i'm not well up on ADO so i reckon my mistake is there.....


    Galway is rockin' !
    Caitriona

  4. #4
    Fanatic Member
    Join Date
    May 02
    Posts
    746
    Could also be (b/c I don't remember exactly how Access handles nulls) that no entry <> evaluate to "". Blank is typically different than NULL. Try testing for NULL.

    You're already using ADO - mostly.

    Add a ref to MS ADO Data Objects 2.x Library (should already be there though due to the data control)

    Then add the following:
    VB Code:
    1. Private Sub Form_Activate()
    2.     Dim cn As ADODB.Connection
    3.     Dim rs As ADODB.Recordset
    4.     Dim sql As String
    5.     Set cn = New ADODB.Connection
    6.     With cn
    7.         .Provider = 'paste the provider portion of the connection string
    8.     'from your ado control as a string ex: "Microsoft.Jet.OLEDB.4.0"
    9.         .ConnectionString = 'paste the rest of the connection string
    10.         .Open
    11.     End With
    12.     Set rs = New ADODB.Recordset
    13.     sql = "SELECT DISTINCT [Focus_Area_of_System] FROM [List_of_Current_Practices_SQE]"
    14.     rs.Open sql, cn, adOpenForwardOnly, adLockReadOnly
    15.     Do Until rs.EOF
    16.         cboFocus_Area_of_System.AddItem rs!Focus_Area_of_System
    17.         rs.MoveNext
    18.     Loop
    19.     rs.Close
    20.     cn.Close
    21.     Set rs = Nothing
    22.     Set cn = Nothing
    23. End Sub

  5. #5
    Member
    Join Date
    Apr 02
    Location
    Galway,Ireland
    Posts
    63
    Sorted! Thanks a lot,
    Triona
    Caitriona

Posting Permissions

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