I am trying to extract data from my access database into excel. I want to have a filter so that only data with a specific date is extracted. I try to include a WHERE condition for this reason.
When I run the following code, only the headers of my tables are extracted (1 row).
When I change Date=03/09/2013 to another condition like Index=SPX, I get all the data corresponding to SPX extracted which is what I want to happen but ith the date.
Any idea's why this might be happening? Code is as follows:
Sub ADOFromAccessToExcel()
Dim conn As ADODB.Connection, rec As ADODB.Recordset
Dim Cols As Integer
Dim DBName As String
Dim Extract As Range
DBName = "C:\Documents and Settings\uname\Desktop\Access DB\Yield2.mdb;"
Set Extract = Sheets("Sheet2").Range("A1")
Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBName & ";"
Set rec = New ADODB.Recordset
rec.Open "SELECT * FROM SPX WHERE Date=03/09/2013; ", conn, , , adCmdText
For Cols = 0 To rec.Fields.Count - 1
Extract.Offset(1, Cols).Value = rec.Fields(Cols).Name
Next
Extract.Offset(1, 0).CopyFromRecordset rec
rec.Close
Set rec = Nothing
conn.Close
Set conn = Nothing
End Sub