I have code that checks whether GTotal and CTotal are equal. If they’re not, the user shouldn’t be able to send it off for Approval. However, it works 99% of the time. On occasion, there is a document that slip through for Approval, even though the GTotal and CTotal aren’t equal. Any ideas how I can make my code more efficient to ensure it works 100% for every document?
Thx
My code ******
If Format(doc.ctotal(0), “Standard”) <> Format(doc.gtotal(0), “Standard”) Then
Msgbox “Total cost is not equal to allocated cost.”
Exit Sub
Else
If doc.Gtotal(0) = 0 Then
Msgbox "Items and their cost must be entered prior to submitting. Total cost is currently $0.00."
You’ll need to post more of the code. All I can tell from this, really, is that you aren’t in the habit of using the Elseif keyword. If you have multiple conditions being tested throughout your code and are not using either Elseif or Select Case, the flow of execution can be difficult to read. Are you sure the equality test is even happening? Have you stepped through this in the debugger?
If you are sure that you GTotal and CTotal are numeric you don’t need to format them, you can just compare them as in:
if doc.CTotal(0) <> doc.GTotal(0) then …
Using format it’s possible for example that GTotal could be text and CTotal a number that formatted to GTotal and they would match.
One thing to also watch if you are comparing numbers is round-off errors, if you are using dollars for example you might want to test for being within a tenth of a cent, such as:
if Abs(doc.CTotal(0) - doc.GTotal(0)) < 0.001 then …
The last problem I encountered was that GTotal was a value of $425.50 and CTotal was $851.00 (the user duplicated the entry and came up with this value). And although they were obviously not equal, the document was still mailed for approval.