2016-08-14 52 views
2

我試圖從Excel導入數據到Access中。 2010年兩者都是完美的,直到我遇到一個包含[文本'A'文本]的單元格。此時訪問完全停止Sub。當我手動將Excel單元格更改爲[文本文本]或將''更改爲``時,所有內容都可以再次正常工作。但是必須手動更改源Excel才能達到目的。當excel單元格包含雙精度值時,VBA將excel導入訪問毛刺''

當一個或多個單元格包含['A']時,如何導入Excel工作表? 非常感謝您的幫助。

'This checks if file exsist, imports file, then imports any sequential files. 
Option Explicit 
Public Sub ImportXL2(bolJustExcelFile As Boolean, Optional bolRefresh As Boolean) 

      Dim rstXL As DAO.Recordset 
      Dim x As Integer, y As Long 
      Dim strPath1 As String, strPath2 As String 
      Dim strPN As String, strDescription As String, strPrime As String 
      Dim intOHB As Integer, sngCost As Single, intMin As Integer, intMax As Integer 
      Dim strCode As Integer, strNumber As String, strDate As String, strQty As Integer, strRepairable As String, strEntity As String 

      DoCmd.SetWarnings False 
      DoCmd.RunSQL "DELETE FROM ExcelFile" 

      If bolJustExcelFile = False Then 
       DoCmd.RunSQL "DELETE FROM ExcelFileCombined" 
      End If 

      For x = 1 To 10 

      DoCmd.RunSQL "DELETE FROM ExcelFiletemp" 

      strPath1 = Environ("userprofile") & "\Desktop\Folder\ExcelFile.xlsx" 
      strPath2 = Environ("userprofile") & "\Desktop\Folder\ExcelFile" & x & ".xlsx" 

       If x = 1 Then 
        If FileExists(strPath1) = -1 Then 
         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "ExcelFiletemp", strPath1, False, "A:L" 

         Else 
         If bolRefresh = True Then 
          MsgBox "ExcelFile File Not Found", , "Missing ExcelFile File" 
         End If 

         Exit For 
        End If 
       Else 
        If FileExists(strPath2) = -1 And bolJustExcelFile = False Then 
         DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "ExcelFiletemp", strPath2, False, "A:L" 
        Else 
         GoTo SkipXL 
        End If 
       End If 

       Set rstXL = CurrentDb.OpenRecordset("SELECT * FROM ExcelFiletemp", dbOpenSnapshot) 

       rstXL.MoveLast 
       rstXL.MoveFirst 

        For y = 1 To 4 
         rstXL.MoveNext 
        Next y 

       strEntity = Right(rstXL![F1], 6) 

        For y = 1 To 4 
         rstXL.MoveNext 
        Next y 
      On Error GoTo ErrHandler 

        For y = 1 To rstXL.RecordCount - 8 

          strPN = rstXL![F1] 
          strDescription = rstXL![F2] 
          strPrime = rstXL![F3] 
          intOHB = rstXL![F4] 
          sngCost = rstXL![F5] 
          intMin = rstXL![F6] 
          intMax = rstXL![F7] 
          strCode = rstXL![F8] 
          strRepairable = rstXL![F12] 

          If x = 1 Then 
           DoCmd.RunSQL "INSERT INTO ExcelFile (PN, Description, Prime, OHB, Cost, Min, Max, Code, Repairable, Entity) VALUES ('" & strPN & "','" & strDescription & "','" & strPrime & "'," & intOHB & "," & sngCost & "," & intMin & "," & intMax & "," & strCode & ",'" & strRepairable & "','" & strEntity & "');" 
          End If 

          If bolJustExcelFile = False Then 
           DoCmd.RunSQL "INSERT INTO ExcelFileCombined (PN, Description, Prime, OHB, Cost, Min, Max, Code, Repairable, Entity) VALUES ('" & strPN & "','" & strDescription & "','" & strPrime & "'," & intOHB & "," & sngCost & "," & intMin & "," & intMax & "," & strCode & ",'" & strRepairable & "','" & strEntity & "');" 
          End If 
        rstXL.MoveNext 
        Next y 
       rstXL.Close 
SkipXL: 
       Next x 

      Set rstXL = Nothing 

      DoCmd.SetWarnings True 
ErrHandler: 
        If Err.Number = 94 Then 'Invalid use of Null 

        rstXL.MoveNext 
        End If 

      End Sub 
+0

您使用的是單引號來包裝你的文本字段。您可能需要將字符串中的單引號替換爲無 - 或轉義它們。您還應該修改錯誤處理程序,以便在SQL插入錯誤的情況下進行陷阱和退出(如果需要)你如何處理單引號取決於你是否希望他們在你的數據庫中完全一樣 - 或者清理 – dbmitch

+0

一個繁瑣但可能有效的方法可能是將數據導入爲數組,並通過它們循環轉義單引號( ascii 39,又名打字機撇號)。這可能有所幫助:http://stackoverflow.com/questions/881194/how-do-i-escape-special-characters-in-mysql/881208#881208 – Miqi180

+1

[參數查詢](http://stackoverflow.com/search q =%5Bms接入%5D +參數+查詢) – HansUp

回答

0

我認爲使用recordset添加新的記錄會以某種方式讓你無後顧之憂有關SQL語法越來越因爲特殊字符的錯誤例如單引號或雙引號。您可以嘗試添加一項功能:

Function insrt_item(rstXL as DAO.Recordset, tbl_dest as String) ' set tbl_dest to ExcelFile or ExcelFileCombined since they are same fields anyway 
    With currentdb.OpenRecordSet(tbl_dest) 
     .AddNew 
     !PN = rstXL!F1 
     !Description = rstXL!F2 
     '.. add more fields here 
     .Update 
     .Close 
    End With 
End Function 
1

您可以將單引號加倍。

Function EscQ(text As String) 

    EscQ = Replace(text, "'", "''") 

End Function 

用法:

 

DoCmd.RunSQL "INSERT INTO ExcelFileCombined (PN, Description, Prime, OHB, Cost, Min, Max, Code, Repairable, Entity) VALUES ('" & EscQ(strPN) & "','" & EscQ(strDescription) & "','" & EscQ(strPrime) & "'," & intOHB & "," & sngCost & "," & intMin & "," & intMax & "," & strCode & ",'" & EscQ(strRepairable) & "','" & EscQ(strEntity) & "');" 

+0

托馬斯謝謝, 功能EscQ(文本字符串) EscQ =替換(文字, 「'」, 「 ''」) 端功能 是一個優雅的解決方案。 – MrJCob

+0

我很高興能夠提供幫助。您可能想考慮在[代碼評論](http://codereview.stackexchange.com/)上發佈您的代碼。你可以在那裏深入分析你的代碼。您可以將「選擇」和「插入」語句組合成一個查詢。例如: – 2016-08-15 05:09:29

+0

INSERT INTO ExcelFile(PN,說明,總理,OHB,成本,最小值,最大值,代碼,可修改,實體) 選擇[F1],[F2],[F3],[F4],[F5] ,[F6],[F7],[F8],[F12] FROM ExcelFiletemp' – 2016-08-15 05:09:45