Solved

Get Year And Months From Months

Posted on 2014-01-18
2
272 Views
Last Modified: 2014-01-18
I neee a rotuine that will be passed the number of months and needs to return a string var of the number of years and a string var of the number of months.

Thought this was going to be easy but rounding is getting in the way.  On a numbe of months like 23 I am getting 2 year -1 months.

Here's what I have so far, which doesn't work.

Public Sub convertNumMonthsToYearsandMonths(passedNumberOfMonths As Long, _
                                            returnYearStr As String, _
                                            returnMonthsStr As String)
'round

Dim wkYears As Double
Dim wkYearsInt As Long
Dim wkMonths As Long
'
If passedNumberOfMonths < 12 Then
    wkYears = 0
Else
    wkYears = Round(passedNumberOfMonths / 12, 2)
End If
wkYearsInt = CInt(wkYears)
'
wkMonths = passedNumberOfMonths - (wkYearsInt * 12)
'
returnYearStr = Trim(Str(wkYearsInt))
returnMonthsStr = Trim(Str(wkMonths))
'
End Sub

Open in new window


Hwo can I get this to work?
0
Comment
Question by:mlcktmguy
2 Comments
 
LVL 29

Accepted Solution

by:
IrogSinta earned 200 total points
ID: 39791573
Seems like you can do this easily without a function:

iYears = Int(totalMonths / 12)
iMonths = ((totalMonths / 12) - iYears) * 12

Open in new window


Ron
0
 
LVL 21

Assisted Solution

by:Boyd (HiTechCoach) Trimmell, Microsoft Access MVP
Boyd (HiTechCoach) Trimmell, Microsoft Access MVP earned 50 total points
ID: 39791609
And there is maintain your data types:

Dim passedNumberOfMonths As Long
Dim returnYearStr As String
Dim returnMonthsStr As String

passedNumberOfMonths  = 23

returnYearStr =  Str(passedNumberOfMonths  \ 12)
returnMonthsStr  = Str(passedNumberOfMonths   - ((passedNumberOfMonths   \12)*12))

Open in new window


Proof of Concept:

? 23 \ 12
 1

? 23 - ((23 \12)*12)
 11


or

Public Sub convertNumMonthsToYearsandMonths(passedNumberOfMonths As Long, _
                                           ByRef  returnYearStr As As Variant, _
                                           ByRef returnMonthsStr AsAs Variant)

returnYearStr = Trim(Str(passedNumberOfMonths  \ 12))
returnMonthsStr = Trim(Str(passedNumberOfMonths   - ((passedNumberOfMonths   \12)*12)))

End Sub

Open in new window

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

30 Experts available now in Live!

Get 1:1 Help Now