Something like this. Change sCnn to the filepath of you db. Also depending on what Access data base you are using change for your needs. .2003 Access Use Jet
2007 db or higher use ACE
Public Sub GetData()
' 30-Dec-2007, working in Excel 2000-2007
Dim oCnn As Object
Dim oRs As Object
Dim sCnn As String
Dim sSQL As String
'If Val(Application.Version) < 12 Then
sCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Mike\Desktop\db1.mdb; Persist Security Info=False;"
'Else
'sCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb; Persist Security Info=False;"
'End If
sSQL = "SELECT Table1.Fruit FROM Table1 WHERE (((Table1.Color)='" & Cells(2, 1).Value & "'));"
On Error GoTo SomethingWrong
Set oCnn = CreateObject("ADODB.Connection")
Set oRs = CreateObject("ADODB.Recordset")
oCnn.Open sCnn
oRs.Open sSQL, oCnn, 0, 1, 1
' Check to make sure we received data and copy the data
If Not oRs.EOF Then
Cells(2, 2).CopyFromRecordset oRs
Else
MsgBox "No records returned from : " & sSQL, vbCritical
End If
' Clean up our Recordset object.
oRs.Close
Set oRs = Nothing
oCnn.Close
Set oCnn = Nothing
Exit Sub
SomethingWrong:
MsgBox "Err Num : " & Err.Number & vbCrLf & "Error Description :" & Err.Description, vbExclamation, "Error"
On Error GoTo 0
End Sub
Bookmarks