Results 1 to 2 of 2
  1. #1
    waqas is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Aug 2011
    Posts
    117

    Cool To find Month and Days in given date period


    dear experts.

    i want to get month and how many days of each with in given date period.

    like 04-jan-13 to 15-Mar-13.

    result should like below.

    Month Days

    Jan-13 28
    Feb-13 28
    Mar-13 15

    how can i get this information

  2. #2
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You don't say where you want this to end up (in a Textbox, in a Variable, etc) or what you want to use it for, but here's an example using Textboxes named StartDate, EndDate, TargetField (where I'm placing the results) and a Command Button named cmdMonthsAndDays. The results given is a String. You could use another Control/Event to trigger the calculations, depending on your needs, but this is the general idea:
    Code:
    Private Sub cmdMonthsAndDays_Click()
     
      
     Dim Beginning As Date
     Dim Ending As Date
     Dim TotMonths As Integer
     Dim Mmonth As String
     Dim DisplayMonth As Date
     Dim PrintMonth As String
     Dim DaysByMonth As String
     Dim FinalResults As String
     
     If Nz(Me.StartDate, "") = "" Or Nz(Me.EndDate, "") = "" Then
      MsgBox "You must Enter a Start Date and an End Date!"
      Exit Sub
     End If
     
     Beginning = Me.StartDate
     Ending = Me.EndDate
     
     TotMonths = DateDiff("m", Beginning, Ending) + 1
    
     For x = 0 To (TotMonths - 1)
      
      Mmonth = Month(DateAdd("m", x, Beginning))
      
      DisplayMonth = Mmonth & "/" & "1/" & Year(DateAdd("m", x, Beginning))
      
      DaysByMonth = Mmonth & "/" & "1/" & Year(Date) & ": " & Day(DateSerial(Year(DisplayMonth), Month(DisplayMonth) + 1, 0))
      
       
      PrintMonth = Format(DisplayMonth, "mmm-yy")
     
     
     If Month(Beginning) = Month(DisplayMonth) Then
      
       FinalResults = PrintMonth & "  " & (Day(DateSerial(Year(DisplayMonth), Month(DisplayMonth) + 1, 0))) - Day(Beginning) + 1
      
     ElseIf Month(Ending) = Month(DisplayMonth) Then
      
      FinalResults = FinalResults & "   " & PrintMonth & "  " & Day(Ending)
      
     Else
         
      FinalResults = FinalResults & "   " & PrintMonth & "  " & Day(DateSerial(Year(DisplayMonth), Month(DisplayMonth) + 1, 0))
      
      End If
     
     Next x
      
     Me.TargetField = FinalResults
    
     End Sub


    StartDate and EndDate have to be declared as DateTime Datatypes.

    This code was developed/tested on a machine using the American date format of 'mm-dd-yyyy.' Since you are using a non-US date format, you might want read Allen Browne's article on the subject, since it can cause problems in Access:

    http://allenbrowne.com/ser-36.html

    Somebody sharper than myself will probably come along with a hack that will do this in 8 lines, but the above works.

    Linq ;0)>
    Last edited by Missinglinq; 02-12-2013 at 11:06 PM.
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Alert on Existing Date Period
    By waqas in forum Access
    Replies: 5
    Last Post: 02-03-2013, 10:38 PM
  2. Replies: 4
    Last Post: 01-25-2013, 05:20 AM
  3. Replies: 3
    Last Post: 06-19-2012, 10:42 PM
  4. Getting Tax Period from current date
    By crxftw in forum Forms
    Replies: 3
    Last Post: 07-09-2011, 07:12 AM
  5. Total no of days in a month
    By wasim_sono in forum Forms
    Replies: 4
    Last Post: 10-15-2006, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums