2016-05-17 81 views
0

我想將所有查詢從MS Access導出到Excel。我目前能夠導出所有非生成表查詢,但是,我沒有看到生成表查詢彈出。有沒有辦法以以類似的方式導出生成表查詢?謝謝。MS Access 2010:如何將所有生成表查詢字段導出到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 

末次

+0

你應該有什麼工作。我有一個完全按照你的方式工作的導出例程(在QueryDef部分),它也通過'Type = dbQMakeTable'提取查詢。注意:你的第一個For應該去'Count -1'。 –

+0

@HansUp,Make Table Query使用SQL CREATE,不一定需要SELECT INTO。 –

+0

我的解決方法是獲取創建的實際表(因此存在於我在表上運行的VBA輸出中(與上面的代碼非常類似,僅用於表)。但是,我必須仍然匹配make我的最終結果必須是所有查詢名稱+它們的字段名稱 –

回答

0

你有什麼應該工作。我有一個和你的完全一樣的導出例程(在QueryDef部分),它也提取了Type = dbQMakeTable的查詢。

但是,生成表查詢可能沒有字段名稱,所以您的第二個For循環不會產生任何結果,使您認爲它不起作用。

使用您的調試器檢查您是否獲得Make Table Queries,然後檢查字段。也許這些查詢只設置SQL來製作表格。然後您可以分析SQL。它看起來像CREATE TABLE <name> (<fields...>);

+0

對於已保存的CREATE TABLE查詢,QueryDef.Type屬性是dbQDDL(96),而不是'dbQMakeTable'(80)。 – HansUp

+0

嗯,所以Make Table Query可能沒有任何字段,只是被定義爲保存的SQL語句?就像MySQL中的視圖一樣? –

相關問題