Brasco

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

8 posts in this topic

#1 ·  Posted

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

@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

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

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

 


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

Share this post


Link to post
Share on other sites

#5 ·  Posted

Thanks a ton for your help.  I'm going to work on this today and i'll report back my results.

Share this post


Link to post
Share on other sites

#6 ·  Posted

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

Share this post


Link to post
Share on other sites

#7 ·  Posted

@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 ;)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#8 ·  Posted

@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.

Share this post


Link to post
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