Check if Date is 2 months prior to today's date - vba

I want to check if a variable date is in the month that is two months prior to today's date.
Is there a way (using a built in date funciton like dateadd()) to cover the edge cases of when month(now())=1 or month(now())=2 in more elegant manner than the following?
?format(month(now()) & " " & year(now()),"MMMM YYYY")=format(dateadd("M",2,format(varMonth & " " & varYear, "MMMM YYYY")),"MMMM YYYY")

You can use the DateDiff function with the "m" interval to calculate the difference in months between two dates.

Related

Access Date Query Problem for Today's Date

I have a set of data that includes a field called ReleaseDate, type of Date/Time, that gets written using NOW() at the time a record is added.
I can open the table and filter for today's date and get a result of 152 records.
I can run this following query, enter today's date at the prompts, and also get a result of 152 records.
SELECT ProductData.ReleaseDate, ProductData.Shift, ProductData.ExtrusionLine, ProductData.RollDensity
FROM ProductData
WHERE (((ProductData.ReleaseDate) Between [Please Enter Start Date mm/dd/yyyy] & " " & #12/30/1899# And [Please Enter End Date mm/dd/yyyy] & " " & #12/30/1899 23:59:59#));
If I run the following query no records are retrieved. I cannot figure out why.
SELECT ProductData.ReleaseDate, ProductData.Shift, ProductData.ExtrusionLine, ProductData.RollDensity
FROM ProductData
WHERE (((ProductData.ReleaseDate)=Date()));
From what I have read Date() as a criteria should return any records with today's date. Prompting for the date is not an option.
In addition to Gutav's answer, better use:
WHERE ProductData.ReleaseDate >= Date() AND ProductData.ReleaseDate < DateAdd("d", 1, Date())
as this can use an index on ProductData.ReleaseDate (faster). See my answer on Unable to use today's date as a criteria in a query
It's because ReleaseDate contains a time part (from Now()). You can strip that:
WHERE Fix(ProductData.ReleaseDate) = Date();

Get past 6 months MONTH in ref to TODAY

Need help in displaying 6 month past month in refrence to todays date. E.g current month is June. below is the code, but is giving the as 1,I need to get January.
MsgBox ("Cheques would be checked for issue Month of " & (Month(Date)-5))
MsgBox ("your date is " & DateAdd("m", -6, Date()))

how to substract only working days from a specific date?

assuming that we have a date of 18/06/2015 for example, and we want to substruct 5 days from it, (Saturday and Sunday not included).
I have thought to elaborate an algorihm with DateAdd()but i wanna know wether there is a function that that enables to do so
thanks in advance
Try the workday worksheet function:
WorksheetFunction.WorkDay("2015-06-18",-5)
=WORKDAY(A1,5)
Assuming you have whatever original date in cell A1.
It worked:
Fecha_Inicio = (Extr_DiaAp & " - " & Extr_MesAp & " - " & Extr_YearAp)
'the last was just a Date that i extracted from other cells.
Fecha_Comp = WorksheetFunction.WorkDay(Fecha_Inicio, 25)
'Fecha_Comp is a Date that starts from Fecha_Inicio 25 working days ahead.
MsgBox Fecha_Comp

Determine number of days between dates

How do I get the whole date difference in VBA?
I know that to get the year or the month or the day I do:
DateDiff("yyyy", Me.DateofBirth, Me.Year).
I want the whole difference. Not just the year, and the two columns that I have are Date of Birth and Year.
Dates are stored internally in VBA as doubles, with the integer portion as the number of days since 1/1/1900. To get the difference between two dates, you can just subtract them:
Dim dob As Date
dob = DateSerial(1990, 1, 1)
Dim difference As Date
difference = Now - dob
Debug.Print Year(difference) - 1900 & " years, " & _
Month(difference) & " months, " & _
Day(difference) & " days."
If you want the total number of days, you can just subtract them and use the numeric value of the underlying double:
Dim dob As Date
dob = DateSerial(1990, 1, 1)
Dim days As Long
days = Now - dob
Debug.Print days & " days."

Datatable compute: filtering by date

I have a datatable with 2 columns: "amount" and "date"
I want to sum "amount" by month & year.
I am trying this:
_tAmount = myDT.Compute("sum(amount)", "date LIKE '%/" & i & "/" & _year & "'")
where:
var i equals a nº month ( 1 to 12)
var _year equals nº year (example:
2011)
But not displays any results....How I can do it? What is wrong?
I am working with the spanish format (example day/month/year). But I have tried with the english format (month/day/year) and no results too.
This is on VB.NET 2008.
I don't thing date works with LIJKE, why don't you use:
dim startdt as date = new date(_year, month, 1)
dim enddt as date = startdt.addmonth(1).adddays(-1)
_tAmount = myDT.Compute("sum(amount)", "date >= " & startdt & " AND date <= " & enddt)
This is out the top of my head, so check it yourself, but you should get the idea.
Also, I use mostly the # sign around the dates to force english notation, but you'll have to experiment yourself.