tommyboy115
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!
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]
'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!
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
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
"Do think you need the parens ... "
DON'T think ....
mx
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
Not really.
You are referencing one form from another ?
mx
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Go to sleep, you've earned it. That was the problem.
lol. Well, we got lucky on that one ....
thank you ...
mx
thank you ...
mx
you may want to do the .text argument
ie
If([FORMS]![FORM1]![TEXT1]
however you may want to ensure that they are actually different, ie
dim str1 as string
dim str2 as string
str1 = [FORMS]![FORM1]![TEXT1].te
str2 = [FORMS]![FORM2]![TEXT2].te
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