If Then Statement: Compile Error, Expected: =

TxStrob

Registered User.
Local time
Today, 12:27
Joined
Sep 23, 2019
Messages
44
I am getting an Expected: = and it highlight the less than or greater than statement. What am I doing wrong?

If Me.cboOption = "Plan 1" Or "Plan 2" Then
Nz(Me.Example1, 0) < & Nz(Me.Example2, 0)
Else
Nz(Me.Example1, 0) > & Nz(Me.Example2, 0)
End If

Thank you
 
You want:
Code:
If Me.cboOption = "Plan 1" Or Me.cboOption = "Plan 2" Then


PS. What do you want to happen if cboOption is either Plan1 or Plan2?
 
If Me.cboOption = "Plan 1" Or "Plan 2" Then
When I see that, I can't help but point out that you cannot expect a computer to reason like a human. You have to be VERY explicit. So going forward it might help to consider that in cases like this, each part of a statement needs to stand on its own when there is a logical operator involved. For example, if I said to you "The dog is black and fat" you would get the right picture. If I just said "and fat" you might take a poke at me. At the very least, you would not get the meaning. That's how the computer interprets the right side of your expression.
 
Last edited by a moderator:
You want:
Code:
If Me.cboOption = "Plan 1" Or Me.cboOption = "Plan 2" Then


PS. What do you want to happen if cboOption is either Plan1 or Plan2?

I have two text boxes that compare values, one cannot be greater than other. Thats all.

Nz(Me.Example1, 0) < Nz(Me.Example2, 0)
 
I have two text boxes that compare values, one cannot be greater than other. Thats all.

Nz(Me.Example1, 0) < Nz(Me.Example2, 0)
So, what does cboOption have to do with them?
 
We nor a computer can figure out what you want. Please describe in plain language ( no code). What it is you want to occur and when.

The main problem with your code is you are never issuing a command. You're just doing comparisons for no effect. These are all comparison operators:

=, >, <

Now here's your code in very simple terms:

If comparison1 then comparison2
Else comparison 3

Your code never does anything, just does these pointless comparisons. So, tell us what you want to happen and when
 
So, what does cboOption have to do with them?

Originally I created the If then statement to just compare two set of values. cboOption has five different options. Two of options, if selected needs to be Nz(Me.Example1, 0) < & Nz(Me.Example2, 0), and the rest of the options are
Nz(Me.Example1, 0) > & Nz(Me.Example2, 0).

Thanks
 
if selected needs to be Nz(Me.Example1, 0) < & Nz(Me.Example2, 0), and the rest of the options are
Nz(Me.Example1, 0) > & Nz(Me.Example2, 0)

Again, those don't tell the computer to do anything. They are just comparisons.

Please explain in simple english what it is you want to occur and when (no code). The above means nothing to us nor a computer and we as humans who have better abilities to see meaning in things still can't just by that code alone.
 
I kind of got it working, except the code is stuck on "MsgBox "You number must be greater than Example2.", vbInformation, "Invalid Entry:" it will not move on to the else statement.

Dim msg1 As String
Dim msg2 As String

If Me.cboOption.Column(0) & "1" Or Me.cboOption.Column(0) & "2" Then
msg1 = Nz(Me.Example1, 0) < Nz(Me.Example2, 0)
Cancel = True
MsgBox "You number must be greater than Example2.", vbInformation, "Invalid Entry:"
Else
If Me.cboOption.Column(0) & "" <> Me.cboOption.Column(0) & "1" And Me.cboOption.Column(0) & "2" Then
msg2 = Nz(Me.Example1, 0) > Nz(Me.Example2, 0)
Cancel = True
MsgBox "You number must be less than Example2.", vbInformation, "Invalid Entry:"
End If
End If
 
I have two text boxes that compare values, one cannot be greater than other.

So the values must be equal? Or Null?

Often better for clarity to give a few examples along with the expected/desired result.
Your use of vba has not clarified your requirement - for me.
 
I kind of got it working, except the code is stuck on "MsgBox "You number must be greater than Example2.", vbInformation, "Invalid Entry:" it will not move on to the else statement.

Dim msg1 As String
Dim msg2 As String

If Me.cboOption.Column(0) & "1" Or Me.cboOption.Column(0) & "2" Then
msg1 = Nz(Me.Example1, 0) < Nz(Me.Example2, 0)
Cancel = True
MsgBox "You number must be greater than Example2.", vbInformation, "Invalid Entry:"
Else
If Me.cboOption.Column(0) & "" <> Me.cboOption.Column(0) & "1" And Me.cboOption.Column(0) & "2" Then
msg2 = Nz(Me.Example1, 0) > Nz(Me.Example2, 0)
Cancel = True
MsgBox "You number must be less than Example2.", vbInformation, "Invalid Entry:"
End If
End If
Hi. Does cboOption.Column(0) contains True or False data? Or, what were you expecting to get with this?

Me.cboOption.Column(0) & "1"

Is that supposed to result in either True or False?
 
Hi. Does cboOption.Column(0) contains True or False data? Or, what were you expecting to get with this?

Me.cboOption.Column(0) & "1"

Is that supposed to result in either True or False?

cboOption does not contain true or false data. If the Example1 is greater or less than the Example2 than it shows display the one of the following messages "You number must be greater than Example2." or "You number must be less than Example2."

I kind of got the code working but it will not move on to the Else statement. I have the Cancel = True, so users cannot move on to the next textbox until they adjust the number.
 
cboOption does not contain true or false data. If the Example1 is greater or less than the Example2 than it shows display the one of the following messages "You number must be greater than Example2." or "You number must be less than Example2."

I kind of got the code working but it will not move on to the Else statement. I have the Cancel = True, so users cannot move on to the next textbox until they adjust the number.

Hi. You keep trying to explain about the textboxes, but we can't even understand what the combobox has to do with what you're trying to do. Once we understand that part, I am certain the rest should become clearer, and we can tell you how to fix it.
 
TxStrob
After 13 posts, you have successfully baffled several of the most experienced forum members.
Despite repeated questions from them, none of your answers have made this any clearer

I have to disagree that you 'kind of got this working'. When the Else part of an If..Else statement never fires, it means the code isn't working.

Please go back to all the earlier replies and give clear, straightforward answers to the questions asked
 
I think what the OP wants is
Code:
If Me.cboOption.Column(0) = "1" Or Me.cboOption.Column(0) = "2" Then
  if Nz(Me.Example1, 0) < Nz(Me.Example2, 0) then
      Cancel = True
      MsgBox "You number must be greater than Example2.", vbInformation, "Invalid Entry:"
  endif
Else
  if Nz(Me.Example1, 0) > Nz(Me.Example2, 0) then
    Cancel = True
    MsgBox "You number must be less than Example2.", vbInformation, "Invalid 
  End If
End If

But then again .....
 
I think what the OP wants is
Code:
If Me.cboOption.Column(0) = "1" Or Me.cboOption.Column(0) = "2" Then
  if Nz(Me.Example1, 0) < Nz(Me.Example2, 0) then
      Cancel = True
      MsgBox "You number must be greater than Example2.", vbInformation, "Invalid Entry:"
  endif
Else
  if Nz(Me.Example1, 0) > Nz(Me.Example2, 0) then
    Cancel = True
    MsgBox "You number must be less than Example2.", vbInformation, "Invalid 
  End If
End If
But then again .....

That sounds reasonable. I hope so...
 
Last edited:
The cboOption has several plans called 1, 2, 3, 4, 5. Plan 1 and 2 number must be greater than Example2. The text boxes are just simple fields to compare any two given numbers between the text boxes to check if one is greater or less than the other depending on the plan you select. For example, you enter 4 in Example1 and 5 in Example2, then it returns a message "Your number must be greater than Example2." If you enter a number that meets criteria, there is no message and you can move on to filling out the rest of the form. The rest of plans will use the same concept but the number must be less than Example2.
 
Last edited:
The cboOption has several plans called 1, 2, 3, 4, 5. Plan 1 and 2 number must be greater than Example2. The text boxes are just simple field fields to compare two numbers between the text boxes. For example, you enter 4 in Example1 and 5 in Example2, then it returns a message "You number must be greater than Example2." If you enter a number that meets criteria, there is no message and you can move on to filling out the rest of the form. The rest of plans will use the same concept but the number must be less than Example2.
That explanation makes more sense than the other ones, and it sound like Cronk's suggestion earlier might be on the right track. Have you tried his suggested code?
 
Thank you Cronk, this resolved it :)
Hi. Glad to hear you finally got it sorted out. Good luck with your project.


PS. I think Cronk is a mind reader. :)
 

Users who are viewing this thread

Back
Top Bottom