Selection Formula where value is in a DataTable - sql

My system has a database table for orders (Sales Lines) and a table that stores customer addresses (Customer_Addresses). Each customer may have up to 10 addresses.
I need to create Crystal Reports to get all orders between a specified date range, where the customer is in a selected country. Such as between 01/01/2016 - 31/12/2016 in the UK.
I have considered selecting all address codes where Country = selected_country from Customer_Addresses, then storing the results in a DataTable. Then I'd use a selection formula of {Sales Lines.Order_Date} >= myDate AND {Sales Lines.Order_Date} <= myOtherDate AND {Sales Lines.Address} = (One of the codes within the DataTable)
What code would I need after {Sales Lines.Address} = to search for records where the address code in Sales Lines.Address is also stored in the DataTable I created when selecting from Customer_Addresses?

Crystal Reports has a simple functionality for this kind of tasks:
Just join from Sales Lines to Customer_Adresses.
Now you can set your date filter and your country filter directly.
You will have duplicates in your data if more than one customer address for the same order matches with the country code. But this is no problem. Crystal Reports has a option to enforce distinct data. Just activate this option in the database menu (it will use a DISTINCT in SQL)...

Related

How can I get accurate totals from a subform when in splitform view, currently each record needs to be selected before the total is correct

I am setting up a system for the reconciliation process of accounts payable. I set up a few tables for inputting the groups of tickets that we get every day to log them before we get in the invoices.
I have a handful of tables that provide information about some of the fields in the tables but the key tables are:
Ticket_Group:
TGID(autonumber)
Ticket_Date
SupplierID(Unique Identifier for Raw Material Provider)
ShipperID(Unique Identifier for Shipping Company of classification)
MaterialID(Unique Identifier for material)
gl_dep(general ledger department)
mat_inv_rec(true/false if material invoice has been received)
freight_inv_rec(true/false if freight invoice has been received)
Ticket:
TID(Autonumber for Each Ticket)
TGID(Links to TGID in Ticket_Group, Many TID to One TGID)
qty(Quantity of Material)
What I want to be able to do is see all of the Records from Ticket_Group that still have their invoices not received. But I also need to see the totals so I can match them correctly. Not exactly the ideal way to do AP reconciliation but it is much better than how we currently do it(pseudo-pivot table with supplier and shipper combinations along one side and days of the month along the top and individual tickets as values being added together in each cell).
What I have now is a splitform of the Ticket_Group table with the Ticket subform. I put a sum([qty]) in the footer of the subform and referenced that field in the mainform. This only half works because the total is right in the single form view but the datasheet view displays the selected records total as the total for all records until each individual record has been selected.
I thought maybe an onload event for the form that cycled through every record might work, but I figured there must be a better way.
I am super new with MS Access so I really appreciate the help!
Referring to the Subform-Field can't work because it refers to the actual (selected) record of the mainform.
That's why the Datasheet-View shows the value of the actual selected record of mainform for all records, because there is only one subform with the caclulated field that changes depending on the mainforms record, not a new one for every record of the mainform.
To get the value in the Datasheet-View get it from the tables, not the subform.
E.g. set the Control-Sourceof a TextBpx to:
=DSum("qty","Ticket","TGID = " & Ticket_Group.TGID)
or add the expression to yourForm.RecordSourceas a field like:
Select Ticket_Group.TGID, Ticket_Group.Ticket_Date, ... , DSum("qty","Ticket","TGID = " & Ticket_Group.TGID) as SumQty FROM Ticket_Group
to have the form editable.
Or join
Select TGID, Sum(qty) As SumTicketQty FROM Ticket Group By TGID
with your mainform's query and then bind it to the mainform, but that leads to a readonly query.

get previous total from filtered amount in crystal report

I have created the following report in Crystal Report using VB.Net 2012. The OPENING BALANCE & NET TOTAL rows are not in my report but my question is there.
TR_ID | TR_DATE |DETAILS | EXPENSE | INCOME | BALANCE
OPENING BALANCE 0.00
1005 08/24/2015 CASH RTN 0.00 10,000.00 10,000.00
1013 08/25/2015 PURCHASE 3,500.00 0.00 6,500.00
1014 08/25/2015 DEPOSIT 5,000.00 0.00 1,500.00
1013 08/27/2015 SALES 0.00 4,000.00 5,500.00
NET TOTAL 5,500.00
I filtered the specific records within the date range of 08/25/2015 and 08/26/2015 & it works well. My question is how I get the previous balance (10000) of the specific date (08/25/2015) in "OPENING BALANCE" row and the total balance in "NET TOTAL" row?
As per the given datas above, the report shows the 4 records well excluding the rows of OPENING BALANCE & NET TOTAL. I want to add the OPENING BALANCE row to show the opening balance of every vendors' accounts when I open the report. And if I view the records of a particular customer within a date range, then OPENING BALANCE row should show the previous balance until the start date of the given date criteria. Ex: If I select records from 08/25/2015 to 08/26/2015 as per above datas, the report will show the two particular records (2nd & 3rd). But I want to show the previous balance 10,000 or the total amount which is above to the star date (08/25/2015) should be shown in the OPENING BALANCE row. And the sub total amount of this vendor's
should be shown in the last NET TOTAL row. So, pls help me, how the formulas will come & how to do.
Thank you
I expect your great help
Thank you
It´s not too much clear to me, but you may try to get the values directly from the report using a variable to do it.
With the report opened, you may entry in the CrystalReport option and select "Selection Expert - Saved Data".
You must create a formula using the "Formula Field" at left panel, and build it using the top 3 windows:
The first top window shows the fields of the table your report handles;
The middle top window show some built-in formulas
SO, you must
Go to formula field and create a new one;
Select the field at the top-left window you want to get (the Openning)
Drag-drop it into the main window (below the 3 top selections) and save it as a new variable
Add this variable into your report. This way, you may get this field for calculations in the same report.
Is it you need to do?
Sorry if I cannot understand exactly what you need.
Hugs!
UPDATE
You may have TOTAL creating a formula of SUM within report.
Click on the field EXPENSE (which appear in 3 rows) and select in menu: CRYSTAL REPORTS - INSERT - SUMMARY. It will create a field that must be positioned at the bottom (in the new Group will be created).
Do the same with INCOME field.
Create a FORMULA FIELD at the bottom to calculate the difference between both SUMARIES. You can name each of these summary fields (like IncSum and ExpSum) and put in that third field: IncSum-ExpSum. You may also put them as invisible.
Your final balance should be:
OldBalance (the parameter I point next lines)
+
Movement (the field that reflects IncSum-ExpSum)
To get the last/old Balance, you may have to get it from code as pass it as a PARAMETER. To do this, create a parameter in te report: see in the FIELD EXPLORER you have a choice of PARAMETER FIELD. AFter create it, use this code:
Dim MyCrystalReportt As New ReportDocument()
MyCrystalReportt.Load(Application.StartupPath & "\ReportSample.rpt\")
MyCrystalReportt.SetDatabaseLogon(UserID, Password, DataSource, InitialCatalog)
Dim myLogonInfo As New TableLogOnInfo()
Dim myTable As Table
For Each myTable In MyCrystalReportt.Database.Tables
myLogonInfo = myTable.LogOnInfo
myLogonInfo.ConnectionInfo.ServerName = DataSource
myLogonInfo.ConnectionInfo.DatabaseName = InitialCatalog
myLogonInfo.ConnectionInfo.UserID = UserID
myLogonInfo.ConnectionInfo.Password = Password
myTable.ApplyLogOnInfo(myLogonInfo)
Next
MyCrystalReportt.SetParameterValue("Data1", LastBalance.Value.ToString)
ReportsForm.CrystalReportViewer1.Reportsource = MyCrystalReport
ReportsForm.show
Notes:
You may have a form (ReportsForm) with a Crystal Control within (CrystalReportViewer1).
Datasource is the Server Name or IP
InitialCatalog is the Database (not the table!)
The ParameterValue MUST be a string - is numeric is required, convert it to number inside the Report. Use a temp_field to get it (see formulas of Crystal and you will find some to make this task).
I hope it can help you.
Good luck.

Create one query with sum and count with each value pulled from a different table

I am trying to create a query that pulls two different aggregated values from three different tables during a specific date range. I am working in Access 2003.
I have:
tblPO which has the high level purchase order description (company name, shop order #, date of order, etc)
tblPODescription which has the dollar values of the individual line items from customers the purchase order
tblCostSheets which as a breakdown of the individual pieces that we need to manufacture to satisfy the customers purchase order.
I am looking to create a query that will allow me, based on the Shop Order #, to get both the sum of the dollar values from tblPODescriptions and the count of the different type of pieces we need to make from tblCostSheets.
A quick caveat: the purchase order may have 5 line items for a sum of say $1560 but it might take us making 8 or 9 different parts to satisfy those 5 line items. I can easily create a query that pulls either the sum or the count by themselves, but when I created my query with both, I end up with numbers that are multipled versions of what I want. I believe it is multiplying my piece counts and dollar values.
SELECT DISTINCTROW tblPO.CompanyName, tblPO.ShopOrderNo, tbl.OrderDate, Sum(tblPODescriptions.ItemAmount) AS SumOfItemAmount, Count(tblCostSheets.Description) AS CountOfDescription
FROM (tblPO INNER JOIN tblPODescriptions ON (tblPO.CompanyName = tblPODescriptions.CompanyName) AND (tblPO.PurchaseOrderNo = tblPODescriptions.PurchaseOrderNo) AND (tblPO.PODate = tblPODescriptions.PODate)) INNER JOIN tblCostSheets ON tblPO.ShopOrderNo = tblCostSheets.ShopOrderNo
GROUP BY tblPO.CompanyName, tblPO.ShopOrderNo, tblPO.OrderDate
HAVING (((tblPO.OrderDate) Between [Enter Start Date:] And [Enter End Date:]));

Calculating a Sum Value on a report in Access

I have a report that is supposed to show the Stock Value of products before and after conversion.
I have the source products product code and stock value displayed on the form, now I need to get the value of the items converted from that one piece of stock.
I assumed this would be possible to achieve using some simple SQL to calculate the sum of all resulting products with the same SCID (Stock Conversion ID). My SCID is used to link a source product to a number of resulting products in a different table.
In this image I have named the SCID boc in the detail section sSCID to try and differentiate it from any fields in tables that it may try to pick up. But basically I need to get the code to look for result products that match the SCID displayed in that box and add them all together to get the sum total of converted stock.
If context helps I am trying to create a form that shows the value of stock before and after a conversion process in order to try and calculate the wastage.
Thanks,
Bob P
You can either use DSum or build your report query to include the calculation. I would prefer the second option. This assumes that SCID is numeric, you would need quotes for a text SCID.
=DSum("sValue","[Stock Conversion Items]","SCID=" & sSCID)
Or
SELECT r.This,r.That,s.ConvValue
FROM ReportTable r
INNER JOIN (
SELECT SCID, Sum(sValue) As ConvValue
FROM [Stock Conversion Items]
GROUP BY SCID) s
ON r.sSCID = s.SCID
The above query uses aliases r and s for the tables : ReportTable r or ReportTables As r

SQL Query For Product Sales Report

I have a query that gives a product sales report by whatever date range I specify.
Something like select whatever from wherever where date ordered between start date and end date order by product id.
My page then loop through the recordset and displays the results on the page in a list.
What I would like to do is provide a list showing PRODUCT A total sales = whatever, PRODUCT B total sales = whatever so on and so forth. So as the loop runs product a = product a + 1
I do this already with staff sales, but there are only 5 staff so I have managed to do this, but there are over 300 product codes.
What is the best way to proceed.
Possible solutions:
Do this in your application code by keeping track of the product code and running totals. When the product code changes, emit an extra row with the totals into the output.
Do something similar to #1, but use a separate GROUP BY query to get the totals.
Create a SELECT statement that UNIONs together two queries, one for the product detail lines and one with the summary information.
Use some product specific command (you don't say what database you're using) to accomplish #3 without having to do the UNION yourself. Both MySQL and SQL Server offer (different) ROLL UP clauses that can do what you want.