ADO SQL when filtering records from MS-Access (solved)

dcalcoder

New Member
Joined
Nov 1, 2005
Messages
21
Hi Guys,

I've just joined the forum after reading all the helpful advice here on many occasions.

I'm a bit stuck with this one and I haven't seen any posts which relate directly to the same problem.

I'm using ADO SQL to read records back from an Access database table.

In the subroutine which follows, the code works fine when I want to bring back all records in the table. However, when I try and use SQL to bring back only records in a certain date range, I get nothing apart from the the table headers.

I'm not at all familiar with this area so it could be something really crazy I'm doing - like not having the correct SQL - though I don't have any errors reported.

Anyway here's the code, if anyone's got any ideas:

Sub ADOImportFromAccessTable(DBFullName As String, _
TableName As String, TargetRange As Range, _
DBStartDate As String, DBEndDate As String, _
DBStartTime As String, DBEndTime As String)

'Define the connection to the database
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
' open the recordset to select all records
' .Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable

' open the recordset with filtering
.Open "SELECT * FROM " & TableName & _
" WHERE DATE >= " & DBStartDate & _
" AND DATE <= " & DBEndDate & ";", cn, , , adCmdText

' write data from the recordset to the worksheet
For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub


Hi Guys,

I worked this out myself, the problem is that the SQL needs to enclose dates in #'s as in:

.Open "SELECT * FROM " & TableName & _
" WHERE Date >= #" & DBStartDate & "#" & _
" AND Date <= #" & DBEndDate & "#;", cn, , , adCmdText

Also my local settings were defaulting dates in date format fields earlier to be DD/MM/YY before they were copied into the string format dates.

The date format for use in VBA SQL can only be MM/DD/YYYY !!!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,203,238
Messages
6,054,312
Members
444,715
Latest member
GlitchHawk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top