2017-03-02 7 views
0

これは私を狂ってしまいます。私はマイクロソフトのアクセスにいくつかのExcelデータをインポートすることに頭を悩ましてきました。彼らが両方のマイクロソフト製品であるので、これが簡単であるべきだと思っている私は愚かです。ファイルをMicrosoft Accessにインポート:フィールドマッピング

それぞれ約40MBの3つのExcelファイルがあります。各ファイルには4つのタブがあり、各タブはファイル間で同じ順序で同じフィールドを持ちます。つまり、ファイル1のタブAは、ファイル2とファイル3のタブAと同じ順序で同じフィールド名を持ちます。また、アクセスデータベース内の対応するテーブルは、ファイルとまったく同じ順序で同じフィールド名になります。他のタブでも同じです。各タブには約90Kの行と約40の列があります。

私はAccessに直接インポートし、新しいテーブルを作成しました。他のファイルのレイアウトは同じですが、他のファイルを正しくインポートするためのアクセス権が得られていないようです。たとえフィールドの名前がまったく同じであっても、マッピングが不正になってしまいます。

1つまたは2つのフィールドの型変換エラーが発生します(アクセステーブルのすべてのフィールドが「ショートテキスト」なので、取得できません)処理のないデータファイル)またはファイルからの間違ったソースフィールドが、データベースの間違ったターゲットフィールドにインポートされます。

ほとんどの場合、いくつかのフィールドがうまくいきません。というのも、テーブル全体をチェックして、問題が解決するかどうかを確認する必要があるからです。それは一貫していない、私はそれを試みるたびに違う。

エクセルファイルからデータをインポートしようとしましたが、各タブをcsvとして保存しました。何も動作しません。 WTFは間違っている。他のデータベース(ファイルメーカーなど)を使ってみてうれしいです。私はアクセスを気にしません、私はちょうどそれが簡単だろうと思ったが、私はなぜこれは非常に厄介なことを得ることはありません。

答えて

0

フォルダ内のすべてのファイルのすべてのワークシートからデータをインポートします。

Dim blnHasFieldNames As Boolean, blnEXCEL As Boolean, blnReadOnly As Boolean 
Dim intWorkbookCounter As Integer 
Dim lngCount As Long 
Dim objExcel As Object, objWorkbook As Object 
Dim colWorksheets As Collection 
Dim strPath As String, strFile As String 
Dim strPassword As String 

' Establish an EXCEL application object 
On Error Resume Next 
Set objExcel = GetObject(, "Excel.Application") 
If Err.Number <> 0 Then 
     Set objExcel = CreateObject("Excel.Application") 
     blnEXCEL = True 
End If 
Err.Clear 
On Error GoTo 0 

' Change this next line to True if the first row in EXCEL worksheet 
' has field names 
blnHasFieldNames = False 

' Replace C:\MyFolder\ with the actual path to the folder that holds the EXCEL files 
strPath = "C:\MyFolder\" 

' Replace passwordtext with the real password; 
' if there is no password, replace it with vbNullString constant 
' (e.g., strPassword = vbNullString) 
strPassword = "passwordtext" 

blnReadOnly = True ' open EXCEL file in read-only mode 

strFile = Dir(strPath & "*.xls") 

intWorkbookCounter = 0 

Do While strFile <> "" 

     intWorkbookCounter = intWorkbookCounter + 1 

     Set colWorksheets = New Collection 

     Set objWorkbook = objExcel.Workbooks.Open(strPath & strFile, , _ 
      blnReadOnly, , strPassword) 

     For lngCount = 1 To objWorkbook.Worksheets.Count 
      colWorksheets.Add objWorkbook.Worksheets(lngCount).Name 
     Next lngCount 

     ' Close the EXCEL file without saving the file, and clean up the EXCEL objects 
     objWorkbook.Close False 
     Set objWorkbook = Nothing 

     ' Import the data from each worksheet into a separate table 
     For lngCount = colWorksheets.Count To 1 Step -1 
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _ 
        "tbl" & colWorksheets(lngCount) & intWorkbookCounter, _ 
        strPath & strFile, blnHasFieldNames, _ 
        colWorksheets(lngCount) & "$" 
     Next lngCount 

     ' Delete the collection 
     Set colWorksheets = Nothing 

     ' Uncomment out the next code step if you want to delete the 
     ' EXCEL file after it's been imported 
     ' Kill strPath & strFile 

     strFile = Dir() 

Loop 

If blnEXCEL = True Then objExcel.Quit 
Set objExcel = Nothing 

http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpAllWkshtsFilesSepTbls

関連する問題