Depends houw you are trying to connect... ADO, DAO, ???. For ADO you need..
Microsoft ActiveX Data Objects 2.? Library.
One thing to note is that you may want to select a library somewhere around
2.5 in order to ensure backwards compatability with older version of XL.
--
HTH...
Jim Thomlinson
- Show quoted text -
Thanks for the response, Jim
I'm still kinda new to all of these so I don't really know the
difference btw ADO or DAO.
The code is below. What kind of connection is it? Thanks a bunch!
Public Sub GetUniqueDataFromAccessFields(MyDatabaseFilePathAndName As
String, MyTable As String, _
MyTableField1 As String, _
MyTableField2 As String, _
MyTableField3 As String, _
MyTableField4 As String, _
MyTableField5 As String, _
MyTableField6 As String, _
MyTableField7 As String, _
DestSheetRange As Range,
ClearRange As Boolean)
Dim MyConnection As String
Dim MySQL As String
Dim MyDatabase As Object
Dim I As Integer
Dim str1 As Variant
'If ClearRange = True clear all cells in column K:O
If ClearRange Then
Sheets(DestSheetRange.Parent.Name).Range(DestSheetRange.Address,
DestSheetRange.Offset(0, 4)).EntireColumn.ClearContents
'Create connection string
MyConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
MyConnection = MyConnection & "Data Source=" &
MyDatabaseFilePathAndName & ";"
' Create MySQL string
str1 = Array(MyTableField1, MyTableField2, MyTableField3,
MyTableField4, MyTableField5, MyTableField6, MyTableField7)
MySQL = ""
For I = LBound(str1) To UBound(str1)
If str1(I) <> "" Then
MySQL = "Select Distinct [" & str1(I) & "] From " &
MyTable
' Open the database and copy the data
On Error GoTo SomethingWrong
Set MyDatabase = CreateObject("adodb.recordset")
MyDatabase.Open MySQL, MyConnection, 0, 1, 1
' Check to make sure we received data and copy the data
If Not MyDatabase.EOF Then
'Copy to K:M in the Criteria sheet (Columns are
hidden)
DestSheetRange.Offset(0, I).CopyFromRecordset
MyDatabase
Else
MsgBox "No records returned from : " & str1(I),
vbCritical
End If
MyDatabase.Close
Set MyDatabase = Nothing
End If
Next I
Exit Sub
SomethingWrong:
On Error GoTo 0
Set MyDatabase = Nothing
MsgBox "Error copying unique data", vbCritical, "Test Access data
to Excel"
End Sub