Extract years between two dates

koolwaters

Active Member
Joined
May 16, 2007
Messages
403
Hi,

I need to extract the years between two dates.

For example:

Start Date 2010-09-07
End Date 2014-08-30

The number of years between the Start Date and End Date are four.

As part of my query result, I need to see only the years between these two dates. So I would see,

2011 2012 2013 2014

If I have
Start Date 2010-09-07
End Date 2013-08-30

I need to see 2011 2012 2013

I hope that someone can assist.

Thanks in advance.
 
Replaced the format portion in the BuiltTableWithQuery sub with the one you just posted but the numbers are still not formatted.

Code:
Public Sub BuiltTableWithQuery(ByVal sQueryToLinkName As String, _
                               ByVal sTblNameForeCast As String, _
                               ByVal sLinkFieldOne As String, _
                               ByVal sLinkFieldTwo As String, _
                               ByVal sTempQueryName As String, _
                               ByVal blnSumAmounts As Boolean, _
                               Optional ByVal sSumThisFlds As String = "")
 
'Routine that creates all entities
'1 create the forecast table
'2 create array with fieldnames
'3 create the querydef
Dim sAmtYFlds As String     'String to hold the dynamic fieldnames for comparison
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim iFldCnt As Integer
Dim sFields() As String
Dim sSQL As String
Dim sSelFlds As String
Dim sGroupFlds As String
'Create the forecast table
CreateTempTable sTblNameForeCast
Set dbs = CurrentDb
'Get the fieldnames from the QueryToLink, ignore the link fields
Set qdf = dbs.QueryDefs(sQueryToLinkName)
iFldCnt = 0
For Each fld In qdf.Fields
 
        'Test if fld = sum field
        If blnSumAmounts And InStr(1, sSumThisFlds, fld.Name, vbTextCompare) <> 0 Then 'It's a sum field
            ReDim Preserve sFields(iFldCnt)
            sFields(iFldCnt) = "Sum([" & fld.Name & "]) as [Total " & fld.Name & "]"
            iFldCnt = iFldCnt + 1
        ElseIf fld.Name = sLinkFieldOne Or fld.Name = sLinkFieldTwo Then
            'Ignore field
        Else
            ReDim Preserve sFields(iFldCnt)
            sFields(iFldCnt) = "[" & fld.Name & "]"
            If blnSumAmounts Then sGroupFlds = sGroupFlds & ", " & "[" & fld.Name & "]"
 
            iFldCnt = iFldCnt + 1
        End If
Next fld
Set tdf = dbs.TableDefs(sTblNameForeCast)
'load all amtFlds from function
sAmtYFlds = sAmountYearFields
'test if current fieldname is in the sAmtYFlds string
'extract year amount fieldnames from tempforecast into array
'This is to eliminate all fields other than the dynamic fields
For Each fld In tdf.Fields
        If InStr(1, sAmtYFlds, fld.Name, vbTextCompare) > 0 Then
        ReDim Preserve sFields(iFldCnt)
            If blnSumAmounts Then
                sFields(iFldCnt) = "Sum([" & fld.Name & "]) as [Total " & fld.Name & "]"
                iFldCnt = iFldCnt + 1
            Else
                sFields(iFldCnt) = "[" & fld.Name & "]"
                iFldCnt = iFldCnt + 1
            End If
        End If
Next fld
sSelFlds = Join(sFields, ", ")
sSQL = SQL_JoinTwoTbls(sQueryToLinkName, sTblNameForeCast, sLinkFieldOne, sLinkFieldTwo, sSelFlds, Mid(sGroupFlds, 3))
'test if the query already exists, if so delete
    For Each qdf In dbs.QueryDefs
        If qdf.Name = sTempQueryName Then dbs.QueryDefs.Delete qdf.Name
    Next qdf
 
Set qdf = dbs.CreateQueryDef(sTempQueryName, sSQL)
'formatting, hard coded for only the amount approved field

Dim prp As DAO.Property
For Each fld In qdf.Fields
    If InStr(1, fld.Name, "AmountApproved", vbTextCompare) <> 0 Then
        Set prp = fld.CreateProperty("Format", dbText, "$ * #,##0.00")
                fld.Properties.Append prp
    ElseIf InStr(1, sAmtYFlds, Mid(fld.Name, 7), vbTextCompare) <> 0 Then
        Set prp = fld.CreateProperty("Format", dbText, "#,##0.00")
                fld.Properties.Append prp
    End If
Next fld

End Sub
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I need your assistance with one other thing.

I am now trying to create a report using these queries but because the column headings are dynamic it is proving a challenge. Did some searches but the scenarios are for reports where you know the column headings.

As my columns will be the years and these are based on the start and end dates of applications, I am not sure how to create this report.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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