Link to home
Start Free TrialLog in
Avatar of tommyboy115
tommyboy115Flag for United States of America

asked on

Testing for If Not Equal in VBA

Hello,
In VBA, if I want to base an If statement on two values equaling or not equaling each other, do I do it like this:

If([FORMS]![FORM1]![TEXT1] <> [FORMS]![FORM2]![TEXT2] ) Then
     'Do Something1
Else
     'Do Something2
End If

This keeps returning "Do Something1", regardless of if they are equal or not equal.

What am I doing wrong? Help!
Avatar of Raynard7
Raynard7

Hi this should be working,

you may want to do the .text argument

ie

If([FORMS]![FORM1]![TEXT1].text <> [FORMS]![FORM2]![TEXT2].text ) Then


however you may want to ensure that they are actually different, ie

dim str1 as string
dim str2 as string

str1 = [FORMS]![FORM1]![TEXT1].text
str2 = [FORMS]![FORM2]![TEXT2].text

you could then do

if str1 <> str2 then
...

and put the line

debug.print "str1 is " & str1 & " and str2 is " & str2

and have a look at the results
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Do think you need the parens ...

If [FORMS]![FORM1]![TEXT1] <> [FORMS]![FORM2]![TEXT2]  Then
     'Do Something1
Else
     'Do Something2
End If

or try this:

If [FORMS]![FORM1]![TEXT1] = [FORMS]![FORM2]![TEXT2]  Then
     'Do Something2
Else
     'Do Something1
End If

mx

TYPO ***

"Do think you need the parens ... "

DON'T think ....

mx
Avatar of tommyboy115

ASKER

I seem to be having this problem with listboxes.  Does that usually create any additional problems?  I have the column set correctly and I'm using the value from the listbox in other code and it's working fine, but not for this.  I'm actually applying this listbox value to a form textbox and using this check to make sure the value isn't applied again once they've already done it.  I already applied the value that I'm checking, so they're definitely the same.
Does that usually create any additional problems?

Not really.

You are referencing one form from another ?

mx
Yes, from one form to another.  This isn't really making any sense to me either. Using <> or = doesn't work.  If I replace, say, [FORMS]![FORM2]![TEXT2], with the actual value, it works.  That would lead one to believe that the value for [FORMS]![FORM2]![TEXT2] is wrong and they don't match, but it isn't.  I msgbox it and they're both equal.  The only thing I can think of is that the ListBox is changing the format of the value, they're both numbers.  I try formatting it before comparing, but that doesn't work either.  I'm at a loss.
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
Go to sleep, you've earned it.  That was the problem.
lol.   Well, we got lucky on that one ....

thank you ...

mx