Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16

    If Statement Ignores Boolean Values in Access VBA


    I have a form with several controls that I'm using to filter its records. To make it simple, let's say the controls are cboCat, chkA, chkB and cmdSearch. In reality there are several more combos and everything worked perfectly until I added the check boxes.

    This is the simplified code I've got:

    In the form's module:
    Code:
    Private Sub cmdSearch_Click()
      Dim lngCatID As Long, blnA As Boolean, blnB As Boolean
    
      If (Me!cboCat = 0) And (Me!chkA = False) And (Me!chkB = False) Then
        MsgBox "Please select something to search by."
      Else
        lngCatID = Nz(Me!cboCat)
        blnA = Me!chkA, 
        blnB = Me!chkB
        
        strFilter = GetFilterString(lngCatID, blnA, blnB)
    
        Me.Filter = strFilter
        Me.FilterOn = True
      End If
    End Sub
    In a standard module:
    Code:
    Public Function GetFilterString(ByVal lngCatID As Long, _
                                    ByVal blnA As Boolean, _
                                    ByVal blnB As Boolean) As Variant
      Dim strFilter As String
    
    MsgBox blnA & " " & blnB
    
      If (lngCatID > 0) And (blnA = False) And (blnB = False) Then     'Case 1
        strFilter = "CatID = " & lngCatID
      ElseIf (lngCatID = 0) And (blnA = True) And (blnB = False) Then  'Case 2
        strFilter = "A = " & blnA
      ElseIf (lngCatID = 0) And (blnA = False) And (blnB = True) Then  'Case 3
        strFilter = "B = " & blnB
      End If
    
    MsgBox strFilter
        
      GetFilterString = strFilter
    End Function
    In all three cases, the first MsgBox in the function gives the correct values and the second MsgBox shows the correct filter string in Case 1. But in Cases 2 & 3, nothing appears in the second MsgBox and no filtering happens on the form. I've also tried the "...And blnA And Not blnB..." style with the same result. What am I doing wrong?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I tested your function by calling it from Immediate Window with inputs that should return appropriate message. It works for all 3 cases.

    When would lngCatID be null?

    Fields are named "A" and "B"?

    Could provide db for analysis. Follow instructions at bottom of my post.
    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
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16
    Thanks for responding so quickly. I forgot to say that these are all unbound controls. I set the default value for cboCat (and all the other combos) to 0. lngCatID would only have a different value if something had been selected. No, there are no fields named A and B; I was just simplifying my code for this post.

    Like I said, this all worked perfectly before I tried including the checkboxes in the code. And it still does as long as I don't choose one of those. There must something wrong with the way I'm checking their values in the if statement.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Maybe I've missed something, but if just one of these tests are not True,

    If (Me!cboCat = 0) And (Me!chkA = False) And (Me!chkB = False)
    MsgBox "Please select something to search by."

    Then the Else portion is evaluated:
    Else
    lngCatID = Nz(Me!cboCat)
    blnA = Me!chkA,
    blnB = Me!chkB

    Yet chkA or chkB may be True, False or Null?

    Perhaps you need to step through the code after making various selections and check if the variables/references evaluate to what you expect, or if the code flow is as you expect.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16
    Quote Originally Posted by Micron View Post
    Yet chkA or chkB may be True, False or Null?
    Well, not exactly. The checkboxes currently have a default value of 0, so it should never be Null, should it? I think I originally set the defaults to False, but I'm not sure what the correct value should be. I still get confused when it comes to Null, 0, "", etc. ;-(

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Checkbox not bound to Yes/No field can be 0, -1, Null unless TripleState property is set to No. Setting DefaultValue property will populate with a value but user could select Null state unless TripleState property is set to No. Even with set as No, unbound checkbox will have Null if there is no DefaultValue when form first opens.
    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.

  7. #7
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16
    Quote Originally Posted by Micron View Post
    Maybe I've missed something, but if just one of these tests are not True,

    If (Me!cboCat = 0) And (Me!chkA = False) And (Me!chkB = False)
    MsgBox "Please select something to search by."

    Then the Else portion is evaluated:
    That part works correctly.

    Perhaps you need to step through the code after making various selections and check if the variables/references evaluate to what you expect, or if the code flow is as you expect.
    That's what the MsgBoxes are there for.

  8. #8
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16
    Quote Originally Posted by June7 View Post
    Checkbox not bound to Yes/No field can be 0, -1, Null unless TripleState property is set to No. Setting DefaultValue property will populate with a value but user could select Null state unless TripleState property is set to No. Even with set as No, unbound checkbox will have Null if there is no DefaultValue when form first opens.
    TripleState is set to No. Default value is currently 0, although I originally set it to False.

  9. #9
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    That's what the MsgBoxes are there for.
    You've verified the values of the checkboxes, but not the combobox.
    Perhaps the combobox has more than one column; the default is cboCat.column(0)

  10. #10
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16
    Quote Originally Posted by davegri View Post
    You've verified the values of the checkboxes, but not the combobox.
    The combo has always been correct, so I didn't bother verifying it.

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    That's what the MsgBoxes are there for.
    Not from what I see. If the line I'm referring to does not evaluate to True, then what does blnA =Me!chkA evaluate to? The msgbox line does not reveal that to us.
    BTW, you might want to check out the difference between using bang (!) vs dot (.)
    ! will not reveal compile errors but will reveal run time errors (when it is perhaps too late). I never use it unless I'm writing code for recordsets, which seems to be the only time it's necessary.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  12. #12
    gr8dane is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2024
    Posts
    16
    Quote Originally Posted by Micron View Post
    If the line I'm referring to does not evaluate to True, then what does blnA =Me!chkA evaluate to?
    I'm assuming it evaluates to either True or False, depending on whether or not it's been selected (the first MsgBox in the function confirms that). If that's not the case, please explain.

    BTW, you might want to check out the difference between using bang (!) vs dot (.)
    I've done a lot of searching on that topic, and everyone gives a different answer. I've settled on putting a bang between a form and a control on it and a dot between a control and a property. Earlier today I accidentally put a dot where I usually put a bang and the line went red until I changed it, so I must be on the right track there.

  13. #13
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    The combo has always been correct, so I didn't bother verifying it.
    Ah, but saying so doesn't make it so.

    Your symptoms are exactly as described If cboCat is > 0 for Case 2 or Case 3

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by gr8dane View Post
    That part works correctly.


    That's what the MsgBoxes are there for.
    Well they are not solvinjg your problem though, are they?

    A simple walk of the code would find the issue in a heartbeat, but you seem reluctant to do so.
    As June7 mentioned it works for her, so the values are not what you expect.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Earlier today I accidentally put a dot where I usually put a bang and the line went red until I changed it, so I must be on the right track there.
    I don't think that is reliable. Try this: choose a procedure that refers to a form control. Make sure your code is compiled and saved. Edit the form control so that it is wrong (e.g. Me!txtCustomer changed to Me!txtCust) and compile. There should not be an issue. Now change it to Me.txtCust and compile and you'll raise an error. In the first case, you will not raise an error until that line of code executes (if ever). In the second case, you won't be able to complete a project until you fix the error, which is better than releasing faulty code I'd say.

    I don't know what you read on this subject but I've read lots of stuff where people don't know what they're talking about so I can relate to what you said.
    Simply put, the bang operator provides late bound access to the default property of the thing that follows it. Being late bound means it is not evaluated until the code line is executed.
    The checkboxes currently have a default value of 0, so it should never be Null,
    You said that the checkboxes are unbound. IIRC, unbound controls will ignore default value property settings. Even with default values, when the form is opened are these controls not grey (not selected, and not white)? If they are, their values are Null, not whatever your default property value is.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. numeric data type and Boolean values
    By rdougherty in forum Access
    Replies: 5
    Last Post: 04-06-2018, 02:23 PM
  2. Dealing with Duplicates and Boolean Values
    By EcologyHelp in forum Access
    Replies: 6
    Last Post: 11-17-2015, 12:49 PM
  3. Combining two boolean values
    By John_B in forum Access
    Replies: 6
    Last Post: 02-11-2012, 11:45 AM
  4. Unique Values and Boolean Fields
    By Triad in forum Forms
    Replies: 1
    Last Post: 07-15-2010, 06:28 PM
  5. How to query boolean values from table
    By kevdmiller in forum Queries
    Replies: 2
    Last Post: 11-30-2006, 07:41 PM

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