Results 1 to 4 of 4
  1. #1
    PeakH is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    143

    Custom Function: how to add Where Clause

    I have a function that looks up if a query has any records. I use this function in an onClick event so that if there are records it will open a report, and if not it will open a form to add records. This worked great when I had criteria in the query itself, but that created some problems so I got rid of the query criteria and put that criteria in the onClick event. Now the query always has records, so it will always open the report even if the report has no records with the specific criteria.



    I think a way for me to fix this is to add a WhereCondition into the function itself. The problem being that I have no idea how to do this.
    I tried looking up the VBA code for the OpenForm/Report functions, since they have where conditions, thinking I could copy some code from it, but I have had no luck.


    Code:
    Function fcnRptHasRecs(qQuery As String) As Boolean
    'Checks if there are any records
        Dim r As DAO.Recordset
    
    
        fcnRptHasRecs = False
        
        Set r = CurrentDb.OpenRecordset(qQuery)
        If Not r.BOF And Not r.EOF Then
            r.MoveLast
            r.MoveFirst
            fcnRptHasRecs = (r.RecordCount > 0)
        End If
        r.Close
    
    
        'clean up
        Set r = Nothing
    End Function

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Something like:

    Set r = CurrentDb.OpenRecordset("SELECT * FROM " & qQuery & " WHERE YourCriteriaHere")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    PeakH is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    143
    This is perfect!

    Thank you so much!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Use custom function in Expression Builder
    By MattLewis in forum Programming
    Replies: 3
    Last Post: 01-17-2018, 10:54 PM
  2. Agregation function - where clause
    By Khalil Handal in forum Access
    Replies: 3
    Last Post: 05-18-2017, 07:22 AM
  3. Now Function Custom Format
    By DrJohn in forum Access
    Replies: 9
    Last Post: 03-16-2016, 09:51 AM
  4. Trouble with string variable in custom vba function
    By CurrentUser in forum Programming
    Replies: 4
    Last Post: 01-10-2014, 09:13 PM
  5. Replies: 0
    Last Post: 03-17-2011, 09:57 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