私は、ハウスキーピングおよび/またはデータ品質問題の負荷あなたがあるかもしれないので、私は主に(照会したいとライアンが言及したのと同様のことを行うと思いますが、テーブルに直接インポートしないでしょう最初に対処する必要があるかもしれない)。
私は最初に複数の英数字フィールド(GGは189フィールドになります; F1-F189と呼ぶ)を持つステージングテーブルを作成し、ファイル名を格納するフィールドを追加します重複したレコードの回避や、テーブル内の更新(datetimestampとprocessフラグ)を処理することができます。
次に、各ファイルを順番にステージングテーブルにインポートし、ループ内でクエリを実行するループを作成できます(あらかじめ記述されているAccessクエリを参照すると、パラメータを設定する必要がありますあなたのFOR-NEXTループのファイル名を使用して、ステージングテーブルから適切なデスティネーションテーブルにレコードを挿入する、時間の経過とともにそれを調整することができます。
ループ内で、各ファイルをインポートした後、次のファイルに移動する前にいくつかのハウスキーピングを実行することができます。
以下は、私が使用しているものからかなりのものを貼り付けたものです。これは面倒で、他人のコードから変更されたビットを使用しています。私はそうではありません:
Sub import_ASSGMU()
' Macro loops through the specified directory (strPath)
' and imports Excel files to staging table in the Access Database
' (tbl_import_generic)
' before using an Access query to insert into the cleaned-up destination table
' (tbl_transformed_ASSGMU)
Dim strPath As String
strPath = "c:\data" ' YOU MUST SET YOUR OWN PATH HERE
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
' this bit is if you want to move files out of the folder after they are processed
' Dim ImportSuccessPath As String
' ImportSuccessPath = "c:\data\Processed" 'hard-coded sub-folder name
' If Right(ImportSuccessPath, 1) <> "\" Then ImportSuccessPath = ImportSuccessPath & "\"
Dim strFile As String 'Filename
Dim strFileList() As Variant 'File Array
Dim intFile As Integer 'File Number
'Loop through the folder & build file list - file name prefix is HARDCODED
strFile = Dir(strPath & "ASSG_MU_*.xls*")
' this takes all the filenames and adds them to an array
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
'see if any files were found
If intFile = 0 Then
'MsgBox "No files found"
Exit Sub
End If
' sorts the array so files are in filename order
' needs a procedure from here:
' http://www.access-programmers.co.uk/forums/showthread.php?t=194737
Call SortArray(strFileList())
' cycle through the list of files in the array & import to Access
For intFile = 1 To UBound(strFileList)
' empty staging table
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE FROM tbl_import_generic;"
DoCmd.SetWarnings True
' delete records from same filename previously imported
'DoCmd.SetWarnings False
' DoCmd.RunSQL "DELETE FROM tbl_transformed_ASSGMU WHERE import_file = """ & strFileList(intFile) & """;"
'DoCmd.SetWarnings True
' allows import of xls AND xlsx
If Right(strFileList(intFile), 1) = "s" Then
DoCmd.TransferSpreadsheet acImport, 8, "tbl_import_generic", strPath & strFileList(intFile), False, "A1:GG50000"
ElseIf Right(strFileList(intFile), 1) = "x" Then
DoCmd.TransferSpreadsheet acImport, 10, "tbl_import_generic", strPath & strFileList(intFile), False, "A1:GG50000"
End If
'DoCmd.Echo False, ""
DoCmd.SetWarnings False
Set db = CurrentDb
Dim qdf As DAO.QueryDef
' create an Access INSERT query with a name that matches the next code line that will take records from the tbl_import_generic and write them to the destination table. At this stage you can embed some hygeine factors or parse data in the Access query
Set qdf = db.QueryDefs("qry_transform_ASSGMU")
' create a Parameter called param_filename and use it to populate a
' destination field - this will then put the name of the imported
' file in your destination table
qdf!param_filename = strFileList(intFile)
qdf.Execute 'dbFailOnError ' useful when testing your query
DoCmd.SetWarnings True
' if you want to "move" files out of the folder when processed
' copy processed file to Processed folder & delete file from Import
' FileCopy strPath & strFileList(intFile), ImportSuccessPath & strFileList(intFile)
' Kill strPath & strFileList(intFile)
Next
MsgBox UBound(strFileList) & " file(s) were imported."
End Sub
遊んでください。これがあなたにとって有用であることを願っています。
あなたが試したこと、出力、そしてその出力を望むものを示してください。 – Matt