MS ACCESS – What is an easy way to compare 2 text fields to validate that they are equal?

MS ACCESS – What is an easy way to compare 2 text fields to validate that they are equal?
1 Table, two serial number labels being scanned in via barcodes.
I want to validate that they are the same serial numbers. The fields are AlphaNumberic IE: SNME12345

I have SN1 and SN2 fields, if they are NOT equal I want to display a msgbox and or conditionally make a field turn red.

Best answer:

if a <> b then
msgbox “jhgjyg”

Sorry headcirc: === is not supported in MS Access (at least through version 2003). Can’t use it :)

Tags: , , , , , , ,

Under Forum

2 Comments for MS ACCESS – What is an easy way to compare 2 text fields to validate that they are equal?

  • 1. headcircus  |  January 15th, 2007 at 11:01 am

    Often times people give examples that may work for you right now, and as the weeks go by you continue to use that pattern when in fact it is not right for all expressions.

    For alphanumeric, in the server or client code, treat it as a string.

    A <> B or A != B, depending on your language.

    For dead-on accuracy across all objects (for a good universal pattern, do this)

    !(A===B)

    === is the true object comparator in JavaScript that does not allow the language to do type conversions. Thus, a negated test of this nature guarantees no “false negatives” as you code into the future.

    Up to you.

  • 2. Capt Crasher  |  January 15th, 2007 at 11:46 am

    I’m not sure why Your getting JAVA Script answers for Access, especially when the people GIVING the answer tell You Access won’t support it…

    If Your doing it in a FORM this would be farily easy. In the PROPERTIES of the SN2 TextBox, under EVENTS (I’m not sure which would be best for this, probably AFTER UPDATE or ON CHANGE(??), but You’ll have to tweak it anyway)… You can write an easy VBA code similar to this:

    Private Sub MatchSN()
    Dim Data1, Data2 as String
    Data1 = Me.[SN1]
    Data2 =Me.[SN2]

    IF Data1 = Data2 then
    Me.SN2.BackColor = 16777215 ‘White
    Exit Sub
    ELSE:
    Me.SN2.BackColor = 255 ‘Red
    Msgbox “Serials DO NOT MATCH!”
    Me.SN2.Setfocus
    Exit Sub
    End if

    End Sub

    You could also try using the “STRCOMP” Function (String Compare).

    In Access 2007 they have a “Conditional Formatting Wizard”, so You could try using that if Your in 2007…

    If any of it gives You trouble, try researching it in the Microsoft Knowledge base, or taking it to a forum that allows “discussions”…

Leave a Comment for MS ACCESS – What is an easy way to compare 2 text fields to validate that they are equal?

Required

Required, hidden

Trackback this post  |  Subscribe to the comments via RSS Feed


Forum