Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get Year And Months From Months

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

840 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