[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 23961
  • Last Modified:

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!
0
tommyboy115
Asked:
tommyboy115
  • 5
  • 3
1 Solution
 
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 MVP, Access and Data Platform)Commented:
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
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
TYPO ***

"Do think you need the parens ... "

DON'T think ....

mx
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

thank you ...

mx
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now