Results 1 to 8 of 8

Thread: Year and Month and data search

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Year and Month and data search

    Hello Everyone! Im new in VB-Access development and I have a project in school about database programming. The program should compute and display the total number of years and month based on the system date, it should be displayed in a textbox. Here's an example:

    Let say the system date = Feb 24, 2007 and the entry date = April 24, 2005

    the display output should be:
    1 year and 10 months


    Also, about the search button, when the a string has been input, it will display all the records with the same string... Please Help! Thank you very much to all!

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Year and Month and data search

    Let say the system date = Feb 24, 2007 and the entry date = April 24, 2005

    the display output should be:
    1 year and 10 months
    To find a difference between two dates, use the datediff fuction
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Year and Month and data search

    Hello and Thank you! Yes, I already used the datediff(), however, when I use it to return the year, it rounds up the year when it reached the 6th month, I need something that will break down the year and month if it is not exactly within the whole year. Please help, Thank you!

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Year and Month and data search

    You can use a series of DateDiff calls, but it gets a little complex, especially since DateDiff doesn't report years and months in a way that you might want.

    Let me see if i can write the entire code for you...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Year and Month and data search

    Thank you so much! Im still trying to figure this out... Please do!

  6. #6
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Year and Month and data search

    I also added days

    VB Code:
    1. Dim dStartDate As Date
    2. Dim dEndDate As Date
    3. Dim iYears As Integer
    4. Dim iMonths As Integer
    5. Dim iDays As Integer
    6.  
    7. dStartDate = "24/04/2005"
    8. dEndDate = "25/02/2007"
    9.  
    10. 'first do the years
    11. iYears = DateDiff("yyyy", dStartDate, dEndDate) - 1
    12.  
    13.     'then the months
    14.     If iYears > 0 Then
    15.     iMonths = DateDiff("m", DateAdd("yyyy", iYears, dStartDate), dEndDate)
    16.    
    17.     Else
    18.    
    19.     iMonths = DateDiff("m", dStartDate, dEndDate)
    20.    
    21.     End If
    22.    
    23.    
    24.     'and now the days
    25.     If iMonths > 0 Then
    26.     dStartDate = DateAdd("yyyy", iYears, dStartDate)
    27.     dStartDate = DateAdd("m", iMonths, dStartDate)
    28.    
    29.     iDays = DateDiff("d", dStartDate, dEndDate)
    30.    
    31.     Else
    32.    
    33.     iDays = DateDiff("d", dStartDate, dEndDate)
    34.    
    35.     End If

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Year and Month and data search

    Thank you very much! Im gonna try it right away as soon as i got home! really thank you so much! Would it be ok to ask an assistance from u from time to time, since Im a beginner in VB?

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Year and Month and data search

    @ Spooshe : That's a wonderful code given by Lintz

    Here is another one... which will give you the result in Years, Months and Days....

    VB Code:
    1. Private Sub Command1_Click()
    2.     'For example
    3.     MsgBox Spooshe(#4/24/2005#, #2/24/2007#, True, True, True)
    4. End Sub
    5.  
    6. Public Function Spooshe(StartDate As Date, EndDate As Date, Optional WithMonths As Boolean = False, _
    7.     Optional WithDays As Boolean = False, Optional DisplayWithWords As Boolean = False) As Variant
    8.    
    9.     On Error GoTo Spooshe_ErrorHandler
    10.    
    11.     Dim iYears As Integer
    12.     Dim iMonths As Integer
    13.     Dim iDays As Integer
    14.     Dim dTempDate As Date
    15.    
    16.     'Check that the dates are valid
    17.     If Not (IsDate(StartDate)) Or Not (IsDate(EndDate)) Then
    18.         DoCmd.Beep
    19.         MsgBox "Invalid date.", vbOKOnly + vbInformation, "Invalid date"
    20.         GoTo Spooshe_ErrorHandler
    21.     End If
    22.    
    23.     'Check that StartDate < EndDate
    24.     If StartDate > EndDate Then
    25.         DoCmd.Beep
    26.         MsgBox "EndDate must be greater than StartDate.", _
    27.             vbOKOnly + vbInformation, "Invalid date position"
    28.         GoTo Spooshe_ErrorHandler
    29.     End If
    30.    
    31.     iYears = DateDiff("yyyy", StartDate, EndDate) - _
    32.         IIf(DateAdd("yyyy", DateDiff("yyyy", StartDate, EndDate), StartDate) > EndDate, 1, 0)
    33.     dTempDate = DateAdd("yyyy", iYears, StartDate)
    34.    
    35.     If WithMonths Then
    36.         iMonths = DateDiff("m", dTempDate, EndDate) - _
    37.             IIf(DateAdd("m", iMonths, DateAdd("yyyy", iYears, StartDate)) > EndDate, 1, 0)
    38.         dTempDate = DateAdd("m", iMonths, dTempDate)
    39.     End If
    40.    
    41.     If WithDays Then
    42.         iDays = EndDate - dTempDate
    43.     End If
    44.    
    45.     'Format the output
    46.     If DisplayWithWords Then
    47.         'Display the output in words
    48.         Spooshe = IIf(iYears > 0, iYears & " year" & IIf(iYears <> 1, "s ", " "), "")
    49.         Spooshe = Spooshe & IIf(WithMonths, iMonths & " month" & IIf(iMonths <> 1, "s ", " "), "")
    50.         Spooshe = Trim(Spooshe & IIf(WithDays, iDays & " day" & IIf(iDays <> 1, "s", ""), ""))
    51.     Else
    52.         'Display the output in the format yy.mm.dd
    53.         Spooshe = Trim(iYears & IIf(WithMonths, "." & Format(iMonths, "00"), "") _
    54.             & IIf(WithDays, "." & Format(iDays, "00"), ""))
    55.     End If
    56.    
    57. Exit_Spooshe:
    58.     Exit Function
    59.    
    60. Spooshe_ErrorHandler:
    61.     Spooshe = Null
    62.     Resume Exit_Spooshe
    63. End Function

    Hope this helps....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width