2016-05-17 11 views
0

MS AccessからExcelにすべてのクエリをエクスポートしたいと思います。私は現在、すべての非テーブル作成クエリをエクスポートすることができますが、テーブル作成クエリがポップアップすることはありません。どのような方法があります同様の方法でメイクテーブルクエリをエクスポートするには?ありがとう。MS Access 2010:すべてのMake-Tableクエリフィールドを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 

End Subの

+0

あなたは何をすべきですか?私はあなたのもの(QueryDefの部分)と同じように動作し、 'Type = dbQMakeTable'でクエリーを抽出するエクスポートルーチンを持っています。注:あなたの最初のForは 'Count -1'に行くべきです。 –

+0

@ HansUp、Make Table QueryはSQL CREATEを使用し、必ずしもSELECT INTOではありません。 –

+0

私の回避策は、作成された実際のテーブルを取ることでした(テーブル上で実行したVBA出力にも存在しています)。テーブル名をクエリ名に戻します。結果はすべてのクエリ名+フィールド名になります。 –

答えて

0

あなたが動作するはずですが何をしています。私は正確に(QueryDefの部分で)あなたと同様に動作し、Type = dbQMakeTableでquerysを抽出するエクスポートルーチンを持っています。

ただし、テーブル作成クエリにはフィールド名が含まれていない可能性があるため、2番目のForループでは結果が得られないため、動作しなかったと考えられます。

デバッガを使用してMake Table Queriesを確認し、フィールドを確認します。たぶんこれらのクエリはテーブルを作るためにSQLしか設定していないかもしれません。その後、SQLを分析することができます。それはのように見えるCREATE TABLE <name> (<fields...>);

+0

保存された 'CREATE TABLE'クエリの場合、' QueryDef.Type'プロパティは 'dbQDDL'(96)です。 'dbQMakeTable'(80)。 – HansUp

+0

ええと、テーブル作成クエリにはフィールドはありませんが、保存されたSQL文として定義するだけです。 –

関連する問題