Results 1 to 7 of 7
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    condition - year not the same then do task unless null

    I'm trying to make a piece of code work



    the idea is that if the years don't match (specific dates don't matter) then it won't let the user enter the date - however I don't want the alert to appear when the user clears the textbox for the bookingdate (if they change the year - it automatically has an action to null the bookingdate field)

    code so far is:

    Code:
    Private Sub BookingDate_AfterUpdate()
    
    
    Dim strYear As String
    Dim strBooking As String
    
    
    strYear = Nz(Year(Me.BookingYear.Column(1)), "no")
    strBooking = Nz(Year(Me.BookingDate.Value), "") ' I get an error if I don't assign nz() to this
    
    
    
    
    If strYear <> strBooking And IsNull(strBooking) Then
    'do no task
    ElseIf strYear <> strBooking And Not IsNull(strBooking) Then
    MsgBox "You have selected a date that is not within the year, please select a correct booking date or change the year"
    Me.BookingDate = Null
    End If
    
    
    End Sub
    so um yeah, how do I get around something like this, the proper way.

  2. #2
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    I think this is what you're looking for... but not quite sure.
    Code:
    Private Sub BookingDate_AfterUpdate()
    
      If Not IsNull(Me.BookingDate) AND Not IsNull(Me.BookingYear.Column(1)) Then
        Dim intYear As Integer
        Dim intBooking As Integer
    
    
        intYear = Year(Me.BookingYear.Column(1))
        intBooking = Year(Me.BookingDate.Value)
    
    
        If intYear <> intBooking Then
        MsgBox "You have selected a date that is not within the year, please select a correct booking date or change the year"
        Me.BookingDate = Null
      End If
    End Sub

    BTW, you had your two variables strBooking and strYear as strings. Both the Year and Nz function return integers by default. You would need to do a Cstr conversion if you truly wanted to make them strings. You may also need to wrap a CDate function around your BookingDate and BookingYear.Column(1) depending on what kind of data is in them.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I just noticed an error in the code by Xipooo - there should be two "End If" lines.
    Code:
    <snip>
        Me.BookingDate = Null
      End If
      End If
    End Sub
    @Xipooo:
    Both the Year and Nz function return integers by default
    I just tested this... Access seems to automatically convert the integer year value to a string. But I will be aware of this possibility in the future. Thanks...

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    testing the code

  5. #5
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by Xipooo View Post
    I think this is what you're looking for... but not quite sure.
    Code:
    Private Sub BookingDate_AfterUpdate()
    
      If Not IsNull(Me.BookingDate) AND Not IsNull(Me.BookingYear.Column(1)) Then
        Dim intYear As Integer
        Dim intBooking As Integer
    
    
        intYear = Year(Me.BookingYear.Column(1))
        intBooking = Year(Me.BookingDate.Value)
    
    
        If intYear <> intBooking Then
        MsgBox "You have selected a date that is not within the year, please select a correct booking date or change the year"
        Me.BookingDate = Null
      End If
    End Sub

    BTW, you had your two variables strBooking and strYear as strings. Both the Year and Nz function return integers by default. You would need to do a Cstr conversion if you truly wanted to make them strings. You may also need to wrap a CDate function around your BookingDate and BookingYear.Column(1) depending on what kind of data is in them.

    thanks

    yeah I was aware of date conversion to string but it never has bothered me - but will keep in mind.

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Works beautifully (and like I imagined it was so simple - I sometimes get scrambled that way)


    thank you so much

  7. #7
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    No problem.

    Yeah I forgot Access sometimes does implicit conversions. Other languages that I'm usually working in aren't that nice to play with. Wires get crossed sometimes.

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

Similar Threads

  1. Replies: 17
    Last Post: 01-26-2014, 06:53 AM
  2. Dsum data between to dates for this year and last year
    By sdel_nevo in forum Programming
    Replies: 1
    Last Post: 06-13-2013, 06:48 AM
  3. Replies: 4
    Last Post: 01-09-2013, 11:16 AM
  4. Financial Year not Calendar Year
    By Kirsti in forum Queries
    Replies: 5
    Last Post: 05-02-2012, 09:11 PM
  5. Replies: 4
    Last Post: 01-10-2012, 06:26 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