Solved Query from Crosstab Query Error Message because of NullStock (1 Viewer)

ahmad_rmh

Member
Local time
Today, 19:49
Joined
Jun 26, 2022
Messages
243
I have made a query from Crosstab Query to make the "Stock Summary Balance" but if one of the column Heading balance is not available then it shows error message becuase of null stock otherwise the query is working with balances.

Error Message: The Microsoft office database engine does not recognize '[OpeningStock]' as a valid field name or expression
(Image is attached herewith)
Note: This message is coming because of the null stock,

How could I avoid this error to happen? If there is other way to get Stock summary balance in crosstab query then let me know please.


The detail of the queries are as under;

a. Columns in Query made from corsstab query: ItemsPK, ItemsListName, Opening, Purchases, Transfers, Adjustments, StockBalance, AvgUsage
(Images are attached herewith)

b. Columns in Crosstab Query: ItemsPk, ItemsListName, Stock, SumofStock_In_Out
(Images are attached herewith)

c. Union Query:

SELECT ItemsPK, ItemsListName, Sum(Stock_In_Out) AS SumOfStock_In_Out, TranDate, TranTypePK, "OpeningStock" AS Stock
FROM qryTransactionsExtended
GROUP BY ItemsPK, ItemsListName, TranDate, TranTypePK
HAVING (((TranDate)<=DateSerial(Year(Date()),Month(Date()),0)))
ORDER BY ItemsPK

UNION ALL

SELECT ItemsPK, ItemsListName, Sum(Stock_In_Out) AS SumOfStock_In_Out, TranDate,TranTypePK, "NetPurchases" AS Stock
FROM qryTransactionsExtended
GROUP BY ItemsPK, ItemsListName, TranDate, TranTypePK
HAVING (((TranDate) Between DateSerial(Year(Date()),Month(Date())+1,1) And DateSerial(Year(Date()),Month(Date()),0)) AND ((TranTypePK)<=2))

UNION ALL

SELECT ItemsPK, ItemsListName, Sum(Stock_In_Out) AS SumOfStock_In_Out, TranDate, TranTypePK, "NetTransfers" AS Stock
FROM qryTransactionsExtended
GROUP BY ItemsPK, ItemsListName, TranDate, TranTypePK
HAVING (((TranDate) Between DateSerial(Year(Date()),Month(Date())+1,1) And DateSerial(Year(Date()),Month(Date()),0)) AND ((TranTypePK)>=3 And (TranTypePK)<=5))

UNION ALL

SELECT ItemsPK, ItemsListName, Sum(Stock_In_Out) AS SumOfStock_In_Out, TranDate, TranTypePK, "MonthlyAdjustments" AS Stock
FROM qryTransactionsExtended
GROUP BY ItemsPK, ItemsListName, TranDate, TranTypePK
HAVING (((TranDate) Between DateSerial(Year(Date()),Month(Date())+1,1) And DateSerial(Year(Date()),Month(Date()),0)) AND ((TranTypePK)=6));

(Image is attached herewith)



Kindly suggest the solution.
 

Attachments

  • ErrorMessage1.GIF
    ErrorMessage1.GIF
    33 KB · Views: 87
  • ErrorMessage2.GIF
    ErrorMessage2.GIF
    30.2 KB · Views: 75
  • Crosstab_Query.GIF
    Crosstab_Query.GIF
    180.9 KB · Views: 80
  • Crosstab_Query_1.GIF
    Crosstab_Query_1.GIF
    49.4 KB · Views: 79
  • qry_from_crosstab_Query.GIF
    qry_from_crosstab_Query.GIF
    248.4 KB · Views: 73
  • qry_from_crosstab_Query_1.GIF
    qry_from_crosstab_Query_1.GIF
    55.1 KB · Views: 79
  • Union_Query_Results.GIF
    Union_Query_Results.GIF
    87.8 KB · Views: 81
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 09:49
Joined
Oct 29, 2018
Messages
21,454
One possible approach is to make sure the Crosstab query always has all possible columns included by using the In() clause or the Column Headings property.
 

ahmad_rmh

Member
Local time
Today, 19:49
Joined
Jun 26, 2022
Messages
243
One possible approach is to make sure the Crosstab query always has all possible columns included by using the In() clause or the Column Headings property.

Sorry, I didn't get the idea, kindly tell me how to do....
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 12:49
Joined
May 21, 2018
Messages
8,525
I am not sure if that is the problem, but here is the way to do what @theDBguy is suggesting using the query designer
In SQL this creates a PIVOT IN clause.

Again I am not clear on your issue, but one thing before doing a crosstab is to use NZ() around fields that could return null.
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 17:49
Joined
Feb 19, 2013
Messages
16,608
@ahmad_rmh - try a simpler query - based on the db I sent you a while ago, avoids the need for a union uqery

Code:
TRANSFORM Sum([Quantity]*[multiplier]) AS SM
SELECT tblItems.ItemPK, tblItems.ItemsListName
FROM tblItems INNER JOIN (tblTranTypes INNER JOIN tblTransactions ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK) ON tblItems.ItemPK = tblTransactions.ItemsFK
GROUP BY tblItems.ItemPK, tblItems.ItemsListName
ORDER BY Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments")
PIVOT Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments") In ("Opening Balance","NetPurchases","NetTransfers","MonthlyAdjustments");

and to include the current stock as well - I don't know what the average usage calculation is (perhaps netTransfers/day(date())?) but should be easy enough to do in a form or report

Code:
TRANSFORM Sum([Quantity]*[multiplier]) AS SM
SELECT tblItems.ItemPK, tblItems.ItemsListName, Sum([Quantity]*[multiplier]) AS CurrentStock
FROM tblTranTypes INNER JOIN (tblItems INNER JOIN tblTransactions ON tblItems.ItemPK = tblTransactions.ItemsFK) ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK
GROUP BY tblItems.ItemPK, tblItems.ItemsListName
ORDER BY Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments")
PIVOT Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments") In ("Opening Balance","NetPurchases","NetTransfers","MonthlyAdjustments");
 
Last edited:

ahmad_rmh

Member
Local time
Today, 19:49
Joined
Jun 26, 2022
Messages
243
@ahmad_rmh - try a simpler query - based on the db I sent you a while ago, avoids the need for a union uqery

Code:
TRANSFORM Sum([Quantity]*[multiplier]) AS SM
SELECT tblItems.ItemPK, tblItems.ItemsListName
FROM tblItems INNER JOIN (tblTranTypes INNER JOIN tblTransactions ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK) ON tblItems.ItemPK = tblTransactions.ItemsFK
GROUP BY tblItems.ItemPK, tblItems.ItemsListName
ORDER BY Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments")
PIVOT Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments") In ("Opening Balance","NetPurchases","NetTransfers","MonthlyAdjustments");

and to include the current stock as well - I don't know what the average usage calculation is (perhaps netTransfers/day(date())?) but should be easy enough to do in a form or report

Code:
TRANSFORM Sum([Quantity]*[multiplier]) AS SM
SELECT tblItems.ItemPK, tblItems.ItemsListName, Sum([Quantity]*[multiplier]) AS CurrentStock
FROM tblTranTypes INNER JOIN (tblItems INNER JOIN tblTransactions ON tblItems.ItemPK = tblTransactions.ItemsFK) ON tblTranTypes.TranTypePK = tblTransactions.TranTypeFK
GROUP BY tblItems.ItemPK, tblItems.ItemsListName
ORDER BY Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments")
PIVOT Switch([trandate]<=Date()-Day(Date()),"Opening Balance",[trantypePK]<=2,"NetPurchases",[trantypePK]<=5,"NetTransfers",True,"MonthlyAdjustments") In ("Opening Balance","NetPurchases","NetTransfers","MonthlyAdjustments");


Thanks a lot CJ, It has worked a lot and now no need of union query and its related queries.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 17:49
Joined
Feb 19, 2013
Messages
16,608
happy to help. If you want to regularly group your transactions into 'net' movements you can always add a another grouping table to simplify the query further (removes the need for the switch function) - I do this for financial systems where I want to group nominal codes into things like sales, cost of sales, overheads etc., often several levels deep for views at different levels of granularity.
 

Users who are viewing this thread

Top Bottom