【问题标题】:MS Access 2010: How to Export All Make-Table Query Fields to ExcelMS Access 2010:如何将所有生成表查询字段导出到 Excel
【发布时间】:2020-07-05 22:16:50
【问题描述】:

我想将我的所有查询从 MS Access 导出到 Excel。我目前能够导出所有非生成表查询,但是,我没有看到生成表查询弹出。有没有办法以类似的方式导出生成表查询?谢谢。

Sub ListQueriesAndFields()
 'Macro Purpose:  Write all Query and field names to and Excel file
 'Source:  vbaexpress.com/kb/getarticle.php?kb_id=707
 'Updates by Derek - Added column headers, modified base setting for loop to include all fields,
 '                   added type, size, and description properties to export
 'Updates by Alex - Converted to be used for Queries instead of Tables.

Dim lTbl As Long
Dim lFld As Long
Dim dBase As Database
Dim xlApp As Object
Dim wbExcel As Object
Dim lRow As Long

 'Set current database to a variable and create a new Excel instance
Set dBase = CurrentDb
Set xlApp = CreateObject("Excel.Application")
Set wbExcel = xlApp.workbooks.Add

 'Set on error in case there are no tables
On Error Resume Next

'DJK 2011/01/27 - Added in column headers below
lRow = 1
With wbExcel.sheets(1)
    .Range("A" & lRow) = "Table Name"
    .Range("B" & lRow) = "Field Name"
    .Range("C" & lRow) = "Type"
    .Range("D" & lRow) = "Size"
    .Range("E" & lRow) = "Description"
End With


'AMK - Converted 5/2/2016 for use with Queries instead of Tables
For lQry = 0 To dBase.QueryDefs.Count
     'If the table name is a temporary or system table then ignore it
    If Left(dBase.QueryDefs(lQry).Name, 1) = "~" Or _
    Left(dBase.QueryDefs(lQry).Name, 4) = "MSYS" Then
         '~ indicates a temporary table
         'MSYS indicates a system level table
    Else
         'Otherwise, loop through each query, writing the query and field names
         'to the Excel file
        For lFld = 0 To dBase.QueryDefs(lQry).Fields.Count - 1  'DJK 2011/01/27 - Changed initial base from 1 to 0, and added type, size, and description
            lRow = lRow + 1
            With wbExcel.sheets(1)
                .Range("A" & lRow) = dBase.QueryDefs(lQry).Name
                .Range("B" & lRow) = dBase.QueryDefs(lQry).Fields(lFld).Name
                .Range("C" & lRow) = dBase.QueryDefs(lQry).Fields(lFld).Type
                .Range("D" & lRow) = dBase.QueryDefs(lQry).Fields(lFld).Size
                .Range("E" & lRow) = dBase.QueryDefs(lQry).Fields(lFld).Properties("Description")
            End With
        Next lFld
    End If
Next lQry
 'Resume error breaks
On Error GoTo 0

 'Set Excel to visible and release it from memory
xlApp.Visible = True
Set xlApp = Nothing
Set wbExcel = Nothing

 'Release database object from memory
Set dBase = Nothing

结束子

【问题讨论】:

  • 你所拥有的应该可以工作。我有一个与您的完全相同的导出例程(在 QueryDef 部分),它还使用Type = dbQMakeTable 提取查询。注意:你的第一个 For 应该去Count -1
  • @HansUp,生成表查询使用 SQL CREATE,不一定是 SELECT INTO
  • 我的解决方法是获取创建的实际表(因此存在于我在表上运行的 VBA 输出中(与上面的代码非常相似,仅用于表)。但随后我仍然必须将生成表的表名与其查询名匹配。我的最终结果必须是所有查询名+它们的字段名。

标签: vba ms-access ms-access-2010


【解决方案1】:

你所拥有的应该可以工作。我有一个与您的完全一样的导出例程(在 QueryDef 部分),它还使用Type = dbQMakeTable 提取查询。

但是,生成表查询可能没有字段名称,因此您的第二个 For 循环不会产生任何结果,让您认为它不起作用。

使用您的调试器检查您是否获得了 Make Table Queries,然后检查字段。也许这些查询只设置了SQL 来制作表格。然后,您可以分析 SQL。好像是CREATE TABLE <name> (<fields...>);

【讨论】:

  • 对于已保存的 CREATE TABLE 查询,QueryDef.Type 属性是 dbQDDL (96),而不是 dbQMakeTable (80)。
  • 嗯,所以Make Table Query可能没有任何字段,只是定义为保存的SQL语句?喜欢 MySQL 中的视图?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-05
  • 2011-12-12
  • 1970-01-01
  • 2010-11-19
  • 2011-01-30
  • 2016-10-11
相关资源
最近更新 更多