Solved

DSum syntax with two form criteria

Posted on 2014-02-17
6
409 Views
Last Modified: 2014-02-17
I have a syntax issue with the folloing code using two criteria.  Can someone spot the problem?

Me.txtAvailQty = DSum("[IncomingQty]", "tblInventoryDetails", -"[SoldQty]", "tblInventoryDetails", "[ProductID] = " & [Forms]![frmInventory]![txtProdID], "[LocationID] " & [Forms]![frmInventory]![txtToID])
0
Comment
Question by:SteveL13
  • 3
  • 3
6 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 39864844
Me.txtAvailQty = DSum("[IncomingQty]", "tblInventoryDetails",  "tblInventoryDetails", "[ProductID] = " & [Forms]![frmInventory]![txtProdID] & " AND [LocationID] = " & [Forms]![frmInventory]![txtToID])

Sometimes it is easier to understand SQL Strings if you build them as a variable so you can see the results of your effort.

Dim strWhere As String
strWhere =   "[ProductID] = " & [Forms]![frmInventory]![txtProdID] & " AND [LocationID] = " & [Forms]![frmInventory]![txtToID])

Me.txtAvailQty = DSum("[IncomingQty]", "tblInventoryDetails", "tblInventoryDetails", strWhere)

PS -- I removed ---- -"[SoldQty] ", --- because I couldn't figure out what it went to.  It isn't the table/query name and didn't have any variable to compare to so it didn't look like it should be part of the where clause.
0
 

Author Comment

by:SteveL13
ID: 39864949
I have to have SoldQty because AvailQty - Sum of IncomingQty minus Sum of SoldQty
0
 
LVL 34

Accepted Solution

by:
PatHartman earned 500 total points
ID: 39865126
You can only get a single column from a domain function.

Is SoldQty in the same table as IncomingQty?  

There are two possible solutions and I don't know which will give you the answer you need.  Probably the second one since I suspect that each transaction would be in a separate record.

Me.txtAvailQty = DSum("[IncomingQty] - [SoldQty]", "tblInventoryDetails", "tblInventoryDetails", strWhere)

OR

Me.txtAvailQty = DSum("[IncomingQty]", "tblInventoryDetails", "tblInventoryDetails", strWhere) - DSum("[SoldQty]", "tblInventoryDetails", "tblInventoryDetails", strWhere)
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

by:SteveL13
ID: 39865364
This worked...

Me.txtAvailQty = DSum("[IncomingQty]", "tblInventoryDetails", "tblInventoryDetails", strWhere) - DSum("[SoldQty]", "tblInventoryDetails", "tblInventoryDetails", strWhere)

but I had to remove the double "tblInventoryDetails" in 2 place.   Thanks.

--Steve
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 39865685
I just washed my hands and can't do a thing with them.  Glad you worked it out.
0
 

Author Comment

by:SteveL13
ID: 39865873
But your solution worked.  I just spotted the double "tblInventoryDetails".  At least I know how to do that.  Thanks again.  I appreciate your efforts.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now