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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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