2017-06-19 67 views
1

即時通訊使用Access 2013和Excel 2013.就參考而言,我使用Microsoft Office 15.0 Access數據庫引擎對象庫。在VBA中插入查詢

所以我試圖從VBA運行INSERT INTO查詢。工作表有一個零件號碼列表,我使用這段代碼轉換成一個數組。

Function partArray() 
    Dim partList() As Variant 
    Dim partArr(10000) As Variant 
    Dim x As Long 

    partList = ActiveWorkbook.Worksheets("Parts").ListObjects("Parts").ListColumns("Part Number").DataBodyRange.Value 

    For x = LBound(partList) To UBound(partList) 
     partArr(x) = partList(x, 1) 
    Next x 

    partArray = partArr 

End Function 

現在我試圖使用INSERT INTO查詢將這些零件號碼輸入到訪問表中。任何想法我怎麼能做到這一點?

+0

您試圖遷移到Access的行數是多少? – Brad

+0

你不能直接用數組來做。您需要使用循環並創建INSERT字符串,就像您手動輸入字符串一樣。 – SandPiper

+0

約1000行。所以創建一個String,然後循環數組中的每個條目。在每個循環中,aString&entry&「,」。這會工作嗎?或者我需要將所有條目轉換爲字符串? –

回答

0

哇!我認爲你的方法是完全錯誤的。嘗試這樣的事情。

Sub ADOFromExcelToAccess() 
' exports data from the active worksheet to a table in an Access database 
' this procedure must be edited before use 
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long 
    ' connect to the Access database 
    Set cn = New ADODB.Connection 
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _ 
     "Data Source=C:\FolderName\DataBaseName.mdb;" 
    ' open a recordset 
    Set rs = New ADODB.Recordset 
    rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable 
    ' all records in a table 
    r = 3 ' the start row in the worksheet 
    Do While Len(Range("A" & r).Formula) > 0 
    ' repeat until first empty cell in column A 
     With rs 
      .AddNew ' create a new record 
      ' add values to each field in the record 
      .Fields("FieldName1") = Range("A" & r).Value 
      .Fields("FieldName2") = Range("B" & r).Value 
      .Fields("FieldNameN") = Range("C" & r).Value 
      ' add more fields if necessary... 
      .Update ' stores the new record 
     End With 
     r = r + 1 ' next row 
    Loop 
    rs.Close 
    Set rs = Nothing 
    cn.Close 
    Set cn = Nothing 
End Sub 

或者,這個。

Sub DAOFromExcelToAccess() 
' exports data from the active worksheet to a table in an Access database 
' this procedure must be edited before use 
Dim db As Database, rs As Recordset, r As Long 
    Set db = OpenDatabase("C:\FolderName\DataBaseName.mdb") 
    ' open the database 
    Set rs = db.OpenRecordset("TableName", dbOpenTable) 
    ' get all records in a table 
    r = 3 ' the start row in the worksheet 
    Do While Len(Range("A" & r).Formula) > 0 
    ' repeat until first empty cell in column A 
     With rs 
      .AddNew ' create a new record 
      ' add values to each field in the record 
      .Fields("FieldName1") = Range("A" & r).Value 
      .Fields("FieldName2") = Range("B" & r).Value 
      .Fields("FieldNameN") = Range("C" & r).Value 
      ' add more fields if necessary... 
      .Update ' stores the new record 
     End With 
     r = r + 1 ' next row 
    Loop 
    rs.Close 
    Set rs = Nothing 
    db.Close 
    Set db = Nothing 
End Sub 

當然,如果你願意,你可以使用TransferSpreadsheet方法。

Option Explicit 

Sub AccImport() 
    Dim acc As New Access.Application 
    acc.OpenCurrentDatabase "C:\Users\Public\Database1.accdb" 
    acc.DoCmd.TransferSpreadsheet _ 
      TransferType:=acImport, _ 
      SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _ 
      TableName:="tblExcelImport", _ 
      Filename:=Application.ActiveWorkbook.FullName, _ 
      HasFieldNames:=True, _ 
      Range:="Folio_Data_original$A1:B10" 
    acc.CloseCurrentDatabase 
    acc.Quit 
    Set acc = Nothing 
End Sub