Compare Two Values before continue...

MackMan

Registered User.
Local time
Today, 06:18
Joined
Nov 25, 2014
Messages
174
Quick one...

I have two subforms. The first is a summary form and carries a "TotalCredit" field across to another subform in Datasheet mode where the total credit is being broken down to individual postings, giving me a breakdown of the "TotalCredit" on the first form. These totals should always match.

However, as simple as it sounds, I'm struggling with a msgbox to check if they actually do agree. If so, then carry on and exit the datasheet subform and return me to the main subform to carry on entering data, but if not, then throw me a critical box up and say I'm no good, and totally rubbish at maths :rolleyes:

If I purposely ensure the values aren't equal I've tried various ways round, and what ever I do, it'll throw up the msgbox then tell me I'm useless, and then move on to the other subform anyway, or If I try to figure out what's going on, it'll just carry on anyway, as if all is well.. which it isn't.

I'm aware that this is just a couple of lines of VBA, but for my life, I'm just not getting it... I must be having a brain freeze day!

Private Sub frmPosts_Exit(Cancel As Integer)
If [Forms]![frmtopline]![frmPosts].[Form]![txtposttotalcredit] <> [Forms]![frmtopline]![frmTopLineSub].[Form]![Credit] Then
MsgBox "Credits do not agree!", vbCritical, "Verify!"
[Forms]![frmtopline]![frmPosts].SetFocus
[Forms]![frmtopline]![frmPosts].[Form]![Credit].SetFocus

End If
End Sub
 
you are referencing the subforms incorrectly.

If frmPosts is your main form then you would reference fields in a subform as

me.subformame.form.fieldname

if frmposts is a subform then you would reference the main form as

parent.fieldname

and if you want to reference a value in another subform then you would use

parent.othersubformname.form.fieldname
 
It's hard to tell from your verbiaage what the issue is. Is it so that the two numbers hardly ever are deemed equal by the code?

IF so, what is the variable type of Credit and the variables summed in txtposttotalcredit? All should be Currency (or Long, if you do not use decimal numbers).

For SIngle and Double, when summing you'll get decimal fluff that you cannot see but the machine does. For such numbers the comparison criterion is:

Abs(Num1-Num2)< eps, where eps is a small positive number measuring the difference and below which you consider two numbers being equal.
 
Hi spike & CJ. Super thanks for responding so quick!

Both should always be equal. If not, then throw me an error message to let me know.

They are both Currency type, and to be perfectly honest, I thought it would be a case of saying if A does not equal B then it will not continue until it does.
I'm more than average when it comes to VBA, but I must confess, I've never even heard of eps. However, one thing I have recently discovered through testing is, that even though if the difference is incorrect and the message box does not appear using the TAB key, if I go back into it, and use the ENTER key, it throws up the error.

So although I will try eps, am I right in thinking that TAB and ENTER have two functions? Odd.. or maybe it's just the way I've programmed for setfocus etc.

Awesome work guys! I'm always learning from the experts!
Brilliant!
 

Users who are viewing this thread

Back
Top Bottom