Totals not equal but sent anyway?

Hi,

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."

Exit Sub

Else

  Send for approval code

Subject: Totals not equal but sent anyway?

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?

Subject: Totals not equal but sent anyway?

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 …

Subject: RE: Totals not equal but sent anyway?

I thnk Richard is right, it maybe a rounding issue (as standard seems to be two decimal places, and you want to do something like

if Abs(doc.CTotal(0) - doc.GTotal(0)) < 0.001 then

In this example the system thinks the values are equal:

Dim s As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Set db = s.CurrentDatabase

Set doc = db.CreateDocument

doc.ctotal = 1.005

doc.gtotal = 1.007

. . .

a = Format(doc.ctotal(0), “Standard”)

b = Format(doc.gtotal(0), “Standard”)

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.”

. . . . . . Exit Sub

. . . Else

. . . . . . Msgbox “Send”

. . . End If

End If

Subject: RE: Totals not equal but sent anyway?

Thank you Richard & Stephen, I will give it a go.

Thanks again :slight_smile:

Subject: RE: Totals not equal but sent anyway?

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.