Export all MS Access SQL queries to text files

后端 未结 5 1517
失恋的感觉
失恋的感觉 2020-12-08 07:32

I have to document an MS Access database with many many macros queries, etc. I wish to use code to extract each SQL query to a file which is named the same as the query, eg

相关标签:
5条回答
  • 2020-12-08 07:46

    Hope this helps.

    Public Function query_print()
    Dim db As Database
    Dim qr As QueryDef
    
    Set db = CurrentDb
    
    For Each qr In db.QueryDefs
      TextOut (qr.Name)
      TextOut (qr.SQL)
      TextOut (String(100, "-"))
    Next
    End Function
    
    Public Sub TextOut(OutputString As String)
    
        Dim fh As Long
    
        fh = FreeFile
        Open "c:\File.txt" For Append As fh
        Print #fh, OutputString
        Close fh
    
    End Sub
    
    0 讨论(0)
  • 2020-12-08 07:48

    This should get you started:

      Dim db As DAO.Database
      Dim qdf As DAO.QueryDef
    
      Set db = CurrentDB()
      For Each qdf In db.QueryDefs
        Debug.Print qdf.SQL
      Next qdf
      Set qdf = Nothing
      Set db = Nothing
    

    You can use the File System Object or the built-in VBA File I/O features to write the SQL out to a file. I assume you were asking more about how to get the SQL than you were about how to write out the file, but if you need that, say so in a comment and I'll edit the post (or someone will post their own answer with instructions for that).

    0 讨论(0)
  • 2020-12-08 07:55

    I modified @andre-bernardes's code to use "|" separators before the query names and ":" separators before the SQL statements. The different separators make it easier to parse the Queries.txt file with python and create a dictionnary of queries and SQL statements. You can then use this dictionary to create views in an SQLite table for example.

    VBA code to extract the SQL queries

    Public Sub ListQueries()
        ' Modified from André Bernardes
        Dim i As Integer
        Dim ff As Long
        ff = FreeFile()
        Open "C:\Dev\Queries.txt" For Output As #ff
        On Error Resume Next
    
        For i = 0 To CurrentDb.QueryDefs.Count - 1
            Debug.Print "|" & CurrentDb.QueryDefs(i).Name & ":"
            Print #ff, "|" & CurrentDb.QueryDefs(i).Name & ":"
    
            Debug.Print CurrentDb.QueryDefs(i).SQL
            Print #ff, CurrentDb.QueryDefs(i).SQL
        Next
    End Sub
    

    Python code to parse Queries.txt into a dictionary

    queries_file = open(data_path + '/Queries.txt')
    queries = queries_file.read().split('|')
    l = [x.split(':') for x in queries]
    l.pop(0)
    table_name_to_query = {name: query for name, query in l}
    

    Create SQLite views from the Access queries

    import sqlite3
    conn = sqlite3.connect('example.db')
    c = conn.cursor()
    for table, query in table_name_to_query.items():
        try:
            c.execute("CREATE VIEW `%s` AS %s" % (table,query))
            print("\n\n"+ table + " passed")
            print(query)
        except Exception as e:
            print("\n\n"+ table + " error")
            print(e)
            print(query)
    
    0 讨论(0)
  • 2020-12-08 08:03

    This solution include fields in query

    Public Sub ListQueries()
        ' Author:                     Date:               Contact:
        ' André Bernardes             09/09/2010 08:45    bernardess@gmail.com     http://al-bernardes.sites.uol.com.br/
        ' Lista todas as queries da aplicação.
        ' Listening:
    
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim l As Integer
    
        On Error Resume Next
    
        For i = 0 To CurrentDb.QueryDefs.Count - 1
            Debug.Print "Query: " & CurrentDb.QueryDefs(i).Name
    
            For j = 0 To CurrentDb.QueryDefs(i).Fields.Count - 1
                Debug.Print "Field " & CurrentDb.QueryDefs(i).Fields(j).Name
            Next
    
            Debug.Print "  SQL: " & CurrentDb.QueryDefs(i).SQL
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-08 08:12
    1. In the VB Window, click Tools->References....
    2. In the References window add the dependency Microsoft Scripting Runtime by checking it off.

    Then this code will export the queries to a file suitable for using grep on:

    Sub ExportQueries()
    
      Dim fso As New FileSystemObject
    
      Dim stream As TextStream
    
      Set stream = fso.CreateTextFile("e:\temp\queries.txt")
    
      Dim db As DAO.Database
      Dim qdf As DAO.QueryDef
    
      Set db = CurrentDb()
      For Each qdf In db.QueryDefs
    
        stream.writeline "Name: " & qdf.Name
        stream.writeline qdf.SQL
        stream.writeline "--------------------------"
      Next qdf
      Set qdf = Nothing
      Set db = Nothing
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题