?
Solved

Get Year And Months From Months

Posted on 2014-01-18
2
Medium Priority
?
279 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
[X]
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
2 Comments
 
LVL 29

Accepted Solution

by:
IrogSinta earned 800 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 200 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…
Suggested Courses

765 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