Results 1 to 14 of 14
  1. #1
    krc9127 is offline Advanced Beginner
    Windows 11 Access 2007
    Join Date
    Jul 2023
    Posts
    32

    limit date Selection


    Is there a way on a form that has a date selection field (with date picker show) to limit how back a date be chosen or a date typed in? I asking to prevent someone from intentionally or unintentionally from adding data to a fiscal year that has been closed.

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    Use the control before update event to prevent user entering to early (or late) date

  3. #3
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    You can get the fiscal year of a date with something like

    Code:
    Function GetFiscalYear(Dt As Variant, Optional StartMonth As Integer = 9) As Variant
    
    If Not IsDate(Dt) Then Exit Function
    
        If Month(Dt) >= StartMonth Then
            GetFiscalYear = (Year(Dt) + 1)
        Else
            GetFiscalYear = Year(Dt)
        End If
    
    End Function
    
    Result:
    ?GetFiscalYear(#9/7/2022#)
     2023 
    ?GetFiscalYear(#8/30/2022#)
     2022
    Gives me another item to add to my tinkering list. I have a custom date picker class and it would be interesting to try it with a limited date range.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  4. #4
    krc9127 is offline Advanced Beginner
    Windows 11 Access 2007
    Join Date
    Jul 2023
    Posts
    32
    Quote Originally Posted by moke123 View Post
    You can get the fiscal year of a date with something like

    Code:
    Function GetFiscalYear(Dt As Variant, Optional StartMonth As Integer = 9) As Variant
    
    If Not IsDate(Dt) Then Exit Function
    
        If Month(Dt) >= StartMonth Then
            GetFiscalYear = (Year(Dt) + 1)
        Else
            GetFiscalYear = Year(Dt)
        End If
    
    End Function
    
    Result:
    ?GetFiscalYear(#9/7/2022#)
     2023 
    ?GetFiscalYear(#8/30/2022#)
     2022
    Gives me another item to add to my tinkering list. I have a custom date picker class and it would be interesting to try it with a limited date range.

    So I just change the 9 to a 7 if our fiscal starts in July and they can't pick anything before the current fiscal year?

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Yes 7 would be july 1.

    if GetFiscalYear(#some date#) = GetFiscalYear(Date()) then . . . It would be in the current fiscal year.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    krc9127 is offline Advanced Beginner
    Windows 11 Access 2007
    Join Date
    Jul 2023
    Posts
    32
    OK I tried to put this in the code for the DATE_BeforeUpdate event, but it errors out. Does it go between the Private Sub, End Sub portions?

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by krc9127 View Post
    OK I tried to put this in the code for the DATE_BeforeUpdate event, but it errors out. Does it go between the Private Sub, End Sub portions?
    And you cannot say what that error is?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    DATE_BeforeUpdate
    You can't use Date for a field or object name as it is a reserved word.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    krc9127 is offline Advanced Beginner
    Windows 11 Access 2007
    Join Date
    Jul 2023
    Posts
    32
    Quote Originally Posted by moke123 View Post
    You can't use Date for a field or object name as it is a reserved word.
    Even if I change the name, it still gives me the same thing. The field date is obviously not the reason.

  10. #10
    krc9127 is offline Advanced Beginner
    Windows 11 Access 2007
    Join Date
    Jul 2023
    Posts
    32
    ok I put the sub routine to call for the function but now it gives me an error. This is my subroutine that calls the function.

    Dim FiscalYear As Variant Dim EntryDate As Variant
    Dim CurrentYear As Integer


    ' Assuming you have a control named "RideDate" on the form
    RideDATE = Me!RideDATE


    ' Call the GetFiscalYear function
    FiscalYear = GetFiscalYear(EntryDate)


    ' Get the current fiscal year
    CurrentYear = GetFiscalYear(DATE)


    ' Check if the fiscal year is the prior year
    If FiscalYear < CurrentYear Then
    ' Cancel the update
    Cancel = True

    ' Display an error message
    MsgBox "The prior fiscal year is locked. You cannot make changes to records from that period.", vbExclamation, "Update Error"
    Else
    ' Proceed with the update (optional additional logic can be added here)
    MsgBox "The fiscal year is: " & FiscalYear
    End If


  11. #11
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    u did not copy the procedure and u did not highlight which line/word get the error nor the error # explanation

  12. #12
    krc9127 is offline Advanced Beginner
    Windows 11 Access 2007
    Join Date
    Jul 2023
    Posts
    32
    Quote Originally Posted by ano View Post
    u did not copy the procedure and u did not highlight which line/word get the error nor the error # explanation
    I found the error. I changed the actual field name but I didn't change the name of the DIM.

    Thanks for all your input.

  13. #13
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Oops, wrote this last night and apparently forgot post it.


    simplified:
    Code:
        If GetFiscalYear(EntryDate) <> GetFiscalYear(Date) Then
            Cancel = True
            MsgBox "The prior fiscal year is locked. You cannot make changes to records from that period.", vbExclamation, "Update Error"
        Else
            MsgBox "The fiscal year is: " & GetFiscalYear(EntryDate)
        End If
    Hard to tell where your error is. you don't show enough code or error.
    Not sure what this is but I dont see a variable RideDate dimensioned anywhere. That would error.
    Code:
    ' Assuming you have a control named "RideDate" on the form
    RideDATE = Me!RideDATE
    Where's EntryDate coming from?
    Code:
    FiscalYear = GetFiscalYear(EntryDate)
    Even if I change the name, it still gives me the same thing. The field date is obviously not the reason.
    Using reserved words can cause random, sporadic errors so don't dismiss them.
    http://allenbrowne.com/AppIssueBadWord.html

    Also note that testing for FY being < than current FY wont prevent entry of a date > than the current FY. Your test should be <> current FY.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  14. #14
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    with incomplete info i assume the spelling and definitions are correct else we never can help

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

Similar Threads

  1. Limit date input based on current date
    By hari101213 in forum Programming
    Replies: 5
    Last Post: 04-02-2020, 09:09 AM
  2. Replies: 4
    Last Post: 02-14-2017, 08:10 AM
  3. Replies: 4
    Last Post: 01-23-2014, 04:34 PM
  4. Limit to date
    By Daryl2106 in forum Access
    Replies: 5
    Last Post: 11-11-2013, 08:42 AM
  5. Limit user to one selection
    By burrina in forum Forms
    Replies: 12
    Last Post: 01-12-2013, 03:22 AM

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