Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

If then question

On a form I have two fields.  When the user enters a value that does not match the value in another field I want a message box to popup.  Here is my code which is not working.  It pops up even if the two field values do match.  Note that both fields are number fields.

    If Me.cbofield2 <> Me.cbofield1 Then
        MsgBox "Caution:  The value does not match the value in field 1."
    End If
Avatar of jamie77777
jamie77777

If not Me.cbofield2 = Me.cbofield1 Then
        MsgBox "Caution:  The value does not match the value in field 1."
    End If
Avatar of Darrell Porter
Are both fields the same type of numeric field (long vs integer vs real vs double, etc) ?
Use the Form BeforeUpdate event, otherwise, you will have to test each combo and cover more cases (null, not null, etc)

Private Sub Form_BeforeUpdate (Cancel As Integer)

    If Nz(Me.cbofield2,"")  <> Nz(Me.cbofield1,"") Then
        MsgBox "Caution:  The value does not match the value in field 1."
    End If

End Sub
...Watch for misspellings, and upper/lower case issues as well;

Mike Jone/Mike Jones
Issac, 1ssac

William Smith/william smith


With numbers, you may have a value like:
4.12345
But "Format" it to display:
4.12

So when you compare this formatted value, to an actual value of 4.12, then will not be equal, even though they "appear" to be the same...


JeffCoachman
I assume that the fields you are referring are combo boxes. You may check as well the row source of your combo boxes as well as the column bound.

Sincerely,
Ed
Avatar of SteveL13

ASKER

This is not working and I suspect that it is because the bound column on the combo box is displaying as 00.00 instead of as a full number with no decimals.  How can I display with no decimals?
I now have the number displaying as a full number, no decimals but the form is not working correctly.  I get the message box even if the values do match.
We have covered almost all the bases here...

Obviously we are at the point where you will have to post a sample db that exhibits this issue.

Then I am sure any expert will see the problem immediately...
Steve ... what are some examples of the numbers in each combo - w/o any formatting ?

mx

Private Sub Form_BeforeUpdate (Cancel As Integer)

   dim x,y
   x= Replace(Replace(Nz(Me.cbofield1,0) ,".","") ,",","")
   y= Replace(Replace(Nz(Me.cbofield2,0) ,".","") ,",","")
    If x <> y Then
        MsgBox "Caution:  The values do NOT match - take evasive, preemptive action now."
    End If

End Sub

So, if x = 125.210  and y =125.121  ... they will not match

mx
This seems to work.  But now one more issue.  The message box does appear if the two fields don't match.  But I want the form to stay open and set the focus on the field, cbofield2 so the user can correct the mistake.
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial