Excel VBA Question: Return if Between 2 Dates

S_W_Langdon

New Member
Joined
Feb 5, 2018
Messages
13
Hi Everyone,

I have had a scan (search) through the posts but am unable to find something on this, so sorry if I missed it, or searched wrong...

I have an Access Database and am trying to pull information out of it into excel based on 2 dates within cells, now I can get it to pull the information from the Database, but it isn't pulling it correctly, instead of pulling all data between the 2 dates it is pulling everything (before, after, between)

Here is the code that I have setup (Var & Var1 are the reference cells)

If Sheet2.Range("AL2").Value = "Yes" Then
SQL = "SELECT * FROM DatabaseTable WHERE StatusStartDate < #" & var1 & "#Then"
SQL = "SELECT * FROM DatabaseTable WHERE StatusStartDate >= #" & var & "#"
Else
SQL = "SELECT * FROM DatabaseTable WHERE StatusStartDate LIKE '" & var & "%" & "'"
End If

I have tried changing the <> on different rows to see if that helps, but it isn't working (am more used to Excel Formula I am afraid)

Any help people can give would be great

Thanks
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi again everyone,

I had a random thought and it worked, ended up being a very simple fix in the end, please see code below

If Sheet2.Range("AL2").Value = "Yes" Then
SQL = "SELECT * FROM DatabaseTable WHERE StatusStartDate BETWEEN #" & var & "# AND #" & var1 & "#"
Else
SQL = "SELECT * FROM DatabaseTable WHERE StatusStartDate LIKE '" & var & "%" & "'"
End If

Just need to get my head out of Excel Formula and back to VBA/ SQL since it has been years

Anyway, hope that this helps someone else in the future :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

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