Table of Contents
Lesson 3 - Generate a report from "Employees" database
In Lesson 3, you will learn how to work with a local Valentina Reports project, create and generate reports.
The report will print all records added to the database from the LiveCode application using local Valentina Project.
To follow this lesson, you should have the LiveCode project created in Lesson 2.
Step 1: Valentina Project Preparation
Valentina Reports are stored in the Valentina Project file.
Valentina Studio Pro is a tool used to create projects and design reports.
- Click Create button and select Datasource… menu item.
- Click Open Database→Open local database and select Database1.vdb on disk.
- Select Database1 in the tree and click OK:
- Click Create button and select Query… menu item.
- Input the following query:
- Test query and click OK:
- Click Create button and select Report… menu item.
Use the Report Wizard to define desired report layout:
Layout is created:
- Click Preview and check the output:
The resulting project you can find here.
Step 2: LiveCode Project Preparation for the Work with Valentina Reports
Copy the files you created in Lesson2 to a new location, start LiveCode, then load the project.
Valentina Reports Init
In the current stack open the context menu and select Edit Script. Modify the code of the openCard handler as follows:
on openCard if the gClient of this stack is true then get valentina_InitClient() else get valentina_Init( 10 * 1024 * 1024 ) end if get valentina_InitReports( "", "", "" ) DatabaseCreate end openCard
Valentina Reports Shutdown
In the current stack open the context menu and select Edit Script. Modify the code of the closeCard handler:
on closeCard DatabaseClose get Valentina_ShutDownReports get Valentina_ShutDown end closeCard
Step 3: Valentina Project Opening
Create a new handler ProjectOpen:
on ProjectOpen get the effective filename of this stack set the itemDel to slash put the (item 1 to -2 of it) & slash & "Project1.vsp" into projPath put VProject_Constructor( projPath ) into mProject get VProject_Open( mProject ) get ShowError() end ProjectOpen
Once the work with project is done it can be closed and freed in the ProjectClose handler:
on ProjectClose get VProject_Close( mProject ) put VProject_Destructor( mProject ) into mProject end ProjectClose
Add calls of ProjectOpen and ProjectClose to openCard and closeCard handlers.
Step 4: Valentina Report Generation
Add function which prints report to PDF file at the temporary location and presents it to the user:
function DoPrintPDF put VProject_MakeNewReport( mProject, "report1", mDatabase, "SELECT * FROM Person" ) into rpt put specialFolderPath( "temporary" ) & slash & "report.pdf" into tmpPath get VReport_PrintToDisk( rpt, tmpPath, "kToPDF" ) get ShowError() get VReport_Destructor( rpt ) launch document tmpPath end DoPrintPDF