View Single Post
 
Old 10-18-2011, 04:57 PM
Joe Patrick Joe Patrick is offline Windows 7 64bit Office 2007
Advanced Beginner
 
Join Date: May 2011
Posts: 33
Joe Patrick is on a distinguished road
Default How to link a combobox to an access database?

Hi,

Can anyone help me with this?

I found this code but can't seem to get it to work.

Access file = PE_CG_list
Table = PE-GC-list
Column = PEs

I have the data source ending in .accdb because that's the path to the file although the original code said to put .mdb. When I put .mdb it says it can't find the file but with .accdb it says it doesn't know the database type.

I have references set to both ADO and Access.

Code:
Sub AutoOpen()
    On Error GoTo Document_Open_Err
    Dim cnn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=U:\TPCentral_Too\PE_GC_list.accdb"
.Open "SELECT [PEs] FROM PE-GC-list ORDER BY [PEs];", _
             cnn, adOpenStatic
    rst.MoveFirst
    With ThisDocument.ComboBox1
        .Clear
        Do
            .Add rst![PEs]
            rst.MoveNext
        Loop Until rst.EOF
    End With
Document_Open_Exit:
    On Error Resume Next
    rst.Close
    cnn.Close
    Set rst = Nothing
    Set cnn = Nothing
    Exit Sub
Document_Open_Err:
    MsgBox Err.Number & vbCrLf & Err.Description, vbCritical, "Error!"
    Resume Document_Open_Exit
End Sub
Any help is appreciated - thank you!
Reply With Quote