Results 1 to 12 of 12
  1. #1
    lsmcal1984 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    5

    Field to display last Friday's date by default

    Hi,

    I have a field on my form that needs to display last Friday's date by default.

    This is on an unbound form which is used to build a query displaying hours between two dates.



    Many thanks!

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Put this in the Form_Load event (or in your autoexec macro).
    Code:
    Private Sub Form_Load()
        TempVars.Add "DefaultFriday", DateAdd("d", IIf(Weekday(Date, [vbFriday]) > 0, -Weekday(Date, [vbFriday]), -7), Date)
    End Sub
    Put this in the field's default value property
    Code:
    =[TempVars].[Item]("DefaultFriday")

  3. #3
    lsmcal1984 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    5
    Thank you for your quick response
    Right now it is displaying last Thursday's date rather that last Friday... any ideas? I tried amending the -7 to -6 but it didn't change it.
    Otherwise it works great!
    Leo

  4. #4
    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
    Add +1 to the end of either line of code:

    Code:
    =[TempVars].[Item]("DefaultFriday") +1


    or

    Code:
    TempVars.Add "DefaultFriday", DateAdd("d", IIf(Weekday(Date, [vbFriday]) > 0, -Weekday(Date, [vbFriday]), -7), Date) + 1

    Personally, I'd use the second example. Don't know if it matters, here, but also note that if you run this code on a Friday it will give you the current date

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

    All posts/responses based on Access 2003/2007

  5. #5
    lsmcal1984 is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Oct 2013
    Posts
    5
    Excellent, works great. Thanks!

  6. #6
    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!

    Welcome to the Forum!

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

    All posts/responses based on Access 2003/2007

  7. #7
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    That was what the -7 was for, but I was assuming Weekday returned 0 thru 6 rather than 1 thru 7. This is the correct (and simpler) code -
    Code:
    TempVars.Add "DefaultFriday", DateAdd("d", -Weekday(Date, [vbSaturday]), Date)

  8. #8
    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
    Elegant! And that resolves the problem (from Post #4) of generating the date on a Friday and getting the current Friday!

    It would be nice if the Access Gnomes either made everything Zero-based or nothing Zero-based!

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

    All posts/responses based on Access 2003/2007

  9. #9
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Naw, the Access gnomes had nothing to do with me misreading http://www.techonthenet.com/access/f...te/weekday.php.

    I don't even have the excuse that it was aircode... I tested the code I posted, but didn't bother to check to see that the result was a Friday!

  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
    Even Allen Browne makes mistakes, though not many!

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

    All posts/responses based on Access 2003/2007

  11. #11
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Hey, if I can't laugh at myself, I'll miss nearly half the available jokes...

  12. #12
    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
    And why should you miss all the fun?
    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. Replies: 9
    Last Post: 04-26-2013, 12:12 PM
  2. Replies: 5
    Last Post: 11-18-2012, 01:27 PM
  3. Select the First friday after a date
    By lucasjkr in forum Queries
    Replies: 1
    Last Post: 11-13-2012, 03:07 PM
  4. Getting the last and the next Friday
    By bonecone in forum Programming
    Replies: 9
    Last Post: 05-10-2012, 02:02 PM
  5. Replies: 2
    Last Post: 04-27-2010, 01:25 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