Hi folks,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
I’m fairly new to this but have had some success working in Excel with copying of data to ACCESS.accdb tables and from ACCESS queries back to Excel. Currently I am using ADO to first copy a list of available report dates from ACCESS to Excel. This list then comprises the choices in a data validation list box from which the user can choose which report date they want. Then I am using a SQL statement (again in Excel ) to get the data I want from an ACCESS standard “select” query, and apply the previously chosen date as the criteria, such that only the data for the chosen report date will be copied over to Excel. My code works fine for any text or numeric criteria but fails as soon as I attempt to use a date, even though these dates have been copied over from the ACCESS db. Here’s the code starting after obtaining the list of report dates:<o></o>
<o>
</o>
'Activex data objects 2.8 referenced<o></o>
<o></o>
Dim DBFullName As String<o></o>
Dim Connection1 As ADODB.Connection<o></o>
Dim Recordset1 As ADODB.Recordset<o></o>
Dim rptDate As String<o></o>
Dim sSQL as String<o></o>
<o> </o>
Sheets("Weekly_Drilling_status").Activate<o></o>
<o></o>
' Connect to Access Database<o></o>
DBFullName = ThisWorkbook.Path & "\Drill progress.accdb"<o></o>
Set Connection1 = New ADODB.Connection<o></o>
Connection1.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"<o></o>
Set Recordset1 = New ADODB.Recordset<o></o>
<o></o>
rptDate = ActiveSheet.Cells(1, 4).Value 'This is the chosen report date 'in dd.mm.yyyy and the debugger confirms this
sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = '# " & rptDate & "#'"<o></o>
<o></o>
With Recordset1<o></o>
.Open sSQL, Connection1 ‘Here is where I get the error 13 “data type mismatch in criteria expression”<o></o>
<o>
</o>
Note that this works fine: 'sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = #02/08/2012#"<o></o>
But this doesn’t: 'sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = #08.02.2012#"<o></o>
The only difference being the choice of date separator. Oddly, in ACCESS the query works fine if I type #08.02.2012# but in SQL view the periods are changed to slashes even though I have a period set for the date separator in my regional settings.<o></o>
<o> </o>
Here are some other things I’ve tried but to no avail:<o></o>
<o> </o>
'ActiveSheet.Cells(1, 4).Value = DateSerial(Right([ActiveSheet.Cells(1, 4).Value], 4), Mid([ActiveSheet.Cells(1, 4).Value], 4, 2), Left([ActiveSheet.Cells(1, 4).Value], 2))<o></o>
'ActiveSheet.Cells(1, 4).NumberFormat = "dd/mm/yyyy"<o></o>
'rptDate = DateSerial(ActiveSheet.Cells(1, 4).Value)<o></o>
rptDate = Format([ActiveSheet.Cells(1, 4).Value)], "dd/mm/yyyy")<o></o>
'rptDate = Format(rptDate, "short date")<o></o>
'rptDate = Format([ActiveSheet.Cells(1, 4).Value)], "dd" & "/" & "mm" & "/" & "yyyy")<o></o>
'rptDate = DateValue(Day([ActiveSheet.Cells(1, 4).Value)]) & "/" & Month([ActiveSheet.Cells(1, 4).Value)]) & "/" & Year([ActiveSheet.Cells(1, 4).Value)]))<o></o>
'rptDate = Format(rptDate, "dd" & "/" & "mm" & "/" & "yyyy")<o></o>
'rptDate = Format([ActiveSheet.Cells(1, 4).Value)], "short date")<o></o>
'sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = '#" & Format(rptDate, "dd/mm/yyyy") & "#'"<o></o>
<o> </o>
I know about data types and working with dates in ACCESS but I’m stymied with this. Appreciate any help.<o></o>
Cheers.
I’m fairly new to this but have had some success working in Excel with copying of data to ACCESS.accdb tables and from ACCESS queries back to Excel. Currently I am using ADO to first copy a list of available report dates from ACCESS to Excel. This list then comprises the choices in a data validation list box from which the user can choose which report date they want. Then I am using a SQL statement (again in Excel ) to get the data I want from an ACCESS standard “select” query, and apply the previously chosen date as the criteria, such that only the data for the chosen report date will be copied over to Excel. My code works fine for any text or numeric criteria but fails as soon as I attempt to use a date, even though these dates have been copied over from the ACCESS db. Here’s the code starting after obtaining the list of report dates:<o></o>
<o>
Code:
'Activex data objects 2.8 referenced<o></o>
<o></o>
Dim DBFullName As String<o></o>
Dim Connection1 As ADODB.Connection<o></o>
Dim Recordset1 As ADODB.Recordset<o></o>
Dim rptDate As String<o></o>
Dim sSQL as String<o></o>
<o> </o>
Sheets("Weekly_Drilling_status").Activate<o></o>
<o></o>
' Connect to Access Database<o></o>
DBFullName = ThisWorkbook.Path & "\Drill progress.accdb"<o></o>
Set Connection1 = New ADODB.Connection<o></o>
Connection1.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBFullName & ";"<o></o>
Set Recordset1 = New ADODB.Recordset<o></o>
<o></o>
rptDate = ActiveSheet.Cells(1, 4).Value 'This is the chosen report date 'in dd.mm.yyyy and the debugger confirms this
sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = '# " & rptDate & "#'"<o></o>
<o></o>
With Recordset1<o></o>
.Open sSQL, Connection1 ‘Here is where I get the error 13 “data type mismatch in criteria expression”<o></o>
<o>
Code:
Note that this works fine: 'sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = #02/08/2012#"<o></o>
But this doesn’t: 'sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = #08.02.2012#"<o></o>
The only difference being the choice of date separator. Oddly, in ACCESS the query works fine if I type #08.02.2012# but in SQL view the periods are changed to slashes even though I have a period set for the date separator in my regional settings.<o></o>
<o> </o>
Here are some other things I’ve tried but to no avail:<o></o>
<o> </o>
'ActiveSheet.Cells(1, 4).Value = DateSerial(Right([ActiveSheet.Cells(1, 4).Value], 4), Mid([ActiveSheet.Cells(1, 4).Value], 4, 2), Left([ActiveSheet.Cells(1, 4).Value], 2))<o></o>
'ActiveSheet.Cells(1, 4).NumberFormat = "dd/mm/yyyy"<o></o>
'rptDate = DateSerial(ActiveSheet.Cells(1, 4).Value)<o></o>
rptDate = Format([ActiveSheet.Cells(1, 4).Value)], "dd/mm/yyyy")<o></o>
'rptDate = Format(rptDate, "short date")<o></o>
'rptDate = Format([ActiveSheet.Cells(1, 4).Value)], "dd" & "/" & "mm" & "/" & "yyyy")<o></o>
'rptDate = DateValue(Day([ActiveSheet.Cells(1, 4).Value)]) & "/" & Month([ActiveSheet.Cells(1, 4).Value)]) & "/" & Year([ActiveSheet.Cells(1, 4).Value)]))<o></o>
'rptDate = Format(rptDate, "dd" & "/" & "mm" & "/" & "yyyy")<o></o>
'rptDate = Format([ActiveSheet.Cells(1, 4).Value)], "short date")<o></o>
'sSQL = "SELECT * FROM [qry1d_DrillProgress_data_for_Excel_plot] WHERE mydate = '#" & Format(rptDate, "dd/mm/yyyy") & "#'"<o></o>
<o> </o>
I know about data types and working with dates in ACCESS but I’m stymied with this. Appreciate any help.<o></o>
Cheers.