1 (edited by AD1408 2017-01-15 01:49:32)

Topic: Reports

How do I get data from multiple db tables on report designer. It didn't display company db table data on report designer preview. Please see "SaleTransHist"  form Report button in the sample project attached.


edit:
Tried Report (SQL) option following the tutorial on Reports forum thread "How to create report"  but it produces an error "near FROM syntax error"

SELECT
Company.coLogo,
Company.coName,
Company.coAddress,
Company.coPhone,
FROM Company;

SELECT
SaleInv.invDate,
SaleInv.invCode,
SaleInv.subTotal,
SaleInv.taxTotal,
SaleInv.grossTotal,
SaleInv.discount,
SaleInv.delivery,
SaleInv.InvTotal,
FROM SaleInv;

SELECT
Customer.name,
Customer.surname,
FROM Customer;

SELECT
SaleProduct.name,
FROM SaleProduct;

SELECT
SaleInvItem.Qty,
SaleInvItem.unitPrice,
SaleInvItem.netPrice,
SaleInvItem.taxAmt,
SaleInvItem.totalAmt,
FROM SaleInvItem;

Example project file on same thread may be out of date as it says it's for older MVD and requires some changes.


Also, I used script from a project in these forums to display an image on tGrid cells but couldn't get it working. Please see form1 company tab.

Post's attachments

Attachment icon ReportTest1.zip 37.46 kb, 587 downloads since 2017-01-14 

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

Hello Adam,


In sql, the last element in your select (just before the FROM) does not need the comma. Remove the last comma before the FROM it it should work smile


Cheers


Math

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Reports

Hi Math,


Thanks a lot for the info... The above mentioned error cause gone.


With Report SQL on "frmSaleInv" form button I get only first block DB table (Company) of the script on report on report designer while I need all of them. Additionally, report designer preview displays coLogo image field as %PNG instead of logo image itself.


With Report on "SaleTransHist" form button I get all except Company db fields values.


Please see updated attached sample project file

Post's attachments

Attachment icon ReportTest2.zip 54.98 kb, 653 downloads since 2017-01-15 

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

Hello Adam,


You can not add more than one query for a SQL Report, which means if you "chain" them (more than one SELECT... FROM...) only the first one will be taken into account. This is why you don't see all your fields in the report designer.


But you tables are linked together with the id you have specified so a JOIN command can solve this.


The SQL Query I setup in your button looks like :

SELECT
    SaleInv.invDate,
    SaleInv.invCode,
    SaleInv.subTotal,
    SaleInv.taxTotal,
    SaleInv.grossTotal,
    SaleInv.discount,
    SaleInv.delivery,
    SaleInv.InvTotal,
    Customer.name,
    Customer.surname,
    Customer.street,
    Customer.city,
    SaleProduct.name,
    SaleInvItem.Qty,
    SaleInvItem.unitPrice,
    SaleInvItem.netPrice,
    SaleInvItem.taxAmt,
    SaleInvItem.totalAmt
FROM
    SaleInv
INNER JOIN Customer ON Customer.id = SaleInv.id_Customer,
 SaleProduct
INNER JOIN SaleInvItem ON SaleInvItem.id_SaleInv = SaleInv.id
AND SaleProduct.id = SaleInvItem.id_SaleProduct

All the fields I want are listed at the beginning of the query after the SELECT command, and then, in the FROM section, tables are listed with their links.


If you correct you SQL Query button with this, you'll see all the listed fields available for your report.


One precision though : Company table and SaleInv table are linked together twice on :
SaleInv.id = Company.id_SaleInv
and
Company.id = SaleInv.id_company


Why do you link those tables twice ? Once should be enough I think.


Furthermore, the SaleInv.id_company is NULL everywhere AND same goes for Company.id_SaleInv, which means that if you include them in the query, you won't get any results on the total query because all conditions have to be met, and this one excludes all the others.


Now, before modifying you database structure, what do you mean by Company ? Is it a provider for products you sell, in which case I'd probably link this table with the SaleProduct table, or is it a customer company, in which case I'd link the Company table with the Customer table.


Concerning  "SaleTransHist" report, you don't see company fields because the link from and to this table are NULL : the link exists, it is referenced in both SaleInv and Company tables, but has no value on either sides.


Hope this helps a bit, because that's a great piece of software you are building there, very nice.


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Reports

Hi Math,


Thanks a lot for the generous amount of info....


I have deleted the link from Company and tried the following on ReportSQL but I still couldn't get it working tho

SELECT
Company.coLogo,
Company.coName,
Company.coAddress,
Company.coPhone,
SaleInv.invDate,
SaleInv.invCode,
SaleInv.subTotal,
SaleInv.taxTotal,
SaleInv.grossTotal,
SaleInv.discount,
SaleInv.delivery,
SaleInv.InvTotal,
Customer.name,
Customer.surname,
SaleProduct.name,
SaleInvItem.Qty,
SaleInvItem.unitPrice,
SaleInvItem.netPrice,
SaleInvItem.taxAmt,
SaleInvItem.totalAmt

FROM
Company
INNER JOIN Company ON Company.id = SaleInv.id_Company,

SaleInv
INNER JOIN Customer ON Customer.id = SaleInv.id_Customer,

SaleProduct
INNER JOIN SaleInvItem ON SaleInvItem.id_SaleInv = SaleInv.id
AND SaleProduct.id = SaleInvItem.id_SaleProduct

Concerning  "SaleTransHist" report, you don't see company fields because the link from and to this table are NULL : the link exists, it is referenced in both SaleInv and Company tables, but has no value on either sides.

Not clear on this one.


Now, before modifying you database structure, what do you mean by Company ?

It's user business-company, where they keep some company info such as tax and company registrations numbers etc beside co logo name address etc to use in reports and prints. i.e. if an invoice printed it'd have user's company logo, address, etc beside what's on inv form. It's not for recording any financial activity. It's mainly getting user company details on reports.


Project is a sample project, you can edit, change as you see fit to make it work.

Adam
God... please help me become the person my dog thinks I am.

6 (edited by mathmathou 2017-01-16 04:39:46)

Re: Reports

Hello again Adam,


First of all :

Concerning  "SaleTransHist" report, you don't see company fields because the link from and to this table are NULL : the link exists, it is referenced in both SaleInv and Company tables, but has no value on either sides.

Not clear on this one.

Here is a "graphical" explanation of what I meant :

The relationships between SaleInv and Company :
http://i.imgur.com/oItQIGV.jpg


As you can see each table is linked to the other one, but, if you look into the tables :
http://i.imgur.com/4JIXsO9.jpg
id_company is linked but empty (NULL) and
http://i.imgur.com/tCEij0L.jpg
it is the same for id_SaleInv on the Company table : linked but empty (NULL)


What is the consequence ?


Look at the previous query I posted, there is at least one "AND", meaning all the conditions must be fulfilled. As a consequence, you don't return anything because there is nothing to return.


WORKAROUND :
To solve that problem, and removed the links between SaleInv and Company, and linked customers with Company (assuming ONE Company can be linked with MANY customers) - that's my example, but you'll build your own with the same idea.


Here is how it looks now :
http://i.imgur.com/WwG8epl.jpg


And the sample data I put into customers to link them with company (again, just my example) :
http://i.imgur.com/SdQcOpI.jpg


Now you can query the whole table system, get your data and put them into the report with :

SELECT
    Company.coLogo,
    Company.coName,
    SaleInv.invDate,
    SaleInv.invCode,
    SaleInv.subTotal,
    SaleInv.taxTotal,
    SaleInv.grossTotal,
    SaleInv.discount,
    SaleInv.delivery,
    SaleInv.InvTotal,
    Customer.name,
    Customer.surname,
    Customer.street,
    Customer.city,
    SaleProduct.name,
    SaleInvItem.Qty,
    SaleInvItem.unitPrice,
    SaleInvItem.netPrice,
    SaleInvItem.taxAmt,
    SaleInvItem.totalAmt
FROM
    SaleInv
INNER JOIN Customer ON Customer.id = SaleInv.id_Customer,
 SaleProduct
INNER JOIN SaleInvItem ON SaleInvItem.id_SaleInv = SaleInv.id
    AND SaleProduct.id = SaleInvItem.id_SaleProduct
INNER JOIN Company ON Customer.id_Company = Company.id

All the data you wanted are in a Dataset, now, how do I format the report to get the logo image ?

Simple steps :
You logo is in the dataset
http://i.imgur.com/tN2ROwG.jpg


but it's an image blobed into the database so add a master data band like this
http://i.imgur.com/tfRjJIM.jpg


set company name field  like this
http://i.imgur.com/r6FnLDk.jpg


now add an image (not the coLogo) as shown below  and click OK (green check mark)
http://i.imgur.com/5MSmJcL.jpg:


Finally, with the image selected in the report set :
1- DataSet = Report
2- DataField = coLogo

like this
http://i.imgur.com/DuC3QhV.jpg
If you don't set the DataSet first, you won't be able to select the DataField


Now let's preview your report :
http://i.imgur.com/wvmmHYk.jpg



Yes, it's ugly like that, but it works. Just arrange the size of your fields, the filters you want, add the other ones, organize them as you wish and you have your report.


Example of very small page work :
http://i.imgur.com/6LVZWaN.jpg


And end result on the report :
http://i.imgur.com/9JAcm5V.jpg


Project attached


Hope it helps


Cheers


Mathias

Post's attachments

Attachment icon ReportTest.zip 386.91 kb, 580 downloads since 2017-01-16 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Reports

Hello Mathmathou,

What kind of program you use for screen captures? It's a great help for show problems and solutions to this forum.

Thanks, Carlo

Re: Reports

Great stuff Mathias.... Thank you so much......................


Now, I need to get down to learn using report designer.


At this point only thing is remaining on this topic is displaying image in a tGrid cell. Please see form1 company tab.
Perhaps Dmitry could offer solution for it.

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

AD1408 wrote:

At this point only thing is remaining on this topic is displaying image in a tGrid cell. Please see form1 company tab.
Perhaps Dmitry could offer solution for it.


Unfortunately the example, which you use don't work when you store images in database.

Dmitry.

Re: Reports

Unfortunately the example, which you use don't work when you store images in database.


Images are using "LinkFile" type.
What's causing the issue of not working on my example?
Perhaps there is a workaround...

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

AD1408 wrote:

Images are using "LinkFile" type.
What's causing the issue of not working on my example?
Perhaps there is a workaround...


In your project you using method "StoreFile", please check settings of component "dbiCoLogo" on form "frmCo"

Dmitry.

Re: Reports

In your project you using method "StoreFile", please check settings of component "dbiCoLogo" on form "frmCo"

Hi Dmitry,


Please see sample project "ReportTest2.zip" on post #3 where LinkFile type used.

http://myvisualdatabase.com/forum/misc. … download=1

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

AD1408
I checked, all fine, I see  images in cells. It's don't work for you?

Dmitry.

Re: Reports

No, I get file path instead of image itself


https://s28.postimg.org/e5xx1cjbh/test.png

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

I see mistake.


1. You should create event "Form1_tgCompany_OnChange" on tab "Events"
You have code of event, but have not created it.


2. Also fix code:

procedure Form1_tgCompany_OnChange (Sender: string);
var
    G: TGraphic;
    i,c: integer;
    sExt: string;
    sFile: string;
begin
    Form1.tgCompany.Columns[1].Visible := False; // hide first column, where we can get image file name
    Form1.tgCompany.Columns.InsertGraphicColumn(1); // insert Graphic Column, index of column is 1

    c := Form1.tgCompany.RowCount-1;
    for i := 0 to c do
    begin
        sFile := Form1.tgCompany.Cells[2,i];
Dmitry.

Re: Reports

Thanks a lot Dmitry...............


iT works fine now..
I was also making mistake of setting sExt cell number to 2 like below:
Form1.tgCompany.Cell[2, i].ObjectReference := G;

Adam
God... please help me become the person my dog thinks I am.

17 (edited by AD1408 2017-01-16 23:06:51)

Re: Reports

The following refers to Report designer.


I delve into report designer... I must say it's one of those awkward app to learn... It seems to me that they took approach of showing left ear with right hand.


I needed to use two data levels - master/details.
In FR help, it states "In the report designer, enable the data sources in the “Report>Data...” dialogue." However, I cannot find it. If MVD report designer is a different version and doesn't have this feature, is there a work around?


I like to insert inv item details into master band and inv totals into details band that details band doesn't repeat after every master band data lines.
https://s30.postimg.org/kspybx22p/zzz_Test5.png


Edit:
-----------------------
One solution I found is that just using header for names labels, master data band for inv items data and inserting inv totals below master data band on the page. Visually (preview) it seems ok. However, I'm not sure if it's right way to do tho.

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

Hi Math,


Lets assume user operates more than one business and wants to assign each sale to one of his company.
For this I linked SaleInv db table to Company db table. It seems to be working OK with Report but I couldn't get the SQL script right to make it work with with ReportSQL.


Script I've tried and didn't work:

INNER JOIN Company ON SaleInv.id_Company =Company.id
WHERE SaleInv.invCode = '{edSaleInvCode}'

Please see reference images below:
DB Schema
http://prnt.sc/dwq5os


SaleInv Form
http://prnt.sc/dwq7nb

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

INNER JOIN Company ON SaleInv.id_Company =Company.id
WHERE SaleInv.invCode = '{edSaleInvCode}'

Adam,


I suspect after defining the relationship you probably did not populate the SaleInv.Id_Company field. Without values in there the query will not return anything. However, I don't think I would recommend setting up that relationship unless you have an underlyng reason for doing so. It could cause a conflict along the way. You could essentially assign an invoice to a company and simultaneously to a customer which could already be assigned to another company. Each company should have there own set of customers and invoices. I would stick to the schema set up by Mathias.

Re: Reports

Hi EHW,


Thanks a lot for the info...


I was thinking in practical terms. I agree that multiple companies cannot share same invoice/s but shouldn't they be able to share same customer/s?


SELECT
    Company.coLogo,
    Company.coName,
 Company.coAddress,
 Company.coPhone,
    SaleInv.invDate,
    SaleInv.invCode,
    SaleInv.subTotal,
    SaleInv.taxTotal,
    SaleInv.grossTotal,
    SaleInv.discount,
    SaleInv.delivery,
    SaleInv.InvTotal,
    Customer.name,
    Customer.surname,
    Customer.street,
    Customer.city,
    SaleProduct.name,
    SaleInvItem.Qty,
    SaleInvItem.unitPrice,
    SaleInvItem.netPrice,
    SaleInvItem.taxAmt,
    SaleInvItem.totalAmt
FROM
    SaleInv
INNER JOIN Customer ON Customer.id = SaleInv.id_Customer,
 SaleProduct
INNER JOIN SaleInvItem ON SaleInvItem.id_SaleInv = SaleInv.id
    AND SaleProduct.id = SaleInvItem.id_SaleProduct
INNER JOIN Company ON Customer.id_Company = Company.id
WHERE SaleInv.invCode = '{edSaleInvCode}'
INNER JOIN Company ON SaleInv.id_Company =Company.id
WHERE SaleInv.invCode = '{edSaleInvCode}'

The above produces syntax error.
If I add semicolon after customer lines like below it doesn't produce error but it doesn't display correct company on report preview. I'm thinking script lines I added are wrong.

FROM
    SaleInv
INNER JOIN Customer ON Customer.id = SaleInv.id_Customer,
 SaleProduct
INNER JOIN SaleInvItem ON SaleInvItem.id_SaleInv = SaleInv.id
    AND SaleProduct.id = SaleInvItem.id_SaleProduct
INNER JOIN Company ON Customer.id_Company = Company.id
WHERE SaleInv.invCode = '{edSaleInvCode}';  
INNER JOIN Company ON SaleInv.id_Company =Company.id
WHERE SaleInv.invCode = '{edSaleInvCode}'

Sample project attached:

Post's attachments

Attachment icon ReportTest 3.zip 65.32 kb, 550 downloads since 2017-01-18 

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

Post #17

One solution I found is that just using header for names labels, master data band for inv items data and inserting inv totals below master data band on the page. Visually (preview) it seems ok. However, I'm not sure if it's right way to do tho.


I thought the above was the solution for for Report not ReportSQL
However, it's not. When there is more than few entries inv totals put on report designer page without band doesn't move downwards. I tried footer but  report data is not displayed. Please see the reference images below:
http://prnt.sc/dx7zj1
http://prnt.sc/dx81km


Also page numbering issue on image 2


Report file refered on above images "Test_MultiCo2.fr3" is saved in Reports with attached sample project below:

Post's attachments

Attachment icon ReportTest 3b.zip 62.09 kb, 530 downloads since 2017-01-18 

Adam
God... please help me become the person my dog thinks I am.

Re: Reports

Adam,


Since multiple companies can share customers, I removed the relationship between customers and companies. I fixed your ReportSql and put in the report bands for the invoice. I did not format anything in the report bands. Just wanted to show you the placement of the data in the bands.

Post's attachments

Attachment icon ReportTest 3b Fixed.zip 397.86 kb, 535 downloads since 2017-01-18 

Re: Reports

ehwagner wrote:

Since multiple companies can share customers, I removed the relationship between customers and companies. I fixed your ReportSql and put in the report bands for the invoice. I did not format anything in the report bands. Just wanted to show you the placement of the data in the bands.


Thank you soooooooooooooooo much EHW.............................
Great stuff...


There are two things I noticed.
1. Couldn't get coLogo image displayed
2. On group footer band you have calculated net, tax and total from master data band.
On my actual project inv totals are calculated on inv form as there are discount and delivery cost calcs for the whole invoice.  What I like to do is to copy already calculated values of
sub total
tax total
gross total
less discount
plus delivery
inv total
fields to group footer or suitable band.

Adam
God... please help me become the person my dog thinks I am.

24 (edited by ehwagner 2017-01-18 23:56:54)

Re: Reports

Adam,


You have hit on a nuance, qwirk, snafu or whatever you want to call it with Fast Report and MVD. For some reason, you can place calculated totals in the footer, but you cannot place report fields in there. But all is not lost. You have to go around the woods to get to grandmother's house in this case. In a nutshell, you have to place the report fields that you want in the Group Footer into the Group Header. In your case you don't want to see these fields in the Group Header, so you have to hide them (white text font). Then you can define label fields (memo in Fast Report terms) in the Group Footer. Then using Pascal script in Fast Report you can populate and format the Group Footer fields with the hidden fields in the Group Header.


I have screenshots, but not sure how to place them here in the post. I'm winging it. Hopefully they show.


Well they did not show. Maybe somebody can enlighten me in how to place images in the post.

Post's attachments

Attachment icon ReportTest 3b Fixed 2.zip 398.46 kb, 646 downloads since 2017-01-19 

Re: Reports

Once again thanks a million EHW.........


We are almost there I think...
Only first company logo image on the list is shown, in this case penguin corp.
When I try another company report their logo image is not shown.


On image display on a post, I couldn't find a way to display an image in a post, only a link, on this forum. Instead I use PostImg. They allow hot linking and provides all kind of hot links including for forums: https://postimage.io


I also use Lightshot at http://prnt.sc
It's a small free screen capture, upload and hosting utility. It also provides online edit of your captured image. I found it very handy for screen capture and sharing. They don't allow hot linking tho.


I'd be very interested to see your screen cap for how you did place those inv totals in group footer band.

Adam
God... please help me become the person my dog thinks I am.