Results 1 to 4 of 4
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Can someone explain to me why this IF statement is not failing?

    I felt like this image explains it faster but below I have text.
    Click image for larger version. 

Name:	Untitled.jpg 
Views:	14 
Size:	19.3 KB 
ID:	49455

    Basically the code is
    Code:
    Dim Boo1, Boo2, Boo3, Boo4, Boo5 As Boolean
    
    Boo1 = False
    Boo2 = False
    Boo3 = False
    Boo4 = False
    
    Boo5 = True
    
    If Boo1 Or Boo2 Or Boo3 Or Boo4 And Not Boo5 then
    Do something
    End If
    So it shouldn't be activating because Boo5 is true which should be cancelled out by the Not. At least that is my understanding and yet it still activates.
    I can easily work around it by using a nested If statement but I want to know why it doesn't work.

    Thanks in advance!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    In your declaration line the only variable declared as Boolean is the last one, the others are Variant. VBA requires each variable type to be declared individually or it defaults to Variant.

    Dim Boo1 As Boolean, Boo2 As Boolean, Boo3 As Boolean, Boo4 As Boolean, Boo5 As Boolean

    You are mixing AND and OR operators. Might need to use parentheses to control the order of operations (remember Algebra lessons).

    If (Boo1 Or Boo2 Or Boo3 Or Boo4) And Not Boo5 Then
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by June7 View Post
    In your declaration line the only variable declared as Boolean is the last one, the others are Variant. VBA requires each variable type to be declared individually or it defaults to Variant.

    Dim Boo1 As Boolean, Boo2 As Boolean, Boo3 As Boolean, Boo4 As Boolean, Boo5 As Boolean

    You are mixing AND and OR operators. Probably need to use parentheses to control the order of operations (remember Algebra lessons).

    If (Boo1 Or Boo2 Or Boo3 Or Boo4) And Not Boo5 Then
    REALLY?! I have been under the impression it just took whatever the last one was defined as!!!!! Ugh I have to go change a ton of variables. Thanks very much!

    Also thanks on the parentheses

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I tested your code. Unaltered, it does not 'activate' - it does not enter the If Then. Instead of Do Something, use Debug.Print "test". So what is different about your real code? What is the value of each of the variables?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Why is it so hard to explain VBA?
    By DigitalAdrenaline in forum Access
    Replies: 1
    Last Post: 08-20-2016, 06:04 AM
  2. Replies: 3
    Last Post: 01-22-2014, 11:31 PM
  3. Please explain this formula
    By Johin.b in forum Access
    Replies: 2
    Last Post: 07-09-2013, 11:33 AM
  4. plz explain how to use iif in msaccess
    By pabbiraj in forum Access
    Replies: 2
    Last Post: 05-22-2012, 01:39 PM
  5. Can someone explain to me why this is not working
    By Darkladymelz in forum Programming
    Replies: 6
    Last Post: 03-19-2012, 05:55 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums