Solved

Get Year And Months From Months

Posted on 2014-01-18
2
269 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

706 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

19 Experts available now in Live!

Get 1:1 Help Now