2013-06-05 80 views
2

我在Excel中的宏綁定到一個命令按鈕在我的工作表中的一個。點擊時,我試圖將工作表「FeedSamples」中的數據導出到名爲「ImportedData」的Access數據庫表中。導出Excel工作表來訪問表(.ACCDB)

誰能幫助我?我從網上嘗試了多個例子,但沒有運氣。這是我現在卻不斷收到「運行時錯誤‘3343’:無法識別的數據庫格式「文件路徑\ FeedSampleResults.accdb

Dim db As Database 
Dim rs As Recordset 
Dim r As Long 
Set db = OpenDatabase("filePath\FeedSampleResults.accdb") 
Set rs = db.OpenRecordset("ImportedData", dbOpenTable) 
r = 2 
Do While Len(Worksheets("FeedSamples").Range("A" & r).Formula) > 0 
    With rs 
     .AddNew 
     .Fields("REPTNO") = Worksheets("FeedSamples").Range("B" & r).value 
     .Update 
    End With 
    r = r + 1 
Loop 
rs.Close 
Set rs = Nothing 
db.Close 
Set db = Nothing 

一旦我得到這個成就,我需要的代碼有訪問表導出數據到一個dBASE文件。

回答

1

下面是使用ADO。你需要設置數據源Access數據庫的完整路徑的代碼。

Sub ExcelToAccessAdo() 

    Dim cn As ADODB.Connection, rs As ADODB.Recordset, row As Long 
    Set cn = New ADODB.Connection 
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _ 
      "Data Source=filePath\FeedSampleResults.accdb;" 

    ' open a recordset 
    Set rs = New ADODB.Recordset 
    rs.Open "ImportedData", cn, adOpenKeyset, adLockOptimistic, adCmdTable 

    row = 3 ' the start row in the worksheet 
    Do While Not IsEmpty(Worksheets("FeedSamples").Range("A" & row)) 

     With rs 
      .AddNew ' create a new record 
      .Fields("REPTNO") = Worksheets("FeedSamples").Range("A" & row).Value 
      .Update 
     End With 
     row = row + 1 
    Loop 

    rs.Close 
    Set rs = Nothing 
    cn.Close 
    Set cn = Nothing 

End Sub