Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102

    Criteria in a query (select the previous month)

    Hello everybody,




    I have a query that is needed for a report. In this query I have a field called "Date". In this field I have written the following criterion:
    >=[Forms]![foPayrollMonth]![StartDate]-30 And <=[Forms]![foPayrollMonth]![EndDate]-30
    With this criterion I try to call up the data from the previous month, but this is too imprecise. Is there another way to "filter" from the first day of the previous month to the last day of the previous month?

    Thank you for the help!

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Use the DateSerial() function with the DateAdd() function
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    This may give you a starting point.

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: lastMonth
    ' Purpose: Given a Date, identify the previous month Start day to End Day
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 01-May-23
    ' ----------------------------------------------------------------
    Sub lastMonth()
        Dim MyDate(3) As Date
        MyDate(0) = #2/25/2023#
        MyDate(1) = #3/17/2020#
        MyDate(2) = #12/25/2021#
        MyDate(3) = #1/1/2000#
        Dim i As Integer
        For i = LBound(MyDate) To UBound(MyDate)
           Debug.Print DateSerial(Year(MyDate(i)), Month(MyDate(i)) - 1, 1) & "  thru " & DateSerial(Year(MyDate(i)), Month(MyDate(i)), 0)
        Next i
    End Sub
    Result:

    01-Jan-23 thru 31-Jan-23
    01-Feb-20 thru 29-Feb-20
    01-Nov-21 thru 30-Nov-21
    01-Dec-99 thru 31-Dec-99

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Hmm, I did not know DateSerial was clever enough to know when to amend the Year?
    That was why I suggested the DateAdd()

    I knew it it did it for the Day, never ocurred to me it would do it for the Month as well.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102
    Quote Originally Posted by orange View Post
    This may give you a starting point.

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: lastMonth
    ' Purpose: Given a Date, identify the previous month Start day to End Day
    ' Procedure Kind: Sub
    ' Procedure Access: Public
    ' Author: Jack
    ' Date: 01-May-23
    ' ----------------------------------------------------------------
    Sub lastMonth()
        Dim MyDate(3) As Date
        MyDate(0) = #2/25/2023#
        MyDate(1) = #3/17/2020#
        MyDate(2) = #12/25/2021#
        MyDate(3) = #1/1/2000#
        Dim i As Integer
        For i = LBound(MyDate) To UBound(MyDate)
           Debug.Print DateSerial(Year(MyDate(i)), Month(MyDate(i)) - 1, 1) & "  thru " & DateSerial(Year(MyDate(i)), Month(MyDate(i)), 0)
        Next i
    End Sub
    Result:

    01-Jan-23 thru 31-Jan-23
    01-Feb-20 thru 29-Feb-20
    01-Nov-21 thru 30-Nov-21
    01-Dec-99 thru 31-Dec-99
    How would u use that in the query tho?
    I mean in this field("kriterien"):
    Click image for larger version. 

Name:	Screenshot 2023-05-01 174335.png 
Views:	9 
Size:	6.1 KB 
ID:	50181

    I tried this:
    DateSerial(Year(MyDate(i)), Month(MyDate(i)) - 1, 1) & " thru " & DateSerial(Year(MyDate(i)), Month(MyDate(i)), 0)

    But i got a Syntax Error.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    You do not copy what was posted.
    That was to show you how to get the date range.

    So just use the dates on the form in each use of DateSerial.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102
    Quote Originally Posted by Welshgasman View Post
    You do not copy what was posted.
    That was to show you how to get the date range.

    So just use the dates on the form in each use of DateSerial.
    I did it like this in my form now:
    Code:
        Dim Anfang As Date    
        Dim Ende As Date
        Dim i As Integer
        For i = LBound([Forms]![foLohnabrechnungMonat]![Startdatum]) To UBound([Forms]![foLohnabrechnungMonat]![Enddatum])
        Date = DateSerial(Year([Forms]![foLohnabrechnungMonat]![Startdatum](i)), Month([Forms]![foLohnabrechnungMonat]![Startdatum](i)) - 1, 1)
        Ende = DateSerial(Year([Forms]![foLohnabrechnungMonat]![Startdatum](i)), Month([Forms]![foLohnabrechnungMonat]![Startdatum](i)), 0)
        Next i
    But i get this error:
    Error during compilation:


    Expected: data field

    What did i do wrong?

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Give us an example of the data value(s) on your form and the names of the controls involved.

    Given a dateX,
    this is the first of last/previous month: DateSerial(Year(datex), Month(dateX) - 1, 1)
    this is the last of last/previous month: DateSerial(Year(dateX), Month(dateX), 0)

  9. #9
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102
    Quote Originally Posted by orange View Post
    Give us an example of the data value(s) on your form and the names of the controls involved.

    Given a dateX,
    this is the first of last/previous month: DateSerial(Year(datex), Month(dateX) - 1, 1)
    this is the last of last/previous month: DateSerial(Year(dateX), Month(dateX), 0)
    An example would be:

    [Forms]![foLohnabrechnungMonat]![Startdatum] = 01.05.2023

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Akchayan View Post
    I did it like this in my form now:
    Code:
        Dim Anfang As Date    
        Dim Ende As Date
        Dim i As Integer
        For i = LBound([Forms]![foLohnabrechnungMonat]![Startdatum]) To UBound([Forms]![foLohnabrechnungMonat]![Enddatum])
        Date = DateSerial(Year([Forms]![foLohnabrechnungMonat]![Startdatum](i)), Month([Forms]![foLohnabrechnungMonat]![Startdatum](i)) - 1, 1)
        Ende = DateSerial(Year([Forms]![foLohnabrechnungMonat]![Startdatum](i)), Month([Forms]![foLohnabrechnungMonat]![Startdatum](i)), 0)
        Next i
    But i get this error:
    Error during compilation:


    Expected: data field

    What did i do wrong?
    Well for a start, you do not indent your code which makes it harder to read.
    You are using Date which is a reserved word and not Anfang for what I presume is your starting date?

    How many dates are we talking about here as that will only populate the last set of dates?

    Do you understand any of this logic? or are you just writing whatever comes to mind? I ask as we might have to take a step or two backwards if that is the case.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102
    Quote Originally Posted by Welshgasman View Post
    Well for a start, you do not indent your code which makes it harder to read.
    You are using Date which is a reserved word and not Anfang for what I presume is your starting date?

    How many dates are we talking about here as that will only populate the last set of dates?

    Do you understand any of this logic? or are you just writing whatever comes to mind? I ask as we might have to take a step or two backwards if that is the case.
    I don't rly understand the logic

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Akchayan View Post
    I don't rly understand the logic
    Ok,
    Let's step back and clarify a second.
    You have two dates on your form, yes?, a start date and end date? That is how this sort of process works.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102
    Yes i do.
    The startdate is: "startdatum"
    The enddate is: "enddatum"

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Akchayan View Post
    Yes i do.
    The startdate is: "startdatum"
    The enddate is: "enddatum"
    OK, but if we are working on last month, we only need today's date for example, and from Orange's code we can work out the last month dates 1st and end of month.

    So what purpose do these two dates serve except for that was what you were doing.?

    No need to enter any date really as we can use the system date, yes?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    Akchayan is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Aug 2022
    Posts
    102
    OHHH yeah thats correct.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. query readings from current and previous month
    By JRCharlie in forum Access
    Replies: 14
    Last Post: 04-15-2018, 01:41 PM
  2. Replies: 2
    Last Post: 02-12-2015, 05:14 AM
  3. Replies: 1
    Last Post: 09-06-2014, 01:08 PM
  4. Replies: 2
    Last Post: 04-15-2014, 08:43 AM
  5. Select Month from previous selected year
    By k0enf0rNL in forum Access
    Replies: 1
    Last Post: 01-15-2014, 12:14 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