PatrickJohnson
Registered User.
- Local time
- Today, 09:14
- Joined
- Jan 26, 2007
- Messages
- 68
Hi! I've written a macro and converted it to VBA which takes an average employee headcount for the 2006 calendar year by taking a sample headcount from the 15th of each month and then averaging the 12 numbers. I have the macro doing this just fine.
-Currently it works something like this (I'll post the exact code below too):
-Macro runs delete query to delete any data in Headcount table
-Macro runs an append query which prompts for a month and year
-Macro sends keystroke of "1" and "{ENTER}" for month, then "2006" and {ENTER} for the year
-Query uses the input data to populate a cdate function which adds "/15/" between the month and year to make a finite date
-Query appends the headcounts for each department to the headcount table for the date specified
-All of this runs again for each month, with only the sendkeys for the month value changed
-A select query averages the numbers for each department in the headcounts table with the avg function in the totals box and displays the average number for each department
This works great... as long as I just want 2006's data. What I would like to do is set this up in VBA so that a user can enter a beginning date and end date and it will pull all the headcounts on the 15th of each month that falls between them, then perform the average query stuff.
This way, if there was a need for an average headcount for a random period of time, the user would just enter a start date and end date into prompts and out comes the result.
Anyone have a good idea as to how to accomplish this?
-Currently it works something like this (I'll post the exact code below too):
-Macro runs delete query to delete any data in Headcount table
-Macro runs an append query which prompts for a month and year
-Macro sends keystroke of "1" and "{ENTER}" for month, then "2006" and {ENTER} for the year
-Query uses the input data to populate a cdate function which adds "/15/" between the month and year to make a finite date
-Query appends the headcounts for each department to the headcount table for the date specified
-All of this runs again for each month, with only the sendkeys for the month value changed
-A select query averages the numbers for each department in the headcounts table with the avg function in the totals box and displays the average number for each department
This works great... as long as I just want 2006's data. What I would like to do is set this up in VBA so that a user can enter a beginning date and end date and it will pull all the headcounts on the 15th of each month that falls between them, then perform the average query stuff.
This way, if there was a need for an average headcount for a random period of time, the user would just enter a start date and end date into prompts and out comes the result.
Anyone have a good idea as to how to accomplish this?
Function Headcount_Macro()
On Error GoTo Headcount_Macro_Err
DoCmd.SetWarnings False
DoCmd.OpenQuery "OHA Headcount Macro Delete", acViewNormal, acEdit
SendKeys "1{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "2{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "3{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "4{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "5{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "6{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "7{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "8{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "9{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "10{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "11{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
SendKeys "12{ENTER}", False
SendKeys "2006{ENTER}", False
DoCmd.OpenQuery "OHA Sample Headcount (Specify Month) Append", acViewNormal, acEdit
DoCmd.OpenQuery "OHA Average Headcount from Headcounts Table", acViewNormal, acEdit
DoCmd.SetWarnings True
Headcount_Macro_Exit:
Exit Function
Headcount_Macro_Err:
MsgBox Error$
Resume Headcount_Macro_Exit
End Function