Results 1 to 10 of 10
  1. #1
    xduy is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    6

    Changing a default date value based on the time of day

    I have a form that has a field for the date which currently defaults to =Date() (today), but from midnight to 8AM I want it to default to =Date()-1 (yesterday). Is there an easy way to do this?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    In the form,set the text box at form open.
    txtbox= date() & " 8:00 am"

    but for yesterday, use date math functions to subtract 1 day...

    txtBox = DateAdd("d",-1,Date()) & " 8:00 am"

  3. #3
    xduy is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    6
    In the form I have right now I have the default value for the date text box set as "=Date()" which always brings up the current day. Where do I put what you wrote?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The VBA code to set the value is:

    Code:
    If Format(Now(), "hh") < "08" And Format(Now(), "AMPM") = "AM" Then
        Me!Text8 = Date - 1
    Else
        Me!Text8 = Date
    End If
    The question is where and when to make this happen. Is it bound to a table? In which case it would need to happen on the BeforeUpdate event.

  5. #5
    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
    The IIF() Function was made for this:

    IIf(Time()<#08:01:00 AM#,Date()-1,Date())

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I couldn't get this to work in the Default Value of the textbox, where does it go?

  7. #7
    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
    Worked (in v2007) for me, but it has to be just as given

    IIf(Time()<#08:01:00 AM#,Date()-1,Date())

    especially the bits in red.

    I'm glad you posted, though! I reset my PC's clock, to test this, and forget to reset the time! Realized this when I went to recheck my code, which does still work.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  8. #8
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    I was going from design view to form view and it wasn't working. As soon as I closed and saved the form and reopened it then it worked! That's why I did the VBA thing.

  9. #9
    xduy is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jan 2017
    Posts
    6
    Thank you so much everyone. I got it working now.

  10. #10
    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
    Glad we could help!

    @aytee111: The Access Gnomes like to mess around like that sometimes!

    I think they just get bored!

    Linq ;0)>
    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. Changing Date time in Between Date() AND Date()+...
    By Thompyt in forum Programming
    Replies: 1
    Last Post: 02-22-2016, 12:36 PM
  2. Replies: 2
    Last Post: 08-03-2014, 09:36 AM
  3. Replies: 2
    Last Post: 07-22-2013, 12:13 AM
  4. Changing date to date with time
    By mulchgirl in forum Reports
    Replies: 3
    Last Post: 08-28-2012, 02:36 PM
  5. Changing tables based on field date
    By dssrun in forum Programming
    Replies: 6
    Last Post: 07-10-2011, 10:17 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