Results 1 to 13 of 13
  1. #1
    gpierce9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Location
    NJ
    Posts
    19

    Findng Dlookup in my queries

    12/27/18 Is there a way to find all the queries or forms or reports which might contain a Dlookup?


    i have been opening each query but sometimes miss a spot.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you should not have ANY Dlookups in queries. They ARE the Dlookup, by joining tables for lookups.

    Try not to use them in reports....everything should be in the query.
    Forms can use them for some things.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Probably not without writing VBA code to accomplish. Can get third party add-in that can do this kind of search. I have used Rick Fisher's Find and Replace which costs about $50. I have heard about freebie V-Tools.
    Last edited by June7; 12-27-2018 at 12:42 PM.
    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.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I suppose you could write a procedure that one by one, loads the sql of each query by way of accessing the query def object, then use the Instr function on the sql.
    Or you could use the database documenter, choose only queries, choose only sql and generate a report that shows the sql for each query as long as you don't mind reading it all.
    EDIT
    I guess you could also output that report to Word (it is an option) and use Find in Word.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    gpierce9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Location
    NJ
    Posts
    19
    Quote Originally Posted by ranman256 View Post
    you should not have ANY Dlookups in queries. They ARE the Dlookup, by joining tables for lookups.

    Try not to use them in reports....everything should be in the query.
    Forms can use them for some things.
    12/27 I agree with you. That is why I am removing all my dlookups. I figured out a way to make a query one gather data
    and then query two is able to join. Runs faster too without the dlookup in the Lan.

    I'm looking for a way to find where we placed the dlookups. right now, i open each qry , form and report to see.
    but i miss some. glen

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    findinsql "DLookup"

    Code:
    Function FindInSql(strFind As String) As String
    Dim qdef As DAO.QueryDef
    
    For Each qdef In CurrentDb.QueryDefs
      If InStr(qdef.sql, strFind) Then FindInSql = FindInSql & qdef.Name & vbCrLf
    Next
    
    If Len(FindInSql & vbNullString) > 0 Then
      'switch comment/uncomment next 2 lines to report via message box or printout to immediate window
      'MsgBox FindInSql
      Debug.Print Replace(FindInSql, vbCrLf, "")
    Else
      MsgBox "Search term not found."
    End If
    
    Set qdef = Nothing
    
    End Function
    Depending on your version, you might need a reference to DAO if not already set.
    EDIT: you could easily adapt the above to search form or report controls. A more useful tool might be a form with checkboxes to select the type of objects to look at, then iterate through those selected.

  7. #7
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551

    Inspect Queries

    gpierce9, if you like, you can use the functionality of this code:

    Code:
    Option Compare Database
    Option Explicit
    
    Private Const cstrQName As String = "qryInspect"
    Private Const cstrTName As String = "tblMyQueryDefs"
    
    Sub InspectQueries()
        Dim qd As DAO.QueryDef
        Dim rs As Recordset
    
        With CurrentDb
            On Error Resume Next
            .Execute "CREATE TABLE " & cstrTName _
                         & " (qName TEXT(50) PRIMARY KEY, qSQL MEMO);"
            .QueryDefs.Delete cstrQName
            On Error GoTo 0
            .Execute "DELETE * FROM " & cstrTName & ";"
            Set rs = CurrentDb.OpenRecordset(cstrTName)
        End With
    
        With rs
            For Each qd In CurrentDb.QueryDefs
                .AddNew
                !qName = qd.Name
                !qSQL = qd.SQL
                .Update
            Next qd
            .Close
        End With
        Set rs = Nothing
    
        With CurrentDb.QueryDefs
            On Error Resume Next
            .Delete cstrQName
            On Error GoTo 0
            .Refresh
            Set qd = New DAO.QueryDef
            qd.SQL = "SELECT qName, " _
                     & "InStr(1,[qSQL],'dlookup',1)>0 AS DLookUps, " _
                     & "InStr(1,[qSQL],'dsum',1)>0 AS DSums, " _
                     & "InStr(1,[qSQL],'dcount',1)>0 AS DCounts, " _
                     & "InStr(1,[qSQL],'dmax',1)>0 AS DMaxes, " _
                     & "InStr(1,[qSQL],'dmin',1)>0 AS DMins, " _
                     & "InStr(1,[qSQL],'davg',1)>0 AS DAvgs " _
                     & "FROM " & cstrTName & ";"
            qd.Name = cstrQName
            .Append qd
            .Refresh
            DoCmd.OpenQuery cstrQName
        End With
    End Sub
    Paste it in a standard module and run the subroutine.

    It creates a table that keeps the names of queries and their sql definitions, fills the table with data and creates a query that looks for some domain aggregate functions in separate fields that you can filter as you want.

    I hope it helps.

  8. #8
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Is there a way to find all the queries or forms or reports which might contain a Dlookup?
    Just realized that this isn't 100% clear. When it comes to reports and forms, where is it that you want to look? Underlying queries only, or calculated controls as well? I was assuming the latter. Why specify forms and reports if only looking at their underlying queries? That would be the same as looking at all the queries only. To search form and report controls would be much more intensive, which is why I mentioned adapting the code I wrote. Then again, even that might be insufficient because you could have all sorts of vba constructed sql with lookups in it.

  9. #9
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    accesstos, Nice little routine.
    I had to modify the code a bit to run on a DB with 80 or so queries. The existing code was including some system internal queries the the mod eliminates:
    Code:
        With rs        
            For Each qd In CurrentDb.QueryDefs
                'Debug.Print qd.Name
                If Left(qd.Name, 4) <> "~sq_" Then
                    .AddNew
                    !qName = qd.Name
                    !qSQL = qd.SQL
                    .Update
                End If
            Next qd
            .Close
        End With
    Last edited by davegri; 12-31-2018 at 09:53 AM. Reason: format

  10. #10
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    I've just tried the routine and am also impressed by it. Thank you

    Just for information, database objects starting with "~sq_" are the record sources to form/reports and control sources to combos/listboxes etc.
    Access stores these as 'temporary queries' in the hidden system table MSysObjects

    NOTE: ~sq_f = form ; ~sq_c = form control ; ~sq_r = report ; ~sq_d = report control

    So if you want to search all form/report controls you need to use the code as originally posted by accesstos
    But if you only want to search saved queries, modify the code as suggested by davegri

    EDIT: Suggest one more code change.
    The maximum number of characters in an Access object name is 64 - see https://support.office.com/en-us/art...8-98c1025bb47c

    So to avoid possible errors, I suggest
    Code:
    ...
     .Execute "CREATE TABLE " & cstrTName _
                         & " (qName TEXT(65) PRIMARY KEY, qSQL MEMO);"
    ...
    Last edited by isladogs; 12-31-2018 at 11:47 AM. Reason: More info
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  11. #11
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    The existing code was including some system internal queries the the mod eliminates:
    You're right davegri! In this case, it is useless to inspect the hidden queries.
    An other safer way to skip all hidden queries is to use the GetHiddenAttribute method of Application object, as shown below:

    Code:
    If Not GetHiddenAttribute(acQuery, qd.Name) Then
        'Is not hidden
        '....
    End If
    ridders52, very useful informations! Your suggestions makes code more robust!
    Thank's a lot!
    Last edited by accesstos; 01-01-2019 at 03:53 AM. Reason: spelling

  12. #12
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    An other safer way to skip all hidden queries is to use the GetHiddenAttribute method of Application object
    That will also ignore any 'standard' but hidden queries which may not be what the OP wants
    And as I wrote earlier, if the OP wants to include form & report controls in their check, the original code would work perfectly

    I just ran this code on a very large database including the ~sq_ items - it took around 4 seconds to scan & tabulate the SQL for 3427 queries & 'temp' queries - which is in my view pretty impressive
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  13. #13
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    I fully agree with you. OP has several methods to choose and I think OP is covered.

    At a philosophical level, time and space have no objective substance. However, in our object oriented world, we must save them. ;-)

    Thank you for your kind words and happy new year!

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

Similar Threads

  1. Replies: 5
    Last Post: 06-05-2016, 09:23 PM
  2. Shortening if / dlookup >0 / then dlookup
    By Jack Russel in forum Modules
    Replies: 1
    Last Post: 02-19-2016, 03:01 PM
  3. queries based on union queries
    By vicsaccess in forum Queries
    Replies: 3
    Last Post: 10-10-2015, 07:53 PM
  4. DLookup in Queries
    By Garry in forum Access
    Replies: 5
    Last Post: 09-16-2014, 06:30 AM
  5. Replies: 9
    Last Post: 08-27-2014, 11:34 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