Solved

Error in DSum textbox while on new record

Posted on 2008-10-30
5
335 Views
Last Modified: 2013-11-28
I have a form in datasheet view where the user chooses a few items to buy. I then have a textbox that calculates the total price of those items. The control source of the Totals textbox is as follows:

=DSum("([Price]+([Price]*[Adjust]))*[Quantity]","QuoteDetails","[QuoteID] =" & [QuoteID] & "AND [Optional]=0")

The problem is that when I am on the form and the cursor is in one of the fields for a new record, the Totals textbox shows #Error. Why is this happening and what can I do to prevent this #Error from showing up when the user is selecting a new item?
Thank you for your help.
0
Comment
Question by:cers4686
5 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 22844168


=DSum("(nz([Price],0)+(nz([Price],0)*nz([Adjust],0)))*nz([Quantity],0)","QuoteDetails","[QuoteID] =" & [QuoteID] & " AND [Optional]=0")
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 22844187

try this

=IIF(Not IsNull([YourPrimaryKeyFieldName],   DSum("([Price]+([Price]*[Adjust]))*[Quantity]","QuoteDetails","[QuoteID] =" & [QuoteID] & "AND [Optional]=0") , Null)

Replace YourPrimaryKeyFieldName  with the primary key field name - or some field you can guarantee will not be Null IF if you are on a record (not on a New record)

mx
0
 
LVL 57
ID: 22844438

Or use the function attached like so:

=AvoidError(DSum("([Price]+([Price]*[Adjust]))*[Quantity]","QuoteDetails","[QuoteID] =" & [QuoteID] & "AND [Optional]=0"))


Function AvoidError(n As Variant, varReplaceWith As Variant)
         
    On Error GoTo AvoidError_Error
    
    AvoidError = Nz(n, varReplaceWith)
    
AvoidError_Exit:
    Exit Function
 
AvoidError_Error:
    AvoidError = varReplaceWith
    Resume AvoidError_Exit
    
End Function

Open in new window

0
 

Author Closing Comment

by:cers4686
ID: 31511803
brilliant! thank you!
0
 
LVL 75
ID: 22844596
You are welcome ...

mx
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

825 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