Enhancing Reporting Flexibility with SQL and VBA

advertisement
Enhancing Reporting Flexibility with
SQL and VBA
A brief introduction to some of the ways we’ve
used VBA and SQL to improve the reporting
functionality, of both the core application and
Excelerator in South Devon.
Adam Davies
Senior Systems Development Manager
Introduction
• Qualified accountant by ‘trade’
• School of ‘Google’
• All written in 5.5.3 SQL Server with a central
client configuration & report engine 7.9
• 50 ‘core’ users, 250 web report users
• Users like it simple & quick
• Users are not Excel ‘super-users’
• Strict audit requirements
Excelerator
• Controlling access via Agresso
AGRESSO ROLE(S)
AGRESSO USER
WINDOWS USER
EXCEL ACCESS
• Creating a dynamic crosstab
• Creating selectable auto loading templates
Before We Start
• Referencing Excelerator VBA Project
• Access to run
SQL queries in Excel
Returning the windows username
• Network scripting
Dim objNet As Object
Set objNet = CreateObject("WScript.NetWork")
Network username is: objNet.UserName
• Application username (unreliable)
Application username is: Application.UserName
• Windows Environment
Environ username is: Environ$("USERNAME")
Reference http://www.vbaexpress.com/kb/getarticle.php?kb_id=768
Controlling Access
• Combining VBA, SQL and Environ$ Variables
• Link the Agresso user to the Windows user in
some way
– Single Sign-on
– Usernames the same
– User defined table
• Generate a user_list in excel that can map to
the users window log on
Controlling Access
• Using the same username:
• Link Windows user to Agresso user_id
Agresso set up
Requirement in Excel:
SELECT user_id
FROM aaguserdetail
WHERE
client=‘XX'
AND role_id=‘XXXXXXXX'
Controlling Access
• Using the single sign on domain:
• Link windows user to domain_user to Agresso
user_id
Agresso set up on security tab of user set up
Requirement in Excel:
Controlling Access
• Using a user table:
• Map windows user to Agresso User_id
Agresso set up:
Requirement in Excel:
Controlling Access
EXAMPLE 1
Creating a Crosstab
• Needs to be simple!
• Example all cost centres under an area
– May want to re-run the report for a different area
– Area needs to be user selectable
– Crosstab needs to be seamless to the end user
• Relations need to be defined
– If using wild card relations, a tree is better
because wild cards don’t populate one row for
each item in aglrelvalue. (dep. on set up)
Relation Explanation
• For example three cost centres linked to an
area (N7)
• Aglrelvalue with a standard relation:
• Three lines, one for each costc
Relation Explanation
• Aglrelvalue with a wild-card relation:
• One line for the ‘range’
• Could return ‘Between’ 210 and 250
– But could return invalid codes
Creating a Crosstab
EXAMPLE 2
Clever Templates
• Keep all reports in one area
– Easy to maintain
– Easy for user to select
• Auto load templates
– Need to use agrxlloadall
• Confuses the system if more than one template
• Have to be careful with non-excelerator tabs
Notes about templates
• The ‘list tab’
– Can’t generate using code
– But can copy & paste using VBA
– Or use multiple lists
• The template to load has to be the last tab
– Even if the other tabs are ‘veryhidden’
Clever Templates
EXAMPLE 3
End of Excelerator
Questions?
Adam.davies@nhs.net
Reporting in Agresso
• Creating a custom table
• Adding it into the data model
• Populating it with data
• Updating other browsers
Reporting in Agresso
• Creating a custom table… Why?
– Faster
• Data held at the highest level
– Populate relational values rather than join in browser
• Calculations created on load rather than in browser
each time
– Totally Flexible design
– Use as a data ‘snapshot’ but without data
warehouse browser limitations
• Can’t ‘auto run’
• Doesn’t include periods
Table Creation
• Design your table and create it in SQL
Relations on costc
Latest period
Standard value columns
Calculated columns
Adding the Table
• Add it into the Data Model in Agresso
• Add the table
• Add the columns
Joining the Table
• The table will not be in the data model
• Set up any joins you would like for example:
– Agldimvalue to allow data control & descriptions
Reporting in Agresso
•
•
•
•
Populate it!
Monthly script after DW generation
Ag16 or Stored Procedure
Use an IF EXISTS on
information_schema.tables for prior period
warehouses
• Update browser periods:
– update arabrtgeninfo set period =
Reporting in Agresso
•
Script Breakdown:
1. Remove data for period
–
DELETE FROM region_summary WHERE period = ´$period´
2. Create temp table
–
CREATE TABLE region_summary_$period….
3. Load current period into temp table
–
INSERT INTO region_summary_$period … SELECT … FROM aglaggadp_$period …
4. Load prior period into temp table
–
IF EXISTS (select table_name from information_schema.tables where table_name =
´region_summary_$priorperiod´ ) …BEGIN INSERT INTO region_summary_$period …
SELECT … FROM aglaggadp_$priorperiod … END ELSE BEGIN … END
Reporting in Agresso
5. Combine the data into reporting table
–
INSERT INTO region_summary …
–
Note the CASE to handle a 0 divisor
6. Drop the temp table
–
DROP TABLE region_summary_$period
Download