Results 1 to 6 of 6
  1. #1
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37

    filters in continuos form using VBA code

    Hello,
    I would like to ask you for advice with filtering my data in continuous form, using VBA code.
    I have a list of data collected from several tables:

    Click image for larger version. 

Name:	Capture.jpg 
Views:	14 
Size:	84.5 KB 
ID:	26968

    At the top of the list there are fields used for filtering. I have set "AfterUpdate" action in VBA for every filter filed to set filter:

    Code:
    DoCmd.ApplyFilter , "Done = FALSE And JdeWo LIKE '*" & fWO & "*' AND AsmCase LIKE '*" & Fcase & "*' AND tblProformaType.Type LIKE '*" & fProf & "*' AND tblOrderType.Type LIKE '*" & fOrder & "*' AND tblPartsType.Type LIKE '*" & fParts & "*' AND tblPlanningType.Type LIKE '*" & fPlan & "*' AND tblWarranty.Type LIKE '*" & fWarr & "*' AND Customer LIKE '*" & fCust & "*' "
    Filtering is working pretty well, but only if all filtered columns are filled in correctly. If some of filtered field is empty, whole record is excluded from filter.

    Here is an example:
    1) no filter selected:
    Click image for larger version. 

Name:	Capture1.PNG 
Views:	14 
Size:	23.3 KB 
ID:	26969
    - two records for customer _dev_test are shown (one of them has all fields filled in, second record has no data filled in field AsmCase

    2) filter for column Customer is applied:
    Click image for larger version. 

Name:	Capture2.PNG 
Views:	14 
Size:	17.2 KB 
ID:	26970
    - only one record is selected (record with all fields filled in).


    Can you please advice me, how I can modify my VBA code to filter data including empty columns?

    Thank you very much

    Regards TriFith

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    Check every cbo box and then build the filter....
    Code:
    sub cboBox1_afterupdate()
      FilterData
    end sub
    
    sub cboBox2_afterupdate()
      FilterData
    end sub
    
    sub FilterData()
    Dim sWhere as string
    
    if cboBox1<>"*" then sWhere = sWhere & " and [field1]='" & cboBox1 & "'"
    if cboBox2<>"*" then sWhere = sWhere & " and [field2]='" & cboBox2 & "'"
    
    if sWhere ="" then
        Me.filterOn= false
     else
        'Remove 1st AND
        SWhere = mid(sWhere,5)
        Me.filter = sWhere
        Me.filterOn= true
    end if
    end sub

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    you can try using the nz function

    ....AND nz(tblProformaType.Type) LIKE '*" & fProf & "*' AND....

    on a separate note, Type is a reserved word, using it as a field name will cause you a problem at some point

  4. #4
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37
    Quote Originally Posted by ranman256 View Post
    Check every cbo box and then build the filter....
    Code:
    sub cboBox1_afterupdate()
      FilterData
    end sub
    
    sub cboBox2_afterupdate()
      FilterData
    end sub
    
    sub FilterData()
    Dim sWhere as string
    
    if cboBox1<>"*" then sWhere = sWhere & " and [field1]='" & cboBox1 & "'"
    if cboBox2<>"*" then sWhere = sWhere & " and [field2]='" & cboBox2 & "'"
    
    if sWhere ="" then
        Me.filterOn= false
     else
        'Remove 1st AND
        SWhere = mid(sWhere,5)
        Me.filter = sWhere
        Me.filterOn= true
    end if
    end sub

    Thank you for answer.
    I am not sure if I understand it correctly, but code what you wrote will avoid filtering with empty comboboxes...??? Is that correct?
    Because that is not my problem... My problem is in filtered data.


    I have an idea how to solve this, but I still canīt find way how to set filter for empty fields...
    For example If I have columnnamed AsmCase and I would like to filter all records with no value in this column... What should be the syntax of this VBA filter command??

  5. #5
    TriFith is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Aug 2013
    Location
    Czech republic, Prague
    Posts
    37
    Och...
    I have finally find out how to solve this...
    When I understood syntax of handling with NULL value, solution was simple

    Code:
    Me.Filter = " Customer LIKE '*" & fCust & "*' AND (([AsmCase] Is Null) OR (AsmCase LIKE '*" & Fcase & "*'))........"

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    happy to help!

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

Similar Threads

  1. Replies: 1
    Last Post: 03-27-2015, 09:31 PM
  2. Sum textbox in continuos form
    By cuongmyh in forum Forms
    Replies: 2
    Last Post: 02-27-2015, 10:12 AM
  3. Replies: 1
    Last Post: 06-21-2013, 10:41 AM
  4. Filters in one form affecting another...
    By Dominaz in forum Access
    Replies: 1
    Last Post: 10-25-2011, 04:55 PM
  5. Form Filters Help
    By JeffG3209 in forum Forms
    Replies: 1
    Last Post: 05-22-2011, 10:31 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