2016-11-04 5 views
0

異なる構造を持つ複数のテーブルを含むAccessデータベースがあります(フィールド名、行数、タイトルの番号&)。すべてのテーブルをエクスポート仕様のtxtファイルにエクスポート

私がしたいのは、これらのテーブルをすべて区切り文字( "|")で区切ってtxtファイルにエクスポートし、小数点区切り文字と文字列の引用符を指定することです。

私は、インターネットを閲覧し、どのような私が得たことだっいる:

  • 使用DoCmd.TransferText acExportDelimコマンド
  • カスタマイズされた輸出仕様を保存して、私はエラーmessaggeを取得

それを適用します(「オブジェクトはありません存在しない ")、エクスポート仕様が「シート固有」であるという事実、すなわち異なるフィールドとフィールド名を持つテーブルには適用されないという事実に関連していると私は思う。

私を助けることができますか? ありがとうございます!

EDIT。 元のコードも掲載しています。前にも言いましたが、VBAの初心者です。ウェブ上でコードを探し、ニーズに合わせて実行しました。

Public Sub ExportDatabaseObjects() 
On Error GoTo Err_ExportDatabaseObjects 

Dim db As Database 
Dim db As DAO.Database 
Dim td As TableDef 
Dim sExportLocation As String 
Dim a As Long 

Set db = CurrentDb() 

sExportLocation = "C:\" 'Do not forget the closing back slash! ie: C:\Temp\ 

For a = 0 To db.TableDefs.Count - 1 
    If Not (db.TableDefs(a).Name Like "MSys*") Then 
     DoCmd.TransferText acExportDelim, "Export_specs", db.TableDefs(a).Name, sExportLocation & db.TableDefs(a).Name & ".txt", True 
    End If 
Next a 

Set db = Nothing 

MsgBox "All database objects have been exported as a text file to " & sExportLocation, vbInformation 

Exit_ExportDatabaseObjects: 
Exit Sub 

Err_ExportDatabaseObjects: 
MsgBox Err.Number & " - " & Err.Description 
Resume Exit_ExportDatabaseObjects 

End Sub 

コードを実行する前に、Export_specsをファイルに保存する最初のテーブルを手動でエクスポートしました。

AとBの2つのテーブルを持つデータベースを考えてみましょう。 コードAを適切にエクスポートすると、次のエラーメッセージが表示されます。「3011 - Microsoft Accessデータベースエンジンがオブジェクトを見つけることができませんでした」B#txt 'B#txt'がローカルオブジェクトでない場合は、ネットワーク接続を確認するか、サーバー管理者に連絡してください。 "

+0

存在しないオブジェクトはどれですか? – nicomp

+0

完全なコードを投稿してください。その 'TransferText'行はエクスポート仕様を参照していません。 – Parfait

答えて

1

だから、やや複雑です。 ImportExport仕様を使ってファイルをインポートするルーチンを作成したので、目的に簡単に適応できるはずです。基本的な操作は、あなたが1つのファイルにしたいものを正確に行う仕様を作成することです。次に、このコードを使用してこの仕様をエクスポートします。

Public Function SaveSpecAsXMltoTempDirectory(sSpecName As String) 

Dim oFSO As FileSystemObject 
Dim oTS As TextStream 

Set oFSO = New FileSystemObject 
Set oTS = oFSO.CreateTextFile("C:\Temp\" & sSpecName & ".xml", True) 
oTS.Write CurrentProject.ImportExportSpecifications(sSpecName).XML 

oTS.Close 
Set oTS = Nothing 
Set oFSO = Nothing 

End Function 

その後(私は、このサンプルでは「FILE_PATH_AND_NAME」を使用)メモ帳でこのファイルを開くと、いくつかのプレースホルダを使用してファイル名を置き換えます。その後、戻ってデータベースへのインポートこのコードを使用:

Public Function SaveSpecFromXMLinTempDirectory(sSpecName As String) 

Dim oFSO As FileSystemObject 
Dim oTS As TextStream 
Dim sSpecXML As String 
Dim oSpec As ImportExportSpecification 

Set oFSO = New FileSystemObject 
Set oTS = oFSO.OpenTextFile("C:\Temp\" & sSpecName & ".xml", ForReading) 
sSpecXML = oTS.ReadAll 
For Each oSpec In CurrentProject.ImportExportSpecifications 
    If oSpec.Name = sSpecName Then oSpec.Delete 
Next oSpec 
Set oSpec = CurrentProject.ImportExportSpecifications.Add(sSpecName, sSpecXML) 

Set oSpec = Nothing 
oTS.Close 
Set oTS = Nothing 
Set oFSO = Nothing 

End Function 

今、あなたはサイクルのファイルスルーと、このコードを使用して、それを実行し、ファイル名とスペックでプレースホルダを置き換えることができます。

Public Function ImportFileUsingSpecification(sSpecName As String, sFile As String) As Boolean 

Dim oSpec As ImportExportSpecification 
Dim sSpecXML As String 
Dim bReturn As Boolean 

'initialize return variable as bad until function completes 
bReturn = False 
'export data using saved Spec 
' first make sure no temp spec left by accident 
For Each oSpec In CurrentProject.ImportExportSpecifications 
    If oSpec.Name = "Temp" Then oSpec.Delete 
Next oSpec 
sSpecXML = CurrentProject.ImportExportSpecifications(sSpecName).XML 
If Not Len(sSpecXML) = 0 Then 
    sSpecXML = Replace(sSpecXML, "FILE_PATH_AND_NAME", sFile) 
    'now create temp spec to use, get template text and replace file path and name 
    Set oSpec = CurrentProject.ImportExportSpecifications.Add("Temp", sSpecXML) 
    oSpec.Execute 
    bReturn = True 
Else 
    MsgBox "Could not locate correct specification to import that file!", vbCritical, "NOTIFY ADMIN" 
    GoTo ExitImport 
End If 

ExitImport: 
    On Error Resume Next 
    ImportFileUsingSpecification = bReturn 
    Set oSpec = Nothing 
    Exit Function 
End Function 

明らかにあなたがspec XMLでテーブル名を見つけて、その上にプレースホルダを使用する必要があります。あなたがそれを働かせることができない場合私に教えてくださいと私はエクスポートのために更新されます。

関連する問題