Jump to content

Entering the first and last day of the previous month into a text box


Recommended Posts

Any ideas on how I would go about pulling the first day of the last month and the last day of the last month into a form?  In a nutshell, I want to automate a monthly report that on the first day of the month, runs the report for the previous month without having to manually update the script each time.  Curious if there is a way to do this?  I haven't been able to find anything online.  Thanks in advance.

Form.jpg

Link to comment
Share on other sites

  • Moderators

@Brasco are you looking for just the date? Or the date and day? This is a simple one for the last day date, day is a bit more. You could easily modify for first day of month:

#include <Date.au3>

Local $sLastMonth = (@MON - 1)
Local $iDays = _DateDaysInMonth( @YEAR, $sLastMonth)
    ConsoleWrite($sLastMonth & "/" & $iDays & "/" & @YEAR & @CRLF)

Edit: Admittedly, you'll need another statement to handle it if you're in January and looking to go back to the previous month and year, but this should at least get you started. Ask if you have any questions.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

The "controlID"  parameters of the ControlSend() functions may be incorrect in this example and need changing.

#include <Date.au3>
#include <Array.au3> ; For display purposes only.

;$aArray = _Date_GetFirst_LastOfPreviousMonth("2017/01/02") ; Test a January date as a parameter for the _Date_GetFirst_LastOfPreviousMonth() function.
; Or
$aArray = _Date_GetFirst_LastOfPreviousMonth(_NowCalcDate()) ; Use _NowCalcDate() as parameter for the current date.
_ArrayDisplay($aArray)

; Note: ClassnameNN property for the "controlID"  parameter of the ControlSend() function can be found by using AutoIt's
; "AU3Info" tool.  This tool is found in "SciTE > Tools" menu, or, "Au3info.exe" from ...\(Install directory of )AutoIt3\Au3info.exe
If WinExists("Select Report Criteria") Then
    WinActivate("Select Report Criteria")
    WinWaitActive("Select Report Criteria")
    ControlSend("Select Report Criteria", "", "Edit1", $aArray[0]) ; Assuming top input box has a ClassnameNN property of "Edit1"
    ControlSend("Select Report Criteria", "", "Edit2", $aArray[1]) ; Assuming the next input box has a ClassnameNN property of "Edit2"
Else
    MsgBox(0, "Error", 'Window with title "Select Report Criteria" does not exist.')
EndIf


; Function ------------ _Date_GetFirst_LastOfPreviousMonth -----------------
; Only parameter   - Date in the format is "YYYY/MM/DD"
; Returns an Array - First element  - Array[0] - Start date of previous month in the format of "MM/DD/YYYY".
;                  - Second element - Array[1] -  Last date of previous month in the format of "MM/DD/YYYY".
Func _Date_GetFirst_LastOfPreviousMonth($iThisMonth)
    Local $sLastMonth = _DateAdd("M", -1, $iThisMonth)
    Local $iDays = _DateDaysInMonth(StringLeft($sLastMonth, 4), StringMid($sLastMonth, 6, 2))
    Local $aRetArray = [StringMid($sLastMonth, 6, 2) & "/01/" & StringLeft($sLastMonth, 4), _
            StringMid($sLastMonth, 6, 2) & "/" & $iDays & "/" & StringLeft($sLastMonth, 4)]
    Return $aRetArray
EndFunc   ;==>_Date_GetFirst_LastOfPreviousMonth

 

Link to comment
Share on other sites

hi @Malkey, I've simplified your function a bit

; Function ------------ _Date_GetFirst_LastOfPreviousMonth -----------------
; Only parameter   - Date in the format is "YYYY/MM/DD"
; Returns an Array - First element  - Array[0] - Start date of previous month in the format of "MM/DD/YYYY".
;                  - Second element - Array[1] -  Last date of previous month in the format of "MM/DD/YYYY".
Func _Date_GetFirst_LastOfPreviousMonth($iThisMonth)
    If Not _DateIsValid($iThisMonth) Then Return SetError(1, 0, "")
    Local $aDate = StringSplit(_DateAdd('M', -1, $iThisMonth), '/', 2)
    Local $sYM = $aDate[0] & '/' & $aDate[1] & '/'
    Local $aRetArray = [$sYM & "01", $sYM & _DateDaysInMonth($aDate[0], $aDate[1])]
    Return $aRetArray
EndFunc   ;==>_Date_GetFirst_LastOfPreviousMonth

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I can see this is doing exactly what it should be but I need a little more help (sorry, scripting is new to me).  This makes it all the way to the point of displaying the array and the data is correct, but for the life of me I can't figure out how to actually transfer that data to the form.  I've confirmed the controlID params as:

ControlSend("Select Report Criteria", "", "[CLASS:Edit; INSTANCE:1]", $aArray[0]) 
ControlSend("Select Report Criteria", "", "[CLASS:Edit; INSTANCE:2]", $aArray[1])

..but I'm not sure why it doesnt seem to be returning them to the screen.  Any other hints of things to look into?  Thanks,

2017-07-24_18-07-57.jpg

Link to comment
Share on other sites

  • Moderators

@Brasco can you please post the full code you're using now, including the controlsends? It should work, but seeing what you're doing will help us help you ;)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

@JLogan3o13 der, I figured it out.  There was a _ArrayDisplay($aArray) comment in the code that i commented out and now it's doing what it should be doing.  Appreciate the followup and next time will paste the full code in - I'm sure that would have been useful the first time I asked!  Thanks again, the support here has been great.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...