type mismatch error in SQL query in excel vba

Vijay121

New Member
Joined
Oct 9, 2011
Messages
2
Hi all,
I am getting error 'Type Mismatch' while copying data from access to excel sheet after giving Date criteria in SQL query.
I have users data in Access table containing Processed date field also.
I have two cells in excel sheet namely "From Date" and "To Date".
When I put dates in both cells then I should get data from access table in given range dates.
e.g. If I insert 9/25/2011(mm/dd/yyyy) in "From Date" cell and 10/9/2011(mm/dd/yyyy) in "To Date" cell then I should get data from access
into excel sheet in given dates only which is not happening.
Here is main part of the code:
Dim sSQL As String
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim StartDate, EndDate
StartDate = Trim(ThisWorkbook.Sheets("Reports").Range("G16").Value)
EndDate = Trim(ThisWorkbook.Sheets("Reports").Range("J16").Value)
'StartDate = Trim(ThisWorkbook.Sheets("Reports").TextBox1.Text)
'EndDate = Trim(ThisWorkbook.Sheets("Reports").TextBox2.Text)
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Vijay\Testing_DB.accdb;Persist Security Info=False;Jet OLEDB:Database Password=123"
sSQL = "SELECT SaveDataInDB.* FROM SaveDataInDB WHERE (((SaveDataInDB.[Processed Date]) between '" & StartDate & "' and '" & EndDate & "'));"
'sSQL = "SELECT SaveDataInDB.* FROM SaveDataInDB WHERE (((SaveDataInDB.[Processed Date])>='" & StartDate & "') AND ((SaveDataInDB.[Processed Date])<='" & EndDate & "'));"
Set rs = New ADODB.Recordset
rs.Open sSQL, cn
ThisWorkbook.Sheets("User_Data").Range("A2").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

>>>>
Note: I have Processed Date field as Date with format Short Date in access database
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi

Dates in Access need to be enclosed with # symbols e.g.

sSQL = "SELECT SaveDataInDB.* FROM SaveDataInDB WHERE (((SaveDataInDB.[Processed Date]) between #" & StartDate & "# and #" & EndDate & "#));"



HTH
DK
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,182
Members
448,872
Latest member
lcaw

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