Errors in SELECT query from Excel to Access

ChrisBrook

New Member
Joined
Jan 15, 2016
Messages
12
I'm trying to produce an Excel workbook which connects to my access database and extracts values from a table based on whether those records have a Request_Date between two strings.
The code I have written works fine if I substitute the strings for actual dates however when I then try to replicate this with strings I get the above error, followed by an additional error 'Operation is not allowed when the object is closed'
Below is the code I am using:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;">Public Sub RetrievePaymentMI()
Dim Date1
As Date
Dim Date2
As Date
Dim LastRow
As Long
Dim MIdb
As String
Dim MIpass
As String

Sheets
("Data").Range("B5:N1000").Clear

Date1
= Sheets("Summary").Cells(3, 4).Value
Date2
= Sheets("Summary").Cells(4, 4).Value

OpenDataBase MIdb
, MIpass

sSQL
= "SELECT Policy_Number, Customer_Name, Request_Date, Payment_Type, Dual_Amt, Int_Amt, DnI_Amt, Resolve, Payment_Method, Payment_Description, Further_Info, TL_Name, Status " & _
"FROM tblData " & _
"WHERE Request_Date > Date1 AND Request_Date < Date2 "

RetrieveDataBaseData
(sSQL)

Sheets
("Data").Cells(5, 2).CopyFromRecordset RecordSet

RecordSet
.Close
MIDatabaseconnection
.Close

MsgBox
"Complete"

End Sub</code>I've attempted numerous different ways of doing this, I've declared Date1 & Date2 as strings and dates, I've changed their names. I've attempted enclosing them in # #, " " & ' '.
HELP!!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
So I now have:

Code:
sSQL = "SELECT Policy_Number, Customer_Name, Request_Date, Payment_Type, Dual_Amt, Int_Amt, DnI_Amt, Resolve, Payment_Method, Payment_Description, Further_Info, TL_Name, Status FROM tblData WHERE Request_Date > " & Date1 & " AND Request_Date < " & Date2 & ""
And it runs through with no errors but now it isn't retrieving any data from the tables.
 
Upvote 0
Access generally likes dates enclosed in # so:

Code:
sSQL = "SELECT Policy_Number, Customer_Name, Request_Date, Payment_Type, Dual_Amt, Int_Amt, DnI_Amt, Resolve, Payment_Method, Payment_Description, Further_Info, TL_Name, Status FROM tblData WHERE Request_Date > #" & Date1 & "# AND Request_Date < #" & Date & "#"
 
Upvote 0
Access generally likes dates enclosed in # so:

Code:
sSQL = "SELECT Policy_Number, Customer_Name, Request_Date, Payment_Type, Dual_Amt, Int_Amt, DnI_Amt, Resolve, Payment_Method, Payment_Description, Further_Info, TL_Name, Status FROM tblData WHERE Request_Date > #" & Date1 & "# AND Request_Date < #" & Date & "#"

Brilliant, that's worked a treat! Cheers!
 
Upvote 0
Glad to help. Welcome to the forum, by the way.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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