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!
tommyboy115Asked:
Who is Participating?
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Numbers ... try this:


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

I have to go ... zzzzzz

0
 
Raynard7Commented:
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
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
TYPO ***

"Do think you need the parens ... "

DON'T think ....

mx
0
 
tommyboy115Author Commented:
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.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Does that usually create any additional problems?

Not really.

You are referencing one form from another ?

mx
0
 
tommyboy115Author Commented:
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.
1
 
tommyboy115Author Commented:
Go to sleep, you've earned it.  That was the problem.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
lol.   Well, we got lucky on that one ....

thank you ...

mx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.