Results 1 to 6 of 6
  1. #1
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76

    Ensure dates from two fields(DateFrom and Dateto) are correct and calculatetotaldays


    Hi,
    I have two textboxes which allow users to type in two dates for a certain period.(DateFrom and DateTo) How do i make sure the users do not put a date in DateTo that is earlier that DateFrom ? also how do i calculate the difference between the two dates?

  2. #2
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You can put in a validation rule in for the DateTo control on your form like this:

    [DateTo]>[DateFrom]

    You will have to make sure your users entered the DateFrom first.

    To determine the difference between two dates, you will want to use the datediff() function. This expression will find the number of days between the two date values

    =DateDiff("d",[datefrom],[dateto])

  3. #3
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Quote Originally Posted by jzwp11 View Post
    You can put in a validation rule in for the DateTo control on your form like this:

    [DateTo]>[DateFrom]

    You will have to make sure your users entered the DateFrom first.

    To determine the difference between two dates, you will want to use the datediff() function. This expression will find the number of days between the two date values

    =DateDiff("d",[datefrom],[dateto])
    Ok, so here is my code for the validation :

    Private Sub AvailablePeriodEnd_BeforeUpdate(Cancel As Integer)
    If [AvailablePeriodStart] > [AvailablePeriodEnd] Then
    MsgBox ("You have entered a date wrongly")
    Me!AvailablePeriodEnd = ""
    End If
    End Sub

    However the code in red gives me an error everytime. How do i replace this code with a code that resets the value in the dateto field? I just want the field to be blank if the user enters wrong date.

  4. #4
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You cannot reset the value while you are in the control from which the code was executed. You can, however, prevent the record from being entered if you put the code in the before update event of the form and since the code is not executing from the control, you can reset it

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If [availableperiodstart] > [availableperiodend] Then
      MsgBox ("You have entered a date wrongly")
      Cancel = True
       Me.availableperiodend = Null
    End If
    End Sub

  5. #5
    xwnoob is offline Advanced Beginner
    Windows Vista Access 2000
    Join Date
    Nov 2011
    Posts
    76
    Quote Originally Posted by jzwp11 View Post
    You cannot reset the value while you are in the control from which the code was executed. You can, however, prevent the record from being entered if you put the code in the before update event of the form and since the code is not executing from the control, you can reset it

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If [availableperiodstart] > [availableperiodend] Then
      MsgBox ("You have entered a date wrongly")
      Cancel = True
       Me.availableperiodend = Null
    End If
    End Sub
    Ok but can i use this code in conjunction with the code inside the control?


    Also, how do i implement your code inside the form_beforeupdate event with another code that i have:


    Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Ensure that users do not accidentally save records
    If Not Me.NewRecord Then
    If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
    Cancel = True
    Me.Undo
    End If
    End If
    End Sub

    How do i combine the codes

  6. #6
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You will want to check the date before you check if they want to update the record because otherwise once you update the record it will be too late to check the date.

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

Similar Threads

  1. Ensure the form I'm on is last record
    By zippy483 in forum Programming
    Replies: 1
    Last Post: 11-23-2011, 05:08 AM
  2. Dates & Days Fields
    By djclntn in forum Database Design
    Replies: 5
    Last Post: 10-22-2011, 06:22 PM
  3. Replies: 9
    Last Post: 06-26-2011, 09:14 PM
  4. Correct code for challenging dates?
    By thekruser in forum Queries
    Replies: 6
    Last Post: 11-08-2010, 03:38 PM
  5. Mixed dates - show only complete dates?
    By weeblesue in forum Queries
    Replies: 3
    Last Post: 10-27-2010, 02:15 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