First Day Of Month For Calculated Number Of Months - vba

I'm running into an issue where I need some more guidance. I've got a form that allows users to enter a start date and end date to represent the length of a contract. I'm using the following query to calculate the monthly amount :
SELECT t_Actuals.InvoiceAmount,
(DateDiff("m",[StartDate],[EndDate])+1) AS [Contract Term],
CCur(Round(([InvoiceAmount]/[Contract Term]),0)) AS MonthlyAmount
FROM t_Actuals;
Our VP wants the MonthlyAmount projected throughout the length of the Contract Term (ie. 11 months from Start Date). Ideally, I would like to allocate the MonthlyAmount in a MM-YYYY (01/2012, 02,2012) format throughout the length of the contract term, but, admittedly, I am not that experienced with working with dates in Access outside of the Query Grid.
Regardless, I've been reading about the DateSerial function to find the first day of the month, but how would you implement that into a loop to find the value of the first date of the month for the next 36 months and write that date to a table with the projected date as fldDate and MonthlyAmount.
Sorry, this is kind of a convuluted question. Any advice would be greatly appreciated. Thank you in advance for any direction.

"a loop to find the value of the first date of the month for the next 36 months and write that date to a table with the projected date as fldDate and MonthlyAmount"
Given the starting date of the first month and the number of months, you can use the DateAdd() function to determine the rest of the dates you need to insert. Open your table as a DAO recordset, use its .AddNew method to add a row, assign the values you want in the fields, and call .Update to store the row.
Sounded like your challenge was for the date piece, so I sketched that out and left the MonthlyAmount piece for you to complete.
Public Sub LoadMonthStartDates(ByVal pFirstDate As Date, _
ByVal pMonths As Long)
Const cstrTable = "YourTableNameHere"
Dim db As DAO.database
Dim rs As DAO.Recordset
Dim i As Long
Set db = CurrentDb
Set rs = db.OpenRecordset(cstrTable, dbOpenTable, dbAppendOnly)
For i = 1 To pMonths
rs.AddNew
rs!fldDate = DateAdd("m", i - 1, pFirstDate)
'rs!MonthlyAmount = <your calculated amount>
rs.Update
Next
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub

Related

Copy record in Excel up to specific date

I'm trying to copy a unique row (Row with PUMP for example), "x" amount of times. Eventually, the interval should be based on the column frequency (Frequentie). A record should be copied until the mentioned date, for example, when 01-01-2023 has been reached. This firstly should be completed for the first row and when that has been accomplished it needs to be done for the second row. This to create a dataset which can be used in Power BI. Can anybody help me with this problem?
In the image attached is an example given of the current data set.
Many thanks in advance!
It is always a good idea to show what have you tried so far, because the way the question is asked it looks like you are asking for the whole solution, without any input from your site. And this is a bit against the rules of SO. However, this is something to get you started:
Read the dates from the excel table. Like firstDate = Range("L2")
Locate the max and the min dates. They will be firstDate and lastDate.
Read about loops and copying of Excel range.
Try to do a loop between firstDate and lastDate and copy range valeus in the loop.
This is some loop example, writing dates between two dates on a new row:
Public Sub TestMe()
Dim firstDate As Date
Dim lastDate As Date
Dim cnt As Long
firstDate = DateSerial(2010, 12, 1)
lastDate = DateSerial(2011, 12, 1)
For cnt = 1 To lastDate - firstDate
Cells(cnt, 1) = CDate(firstDate + cnt)
Next cnt
End Sub

Loop that changes a date, copy & pastes resulting data, then continues these processes

I have a workbook with 2 sheets. Sheet 1 has a column with a series of dates that are the same day in a sequence of years (for example 5/1/1986, 5/1/1987, 5/1/1988, ... 5/1/2017).
When the first date at the top of the column is changed (for example from 5/1/1986 to 5/2/1986), all of the other years below will update to that given day and a series of data will be produced in two separate columns through a calculation that is depends on the day as a variable.
The 2nd worksheet has two tables where I need the data resulting from each day between 5/1 and 8/28 for 1986 - 2017 to be pasted from the 1st worksheet after the date is changed (5/1 to 5/2 to 5/3) and so on. The tricky part is the copy and paste needs to happen each time before the date is changed in order to record all of the subsequent data.
I know that I will likely need to use a DateAdd function to loop the addition of one day to the starting date at the top of the column, and I know that I will probably need to utilize a Do Until function to have the sequence stopped when the final date of 8/28 is input. I have been tweaking around the code but I cannot get the formatting correct.
To clarify I need a loop that will do two processes, add one day to 5/1/1986 (until 8/28) and copy the data that each change of day yields and paste it into a 2nd worksheet, before the date is changed again and new data appears. Thanks.
Below is the code I wrote that successfully sets up the first step of the process, setting the start date to 5/1/1986 and copying and pasting the data into the tables in sheet2.
'StartDate is input as 5/1/1986 and plugged into first cell of data range, DK7'
Dim StartDate As String
StartDate = Range("DO50").Value
Range("DK7").Value = StartDate
'EndDate is input as 8/28/1986 and used as reference to end sequence of Copy & Pasting'
Dim EndDate As String
EndDate = Range("DO51").Value
'Corn Z-Score results for StartDate are copied and pasted into Sheet2'
Range("DS7:DS38").Copy
Worksheets(2).Range("D4:D36").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Soy Z-Score results for StartDate are copied and pasted into Sheet2'
Range("DT7:DT38").Copy
Worksheets(2).Range("D41:D73").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

VB.net how to subtract date with another date and get a whole number of days

i use MS Access for my database.
I have 2 columns holding 2 different dates
example
column 1 = 8/7/2016
column 2 = 8/11/2016
i want to subtract 8/11/2016 to 8/7/2016 and get a whole number.
in this example i should be getting 4. What i intend to do is to use the result to compute for something.
Date(Time)s can directly be substracted resulting in a TimeSpan object which has Days or TotalDays properties
Dim result = (#11/08/2016# - #07/08/2016#).Days
Have a look at DateDiff
Days = DateDiff("d", #07/08/2016#, #11/08/2016#)
or in an access sql query:
DateDiff('d', [DateCol1], [DateCol2])

How to calculate time between dates on a report viewer expression

I have a program that uses report viewers to show various types of data to the user, and I need one of them to (basically) calculate the days between two dates WITHOUT weekends (i.e: from Friday to Monday, should show 2 and not 4).
So far, I can calculate it but with the weekends.
If anyone knows how, I'll appreciate it.
Code sample:(the formula i use on one of my tables as a expression)
CInt(Avg(DateDiff(dateinterval.DayOfYear,
Fields!DataEntregue.Value,
Fields!DataPrevista.Value,
FirstDayofWeek.Monday)))
From this question, I have converted Alec Pojidaev's (not accepted) answer to VB. I also returned an integer instead of double:
Public Shared Function GetBusinessDays(startD As DateTime, endD As DateTime) As Integer
Dim calcBusinessDays As Double = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7
If CInt(endD.DayOfWeek) = 6 Then
calcBusinessDays -= 1
End If
If CInt(startD.DayOfWeek) = 0 Then
calcBusinessDays -= 1
End If
Return CInt(calcBusinessDays)
End Function
Usage:
GetBusinessDays(date1, date2)
BTW, in the future, what you were looking for was how to calculate number of business days between two dates in [enter language here]. A quick web search for this would have brought you to a number of answers.

Mean value of data with varying time step

I have a number of dataseries with timestep varying between a few seconds and maybe 120 minutes represented in decimal hours after January 1st. For example one value can be given at time 4692.759994 and the next at 4692.769281. The values can be both positive and negative. I would like to calculate mean values for any given time (usually 1 hour) and wonder of somebody have made anything similar. The code will probable in VBA or VB.net.
Thanks
If you use vb.net you might find this useful:
Dim dtbase As New DateTime(Now.Year, 1, 1)
Dim t1 As DateTime = dtbase.AddHours(4692.759994)
Dim t2 As DateTime = dtbase.AddHours(4692.769281)
Dim Interval As TimeSpan = t2 - t1 'the result is 00:00:33.4340000