Results 1 to 7 of 7

Thread: Tables used in query

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    68

    Tables used in query

    Is there a way to find out all the tables used in a saved access query? Currently I do something like:

    Code:
    Dim qry As DAO.QueryDefs
    
    Set qry = db.QueryDefs
    
    For i = 0 To qry.Count - 1
            tvDBObjects.Nodes.Add "QUERIES", tvwChild, qry(i).Name, qry(i).Name
            For j = 0 To qry(i).Fields.Count - 1
                If PrevTable <> qry(i).Fields(j).SourceTable Then
                    tvDBObjects.Nodes.Add qry(i).Name, tvwChild, , qry(i).Fields(j).SourceTable
                    PrevTable = qry(i).Fields(j).SourceTable
                    DoEvents
                End If
            Next
            DoEvents
        Next
    But this doesn't always work. ie I have a make table query and it didn't return any SourceTables for it. Any help would be great thanks.

    -Rick

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The .SourceTable property will return the first level of tables/queries
    for your tabledef object. Now if you have a form that has a
    recordsource of only a table, you will return a querydef of "~sq_f" + the form name.

    I take it that your are trying to map a series of nested queries
    and place them into a treeview control?

    Try this...

    VB Code:
    1. Option Explicit
    2. 'Add reference to DAO
    3. Private Sub Command1_Click()
    4.  
    5.     Dim qry As DAO.QueryDefs
    6.     Dim db As DAO.Database
    7.     Dim i As Integer
    8.    
    9.     Set db = DAO.OpenDatabase("D:\RobDog888.mdb")
    10.     Set qry = db.QueryDefs
    11.    
    12.     For i = 0 To qry.Count - 1
    13.         If InStr(1, qry(i).Name, "~sq_f") = 0 Then  'Actual queries only
    14.             Debug.Print qry(i).Name; qry(i).Type
    15.             Debug.Print qry(i).Properties(20).Value 'Parse this property to get the tables
    16.             DoEvents                                'Actual queries do not have a fields collection,
    17.                                                     'only form recordsources do. fields = textboxes
    18.                                                     'on form, so to speak.
    19.                                                     'Prop 20 is the DOL prop.
    20.         End If
    21.     Next
    22.  
    23. End Sub
    24.  
    25. 'Output from my db below line - Tables: tblTest, Table1. Queries: Query1, Query2
    26. '-------------------------------------------------------------------------------------------
    27. 'Query1 0
    28. '??  ????????  ????    [color=red]Table1[/color]   ????????  ????    [color=red]tblTest[/color]   ????????....truncated because it is field data after this
    29. 'Query2 0
    30. '??  ????????  ????    [color=red]Query1[/color]   ????????  ????    [color=red]Table1[/color]   ????????....truncated because it is field data after this
    HTH
    Last edited by RobDog888; Nov 29th, 2004 at 05:13 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    I take it that your are trying to map a series of nested queries
    and place them into a treeview control?
    Kindof. I want to be able to access which tables are used within each query. Then Which queries are used within each Report, and what is all used in a Macro. We are trying to break some macros out of 1 DB and put in it's own DB. The macro is large and I don't want to manually find all the information needed by this macro. So I am trying to create something where you can just click the macro, and do an export to another db and it will copy everything that is needed over to the new DB.

    Properties(20).Value gives me an item not found in this collection.
    Properties(17).Values gives me:
    Code:
    ??  ????????  ???????tblNEWCUSTOM
    Which seems to be right. Why is yours different? I'm using Access 2000 SP-3. What is that above anyway? Why all the ?'s?



    [EDIT]

    Plus some have 0 instead of string.
    Plus it seems it goes table name, columns used, table name, columns used, etc.. Parsing out the SQL statement would be easier. I would just think MS would give an easy way to see which tables are in the query, seeing how int Access it shows them graphically.
    Last edited by RickP; Nov 29th, 2004 at 05:40 PM.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Yes, I'm using 2003. Could be the difference, but your get the error
    when you dont filter out the forms. The "?"s are access specific
    dataidentifiers. Something like a GUID pointer to the actual query
    object. so if yuo parse out the table/query names until the
    identifer which specifies that fields in the query, you can have a
    list of all the tables that the query contains at the first level only.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2004
    Posts
    68
    Does this work for all query types? Make table, update, etc... ? That seems to be where I get the 0, cause I am filtering out.

    so if yuo parse out the table/query names until the
    identifer which specifies that fields in the query, you can have a
    list of all the tables that the query contains at the first level only.
    I guess I don't follow you on this. Since it returns tablename, then the columns used from that table, then more tablenames, the only way I can make sure I'm getting a table and not a column is by checking each string against the current tables (doesn't seem very effecient). Plus when you say first level what do you mean? Do you mean if a query called a query? Cause that would show (like your example), and my logic would include that query to export and that query would then be looked at. Major point now is how would you parse that string? Thanks for your help on this btw.

    -Rick

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    In mine, it shows all tables first then all fields.
    Here is an update that is a little better.

    VB Code:
    1. Option Explicit
    2. 'Add reference to DAO
    3. Private Sub Command1_Click()
    4.  
    5.     Dim qry As DAO.QueryDefs
    6.     Dim db As DAO.Database
    7.     Dim i As Integer
    8.    
    9.     Set db = DAO.OpenDatabase("D:\RobDog888.mdb")
    10.     Set qry = db.QueryDefs
    11.    
    12.     For i = 0 To qry.Count - 1
    13.         If InStr(1, qry(i).Name, "~sq_f") = 0 Then  'Actual queries only
    14.             Debug.Print qry(i).Name; qry(i).Type
    15.             Debug.Print qry(i).Properties(qry(i).Properties.Count - 1).Value 'Parse this property to get the tables
    16.             DoEvents                                'Actual queries do not have a fields collection,
    17.                                                     'only form recordsources do. fields = textboxes
    18.                                                     'on form, so to speak.
    19.                                                     'Prop 20 is the DOL prop.
    20.         End If
    21.     Next
    22.  
    23. End Sub
    24.  
    25.  
    26. 'Output:
    27. 'qryMakeTableQry 80
    28. '??  ????????  ????    Table1   ???????? ????????Test2   ???????? ????????gfhs                   
    29. 'Query1 0
    30. '??  ????????  ????    Table1   ????????  ????    tblTest   ???????? ????????Test2   ???????? ????????Field1   ???????? ????????Field2   ???????? ????????Field3   ???????? ????????Field4   ???????? ????????gfhs                   
    31. 'Query2 0
    32. '??  ????????  ????    Query1   ????????  ????    Table1   ???????? ????????Test2   ???????? ????????Field1   ???????? ????????Field2   ???????? ????????Field3   ???????? ????????Test2           
    The first  on mine is designating the end of the tables/queries
    in the query. Then the rest are fields.

    I also made a make-table query and it worked too.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Ok, replaced the for loop with this and I dont know why the
    question amrk can not be found with either a replace or an
    instr

    If I copy one ? and in the immediate window do a ?Asc("?") is
    tells me that its a ascii character 63. Co if I do an Instr on chr(63)
    it still doesnt work.

    VB Code:
    1. If Dir("D:\DB Query Map.txt") <> vbNullString Then Kill "D:\DB Qyery Map.txt"
    2.     For i = 0 To qry.Count - 1
    3.         If InStr(1, qry(i).Name, "~sq_f") = 0 Then  'Actual queries only
    4.             'Debug.Print qry(i).Name; qry(i).Type
    5.             'Debug.Print qry(i).Properties(qry(i).Properties.Count - 1).Value
    6.             Open "D:\DB Qyery Map.txt" For Append As #1
    7.                 Print #1, qry(i).Name; qry(i).Type
    8.                 Print #1, "----------------------------------------"
    9.                 sTemp = qry(i).Properties(qry(i).Properties.Count - 1).Value
    10.                 Print #1, Trim$(Replace(sTemp, Chr(63), "")) 'Isnt working for some reason????
    11.             Close #1
    12.                                                     'Parse this property to get the tables
    13.             DoEvents                                'Actual queries do not have a fields collection,
    14.                                                     'only form recordsources do. fields = textboxes
    15.                                                     'on form, so to speak.
    16.                                                     'Prop 20 is the DOL prop.
    17.         End If
    18.     Next
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width