Excel vba SQL query with datediff

dsamorano

New Member
Joined
Jul 1, 2015
Messages
11
I have a SQL database with a date that is text format. I convert to a date on query the use datediff to see if another date is greater than 90 days different. I don't get a error yet it does not remove the SQL entries that are greater than 90 days

sql = "SELECT * FROM PipelineList WHERE DateDiff('d',format(cdate([appdate]),'mm/dd/yyyy'),format(Date(),'mm/dd/yyyy')) < " & 90 & " and [status1] <> '" & "ACTIVE" & " '"
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
if you run print debug sql just after the sql completes (add a break ponit), then take the sql and run it inside server management studio and see if it returns what you get now or acts as expected
 
Upvote 0
if you run print debug sql just after the sql completes (add a break ponit), then take the sql and run it inside server management studio and see if it returns what you get now or acts as expected
Not sure how to set a breakpoint. But was hoping that the 90 was not being converted to a number. Or something easy like that. Just seems im missing a simple fix
 
Upvote 0
in the vba macro, right click a line just after the debug, then look in the immediate window (Ctrl G) and copy the SQL from there
 
Upvote 0
in the vba macro, right click a line just after the debug, then look in the immediate window (Ctrl G) and copy the SQL from there

Added debug.print SQL immediate window did not show any result.

This code is just to look for records that are past 90 days and delete them.
 
Upvote 0
This is the code, even tried this but still not deleting records 'sql = "SELECT * FROM PipelineList WHERE [status1] <> '" & "ACTIVE" & " '"

Dim sql As String
Dim rs As DAO.Recordset
Dim CurrentDb As DAO.Database

Set CurrentDb = OpenDatabase(ThisWorkbook.Path & "/DATABASE/" & Left(ThisWorkbook.name, (InStrRev(ThisWorkbook.name, ".", -1, vbTextCompare) - 1)) & ".mdb")

MsgBox "start purge"

'sql = "SELECT * FROM PipelineList WHERE [status1] <> '" & "ACTIVE" & " '"
sql = "SELECT * FROM PipelineList WHERE DateDiff('d',format(cdate([appdate]),'mm/dd/yyyy'),format(Date(),'mm/dd/yyyy')) > " & 90 & " and [status1] <> '" & "ACTIVE" & " '"
Debug.Print sql
Set rs = CurrentDb.OpenRecordset(sql)

If rs.RecordCount < 1 Then

With rs

If Not .BOF And Not .EOF Then

.MoveLast

.MoveFirst

If .Updatable Then
.Delete

End If

End If

.Close

End With

Set rs = Nothing

End If

Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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