Results 1 to 4 of 4
  1. #1
    allochthonous is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2006
    Posts
    12

    "Previous Month" button

    Does anyone here know an easy way to get two fields on an Access form to populate with the first day and last day of the previous month by the click of a button?

    Here is the best I could come up with, but this seems so sloppy:



    (of course, this is on the OnClick event on the button)

    Dim TheDate As Date
    Dim DaysBack As Integer


    TheDate = Date 'uses TheDate so that I can enter any date for testing


    DaysBack = (DatePart("d", (Date)))

    [End] = (TheDate - DaysBack)

    [Start] = (TheDate + 1) - (DaysBack + (DatePart("d", [End])))

  2. #2
    Join Date
    Dec 2005
    Location
    Wilmington, DE - USA
    Posts
    275
    Why not just use textboxes with controlsource of:

    =DateSerial(Year(Now()), Month(Now()) - 1, 1)

    for first day of previous month and

    =DateSerial(Year(Now()), Month(Now()), 0)

    for the last day of the previous month?

    And yes, those expressions work even if going back a month crosses a year boundary

  3. #3
    allochthonous is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2006
    Posts
    12
    Huh?

    I guess I need to look up DateSerial.

    I want it tied to a button because I offer several quick date ranges: Today, Yesterday, Month to Date, Year to Date, etc.

    But if that DateSerial thing works, i can tie it to the click event.

    I just don't understand what that code is saying there.

    PK

  4. #4
    Join Date
    Dec 2005
    Location
    Wilmington, DE - USA
    Posts
    275
    PK,

    Quote Originally Posted by allochthonous
    I want it tied to a button because I offer several quick date ranges: Today, Yesterday, Month to Date, Year to Date, etc.

    But if that DateSerial thing works, i can tie it to the click event.
    Yes, you can use the Click event.

    Quote Originally Posted by allochthonous
    I just don't understand what that code is saying there.
    DateSerial(Year, Month, Day) returns a date based on the arguments supplied. By definition, if you put in zero for the Day argument, what you get is the last day of the previous month. It is an extremely handy function.

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

Similar Threads

  1. Access 2003 Date() Function returns "#Name?"
    By smartel@soprema.ca in forum Programming
    Replies: 5
    Last Post: 01-18-2012, 05:52 AM
  2. Replies: 0
    Last Post: 09-25-2008, 12:19 PM
  3. Export to .rtf of "104-" converts to "-655&qu
    By Sherri726 in forum Import/Export Data
    Replies: 0
    Last Post: 12-19-2006, 03:16 PM
  4. Replies: 2
    Last Post: 08-31-2006, 12:19 PM
  5. "Count" and "Countif" in Reports
    By JMantei in forum Reports
    Replies: 1
    Last Post: 06-20-2006, 02:20 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