Results 1 to 5 of 5
  1. #1
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117

    Alternate methods to filter results and which is better ??

    On the path to convert all my macros over to VBA since getting help on macros is painful and have come up with a couple different ways to filter my results when opening the next form.



    I used various examples found online and also looked at the code crated when I converted macros to code on a copy of my database. So I have two ways to filter my results:

    One
    Code:
        DoCmd.OpenForm "F_Log_Apiary_Main", acNormal, "", , , acNormal
        DoCmd.ApplyFilter , "Active = -1 And [Log_Apiary_ID] > 1"
    and Two
    Code:
        DoCmd.OpenForm "F_Log_Apiary_Main", acNormal, "Active = -1 And [Log_Apiary_ID] > 1", , , acNormal
    Is one better than the other or one preferred over the other and if so, why ?

    Is there a better way than either of these methods ?


    Just asking so I can gain a better understanding as I create additional code with more efficiency.

    Thanks.....

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The end results of both versions are the same.

    Why have two lines of code executing two commands when one will serve?

    Use OpenForm and its argument to apply filter when opening another form.

    Use ApplyFilter when desire is to filter same form code is behind.

    Note, your second version is using wrong argument to pass filter criteria.
    And what is purpose of second acNormal?

    Code:
    DoCmd.OpenForm "F_Log_Apiary_Main", acNormal, , "Active = -1 And [Log_Apiary_ID] > 1"
    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
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    IMO they are virtually the same thing, esp. if they both provide the desired result. Those 2 methods are not the only ways either. One can open a form then set the filter property, then turn the filter on. That may be more useful if you wish to maintain a filter but cycle it on and off. Even then, you can do this from the calling form or the called form. You can also set the filter property in the property sheet if that ever makes sense.

    I wouldn't worry too much about maximizing efficiency just yet. What you might consider is to create and post for comments related to efficiency. One tip I can pass on at this time would be that certain methods have defaults and you don't need to write them out (based on you post you can drop all those parameters and it shouldn't matter). I can recommend this site for guidance, but sometimes the problem is that you need a modicum of knowledge to be able to find what you're looking for.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    ScubaBart is offline Competent Performer
    Windows 7 32bit Access 2007
    Join Date
    Jul 2024
    Posts
    117
    Quote Originally Posted by June7 View Post

    Note, your second version is using wrong argument to pass filter criteria.
    And what is purpose of second acNormal?

    Code:
    DoCmd.OpenForm "F_Log_Apiary_Main", acNormal, , "Active = -1 And [Log_Apiary_ID] > 1"
    This is probably where I used the format from where I converted macros to VBA. It seems to put in a lot of stuff that apparently isn't really required. I had wondered why it was there but didn't mess with it not knowing any better.


    I will also research the reference that Micron linked to. Thanks.

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    I had the same thought as micron regarding the defaults for various arguments in a method.
    I'm sure you've noticed Intellisense when your using the VBA editor.

    You'll notice below that FormName is a required argument.(no brackets)
    The rest of the arguments have brackets [ ] around them which indicates they are optional.
    Click image for larger version. 

Name:	00Optional.jpg 
Views:	18 
Size:	22.5 KB 
ID:	52177


    Those optional arguments that have an "= something" are the default values for those arguments.
    Intellisense will show you the other possible values as you move through the arguments.

    Click image for larger version. 

Name:	000Opt2.jpg 
Views:	18 
Size:	35.5 KB 
ID:	52179
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Filter top N results by field
    By Secureuser.cm in forum Queries
    Replies: 17
    Last Post: 08-14-2015, 04:18 PM
  2. Using a query to filter - too many results
    By pbouk in forum Queries
    Replies: 9
    Last Post: 03-14-2013, 11:26 AM
  3. Replies: 1
    Last Post: 08-16-2012, 01:51 PM
  4. Look Up Tables and Filter Results
    By starkeymd in forum Access
    Replies: 1
    Last Post: 01-12-2012, 04:17 PM
  5. cbo to filter results from a query
    By nianko in forum Forms
    Replies: 5
    Last Post: 08-18-2010, 09:43 AM

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