Solved

Not receiving correct result from vba code in access 2010

Posted on 2013-12-17
7
555 Views
Last Modified: 2013-12-17
Hi All,

I wrote the below code to calculate an extra day after a term date has been given for an individual.  So if a term date is entered of 12/11/13, I need the result to be 12/12/13.  With this, I've had to modify the code to for months that have 31, 30, and 28 days (we don't count leap year).  I've tried the code out with someone have the termination date of 11/30/13, hoping that it would produce 20131201 but instead it just gives me 20131130.  I'm hoping someone could help me understand why the coding isn't working.  To further understand the code, the ELDENT field is formatted in "CYYMMDD" which is why I add it to 19000000 to get a YYYYMMDD.

Public Function fnMbrTerm(ELDENT As Double, ELDETS As Double, RunEnd As Double)

Dim TermCalc As Double

If ((ELDENT > 0) And (ELDETS = 0) And (ELDENT < RunEnd)) Then
    TermCalc = ELDENT + 19000000
End If

If Right(TermCalc, 4) = 1231 Then
    fnMbrTerm = TermCalc + 8870
    
ElseIf Right(TermCalc, 4) = ("0131" Or "0331" Or "0531" Or "0731" Or "0831" Or "1031") Then
    fnMbrTerm = TermCalc + 70
    
ElseIf Right(TermCalc, 4) = ("0430" Or "0630" Or "0930" Or "1130") Then
    fnMbrTerm = TermCalc + 71
    
ElseIf Right(TermCalc, 4) = ("0228") Then
    fnMbrTerm = TermCalc + 73

Else

fnMbrTerm = TermCalc

End If

If (ELDENT = 0) Then

fnMbrTerm = "00000000"

End If

End Function

Open in new window

0
Comment
Question by:Anthony6890
  • 5
7 Comments
 
LVL 10

Assisted Solution

by:Korbus
Korbus earned 100 total points
ID: 39725599
If you can extract the day, month and year- just create a DateTime object in your function.  Once you do that, adding a day is super easy (no worries about leap years, days in a month, etc...), so is extracting out the day month and year again.  Looks like this page can give details on how to do that:  http://msdn.microsoft.com/en-us/library/aa227564(v=vs.60).aspx
0
 
LVL 1

Author Comment

by:Anthony6890
ID: 39725602
This is great reading.

Thank you for this.  I was also able to figure it out another way.  I realized that when I completed the Right function, that the four positions were not really true because of the leading zero was dropped.  When I scaled back the reference to just the right 3 values it worked like a charm.  I realized that I would never have the same right 3 values a calendar because of the way the months land.

Thank you for this, though.  I will try to incorporate it into my next phase of the project.

-Ant
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 400 total points
ID: 39725618
Give this a try:

Public Function fnMbrTerm(ELDENT As Double) As String

Dim strFormatted As String

If (ELDENT = 0) Then
  fnMbrTerm = "00000000"
Else
   ELDENT = ELDENT + 19000000
   strFormatted = CStr(ELDENT)
   fnMbrTerm = Format$(DateSerial(Mid(strFormatted, 1, 4), Mid(strFormatted, 5, 2), Mid(strFormatted, 7, 2)) + 1, "YYYYMMDD")
End If


End Function


Jim.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 1

Author Comment

by:Anthony6890
ID: 39725623
Jim,

Thanks for the response.  I like how concise your formula is, but I need to enter some other variables to ensure that I don't continue to pickup the term.  For example, by having that ELDETS = 0 ensures that it's the first time I'm picking up the term.  After my run, that field is populated with a value and then it is bypassed on the next run the following month.
0
 
LVL 1

Author Comment

by:Anthony6890
ID: 39725663
The weird thing is I was able to figure it out by some adjustment, but I get a weird result when I need the year to change...look at this line of code:

ElseIf Right(TermCalc, 4) = 1231 Then
   fnMbrTerm = TermCalc + 8870

All of the other logic applies above.  If you actually do the computation for an ELDENT = 1131231 and then add 19000000 you get 20131231 (the TermCalc result from above).  The four right numbers are 1231.  So I'm saying that the function equal the TermCalc result of 20131231 + 8870 which when you actually add it up equal 20140101 which is the correct answer.  Instead, I'm getting 20131301.  Have any clue why this might be happening?
0
 
LVL 1

Author Comment

by:Anthony6890
ID: 39725691
Jim, I was able to get yours to work great.  Thank you all for your help.
0
 
LVL 1

Author Closing Comment

by:Anthony6890
ID: 39725695
The first one was great reading on learning how to do such a task of splitting the task.  The second solution was an actual resolve to the problem.
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

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Familiarize people with the process of utilizing SQL Server functions 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 Ac…

778 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