Solved Not equal to filter in VBA function

JamieRhysEdwards

New member
Local time
Today, 15:04
Joined
Mar 26, 2022
Messages
27
Hi All,

I have a check box labelled "Show Retired" which when ticked, I want it to show assets labelled "Retired" and when it's unchecked, to hide them. I cannot quite figure out how this is done however.

This is what I've done so far:

Code:
Private Sub chkShowRetired_Click()
    If chkShowRetired.Value = True Then
        Me.FilterOn = False
    Else
        Me.Filter = "[AssetCondition] <> 'Retired'"
        Me.FilterOn = True
    End If
End Sub

If I apply the filter manually by tapping the down arrow on the column and then deselecting Retired, it works as expected. If I then check Show Retired, it removes the filter as expected.

However, if I then disable (uncheck) Show Retired, I then get a 3464 error which states it's a Data type mismatch in criteria expression.

If I then tap debug, it shows Me.FilterOn = False is the culprit?
 
you may try:
Code:
Private Sub chkShowRetired_Click()
    If chkShowRetired.Value = True Then
        Me.FilterOn = False
    Else
        Me.Filter = "Nz([AssetCondition], '@') <> 'Retired'"
        Me.FilterOn = True
    End If
End Sub
 
you may try:
Code:
Private Sub chkShowRetired_Click()
    If chkShowRetired.Value = True Then
        Me.FilterOn = False
    Else
        Me.Filter = "Nz([AssetCondition], '@') <> 'Retired'"
        Me.FilterOn = True
    End If
End Sub
This doesn't seem to do anything... It's fixed the mismatch error but nothing is actually applied :(
 
probably Moved your code to the AfterUpdate event of the checkbox, not the Click event.
 
@Eugene-LS Sorry for the very late reply. It's coming from a lookup table if this helps, so it can either be grabbed textually, or if preferred, an ID value of 4
 
@Eugene-LS Sorry for the very late reply. It's coming from a lookup table if this helps, so it can either be grabbed textually, or if preferred, an ID value of 4
No, it's not an either or choice. It's going to be the ID, or Primary Key value, that matters.
 
Try
Code:
Private Sub chkShowRetired_Click()
   If chkShowRetired.Value = True Then 
       Me.filter = "[AssetCondition] = 'Retired'"      ' <<<<Missing
       Me.FilterOn = True
  Else
      Me.Filter = "[AssetCondition] <> 'Retired'"
      Me.FilterOn = True
  End If
End Sub
 
Thanks for the support all! I think I've figured it out (might not be the most efficient way of doing it and I'll soon see). This is how I'm doing it at present:

Code:
Private Sub Form_Load()
    ...
    TempVars("AssetList_ShowRetired") = False
    ...
End Sub

Private Sub chkShowRetired_Click()
    If (Form!chkShowRetired) Then
        TempVars("AssetList_ShowRetired") = True
        ToggleFilters
    Else
        TempVars("AssetList_ShowRetired") = False
        ToggleFilters
    End If
End Sub

Private Function ToggleFilters()
    DoCmd.ShowAllRecords
    
    If (Not TempVars("AssetList_ShowRetired")) Then
        ' Filter out Retired assets if checkbox is cleared
        DoCmd.ApplyFilter "", Eval("""RetiredDate is null or RetiredDate > date()"""), ""
    End If
End Function
 

Users who are viewing this thread

Back
Top Bottom