0

I want to clarify VBA IF statement. What is the difference between them:

if x=true then
'do something at true
end if

if NOT x=false then
' do something at true
end if

Both statements will check same conditions. What is the difference among them.

To be more specific I am working on recordset object adodb and trying to find the difference between these and can't figure out

If Not objMyRecordset.EOF=True Then
' do something when objMyRecordset.EOF is not true (Implicitly means False)
End if

If objMyRecordset.EOF=False Then
' do something when objMyRecordset.EOF returns False (implicitly means not True)
End if

Both If's are checking same condition here too. But on numerous articles on internet I found the test condition with If Not objMyRecordset.EOF=True Then. There must be some real reason behind this. can someone explain?

Regards

Teamothy
  • 2,000
  • 3
  • 16
  • 26
sunnyimran
  • 3
  • 1
  • 4
  • 1
    `If Not False` equals `If True` and also `If Not True` equals `If False`. https://en.wikipedia.org/wiki/Boolean_algebra – SNR Oct 02 '19 at 08:37
  • 1
    No need for `True` and `False`. If you deal with a `Boolean`, simply write `If x Then` or `If Not x Then`. – FunThomas Oct 02 '19 at 08:59
  • You will see `If Not obj Is Nothing` testing if an object is instantiated because `Is Something` is not a keyword. We can't test if an object is something, we can only test if it is not nothing. In the case of dictionaries, you can test if a key exists with `If dict.Exists(key)` but this will create the key if it doesn't already exist; that does not happen when using `If Not dict.Exists(key)` – ProfoundlyOblivious Oct 02 '19 at 09:23
  • ProfoundlyOblivious, as far as I understood is sometimes we need to test if something exists but there is no keyword for that time's 'exists' so we check the opposite, negation of (if something not exists) will return required result. But what for the case where I have possibility to check both ways, like my question about recordsets. I can test both cases. I came across a lot like "If Not objMyRecordset.EOF=True" while it is working other way too "If objMyRecordset.EOF=False" as there any real benefit in such scenario? – sunnyimran Oct 02 '19 at 12:52
  • You should use is "If Not rs.EOF Then " - there's no need for a true or false because EOF is itself Boolean. The styles you are seeing is where people are copying this without understanding and adding a True or false. Strictly speaking the routine "If rs.EOF Then 'do nothing ELSE 'do something " is slightly more efficient then "If Not rs.EOF then 'do something" because the Not requires an extra clock cycle - but that's less important these days than it once was. – Harassed Dad Oct 02 '19 at 15:01
  • Sunny, no "real benefit" exists between `Is` vs `Not Is` [read here](https://stackoverflow.com/questions/3818933/is-ifvar-true-faster-than-ifvar-false) and the speed of `If` [might surprise you](https://stackoverflow.com/questions/315306/is-if-expensive) Dropping the equivalence test (don't use `If True = True`, use `If True`) and using `False` instead `Not True` may be faster **but** a well named variable in a context that makes your code more enjoyable to read and easier to understand is far more important than an imperceivable speed difference. If nanoseconds matter, don't use VBA. – ProfoundlyOblivious Oct 05 '19 at 16:41
  • "ProfoundlyOblivious" and "Harassed Dad" your comments satisfies me. No idea how to mark my question as answered. – sunnyimran Oct 14 '19 at 18:06

0 Answers0