Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automatically Pull Date into Query Parameter - Access 2010

    I am writing an update query where I need a date parameter. I need to make a change if a date is >= the prior month end. For example, if I did it today, I would want to change all records with the date >= 7/31/12.

    Currently, I have a parameter in the query to prompt me for the date. However, if I make a typo, the calculation will impact the wrong records. Is there a function / expression I can use for the date so it is automatically pulled in and I do not have to babysit my macro to wait for that parameter or put it in a table to be referenced by the query??

    Thanks!

    Nancy

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Nancy,

    You can use this user defined function (UDF) to return the last day of the previous month.
    Code:
    Function dteEOPM() As Date
    
       Dim dteCurDate As Date
       Dim dteEOMPrev As Date
       
       dteCurDate = Date
       dteEOMPrev = DateValue(Format(Month(dteCurDate), "##") & "/1/" & _
                    Format(Year(dteCurDate), "####")) - 1
       
       dteEOPM = dteEOMPrev
       
    End Function
    dteEOPM Query Design.JPG
    dteEOPM Query Result.JPG
    Just use this in your querydef ">=dteEOPM()".
    BTW: are you sure you want >= and not just >?
    Last edited by RetiredGeek; 2012-08-07 at 16:12.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it is >=. We're looking at transaction maturity dates, and the data in our extract is always as of the prior month end. Therefore, a transaction maturing 7/31/12 is still active on 7/31/12. One that matures 7/30/12 would not be.

    Thanks for your help - I'll try it.

    Nancy

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Nancy,

    Or you can enter it directly into the Expression Builder:

    dteEOPM Via Expression Builder.JPG

    Personally, I prefer the UDF as I consider it cleaner. However, some people don't like to use VBA for a variety of reasons, it's all a matter of your personal preference.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    2 Star Lounger
    Join Date
    Oct 2004
    Location
    Minnesota, USA
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, RG

    I agree with using VBA - lots of room to write comments to explain what one is doing.

    Nancy

  6. #6
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts
    You could simply use this in RG's thread:
    Code:
        dteEOPM = DateSerial(Year(Date), Month(Date), 0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •