Comparison of string content problem

  • Thread starter Thread starter iamrdbrown
  • Start date Start date
I

iamrdbrown

I have the following code for trying to compare the contents of 2 different
fields on a form. My problem is that I get the error message & focus gets
set back to the 1st box regardless of if the contents match or not. Can
someone help me see what I am overlooking?

Private Sub txtRatingLabel_AfterUpdate()
Dim iAnswer As Integer

If txtCartonLabel <> ([txtRatingLabel] & "*") Then
iAnswer = MsgBox("Carton label scanned doesn't match Rating Label
scanned. Please try it again.", , "Label Scan Comparison")
txtRatingLabel.Value = ""
txtCartonLabel.Value = ""
txtCartonLabel.SetFocus
Else
ckbOnHold.SetFocus
End If
 
You are using the inequality operator, <>, but at the same time using the
asterisk, presumably as a wildcard. The expression would only evaluate to
False where the carton label had an asterisk as its final character and
otherwise exactly equalled the rating label.

You need to use the Not and Like operators:

If Not Me.txtCartonLabel Like Me.txtRatingLabel & "*" Then

Just to be sure this is what you want, if the carton label is "abcd" and the
rating label is "abc" then it will evaluate to False, i.e. they match. If
the rating label is "abx" however then it will evaluate to True, i.e. they
don't match, so you'll get the message box popping up.

Also if the carton label were "xabcd" for instance, then with rating labels
of both the above values it would evaluate to True (no match) as you've
included a wild card character only at the end, not at the beginning.

BTW you don't need to return a value with the MsgBox function you can simply
call it like so:

MsgBox "Carton label scanned doesn't match Rating Label scanned. Please try
it again.", vbExclamation, "Label Scan Comparison"

You'll notice that in this case the parentheses are omitted. When you want
a function to return a value include the parentheses, when you just want it
to do perform an action omit them.

Ken Sheridan
Stafford, England
 
Ken Sheridan said:
You are using the inequality operator, <>, but at the same time using the
asterisk, presumably as a wildcard. The expression would only evaluate to
False where the carton label had an asterisk as its final character and
otherwise exactly equalled the rating label.

You need to use the Not and Like operators:

If Not Me.txtCartonLabel Like Me.txtRatingLabel & "*" Then

Just to be sure this is what you want, if the carton label is "abcd" and the
rating label is "abc" then it will evaluate to False, i.e. they match. If
the rating label is "abx" however then it will evaluate to True, i.e. they
don't match, so you'll get the message box popping up.

Also if the carton label were "xabcd" for instance, then with rating labels
of both the above values it would evaluate to True (no match) as you've
included a wild card character only at the end, not at the beginning.

BTW you don't need to return a value with the MsgBox function you can simply
call it like so:

MsgBox "Carton label scanned doesn't match Rating Label scanned. Please try
it again.", vbExclamation, "Label Scan Comparison"

You'll notice that in this case the parentheses are omitted. When you want
a function to return a value include the parentheses, when you just want it
to do perform an action omit them.

Ken Sheridan
Stafford, England


This did work, but I had to change the code as follows:
If Not Me.txtCartonLabel Like Me.txtRatingLabel & "*" Then

I needed the 2 fields to match up to the wild card on the end. I really
appreciate the help & hopefully this will 'stick' in my memory so that I
don't make the same mistake again.

R Brown
 
Back
Top