2012-06-27 69 views
2

我在Excel文件中有一個表格,其中包含一些數據,並且我希望使用VBA將這些數據導出到我的Access數據庫(在我的數據庫的具體表格中稱爲「水質」)代碼,以避免每次打開我的數據庫,我想介紹更多的數據。從Excel導出數據到使用VBA進行訪問

目前,我有這樣的代碼,但它不工作...

Sub Button14_Click() 

' Macro purpose: To add record to Access database using ADO and SQL 
' NOTE: Reference to Microsoft ActiveX Data Objects Libary required 

' Exports data from the active worksheet to a table in an Access database 

'Dim cnt As New ADODB.Connection 
'Dim rst As New ADODB.Recordset 
Dim cnt As DAO.Database 
Dim rst As Recordset 
Dim dbPath As String 
Dim tblName As String 
Dim rngColHeads As Range 
Dim rngTblRcds As Range 
Dim colHead As String 
Dim rcdDetail As String 
Dim ch As Integer 
Dim cl As Integer 
Dim notNull As Boolean 
Dim sConnect As String 

'Set the string to the path of your database as defined on the worksheet 
dbPath = "C:\Documents and Settings\Administrador\Mis documentos\MonEAU\modelEAU Database V.2.accdb" 
tblName = "Water Quality" 
Set rngColHeads = ActiveSheet.Range("tblHeadings") 
Set rngTblRcds = ActiveSheet.Range("tblRecords") 

'Set the database connection string here 
sConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & dbPath & "';"  'For use with *.accdb files 
' sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";"  'For use with *.mdb files 

'Concatenate a string with the names of the column headings 
colHead = " (" 
For ch = 1 To rngColHeads.Count 
    colHead = colHead & rngColHeads.Columns(ch).Value 
    Select Case ch 
     Case Is = rngColHeads.Count 
      colHead = colHead & ")" 
     Case Else 
      colHead = colHead & "," 
    End Select 
Next ch 

'Open connection to the database 
cnt.Open sConnect 
'Begin transaction processing 
On Error GoTo EndUpdate 
cnt.BeginTrans 

'Insert records into database from worksheet table 
For cl = 1 To rngTblRcds.Rows.Count 
    'Assume record is completely Null, and open record string for concatenation 
    notNull = False 
    rcdDetail = "('" 

    'Evaluate field in the record 
    For ch = 1 To rngColHeads.Count 
     Select Case rngTblRcds.Rows(cl).Columns(ch).Value 

       'if empty, append value of null to string 
      Case Is = Empty 
       Select Case ch 
        Case Is = rngColHeads.Count 
         rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL)" 
        Case Else 
         rcdDetail = Left(rcdDetail, Len(rcdDetail) - 1) & "NULL,'" 
       End Select 

       'if not empty, set notNull to true, and append value to string 
      Case Else 
       notNull = True 
       Select Case ch 
        Case Is = rngColHeads.Count 
         rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "')" 
        Case Else 
         rcdDetail = rcdDetail & rngTblRcds.Rows(cl).Columns(ch).Value & "','" 
       End Select 
     End Select 
    Next ch 

    'If record consists of only Null values, do not insert it to table, otherwise 
    'insert the record 
    Select Case notNull 
     Case Is = True 
      rst.Open "INSERT INTO " & tblName & colHead & " VALUES " & rcdDetail, cnt 
     Case Is = False 
      'do not insert record 
    End Select 
Next cl 

EndUpdate: 
'Check if error was encounted 
If Err.Number <> 0 Then 
    'Error encountered. Rollback transaction and inform user 
    On Error Resume Next 
    cnt.RollbackTrans 
    MsgBox "There was an error. Update was not succesful!", vbCritical, "Error!" 
Else 
    On Error Resume Next 
    cnt.CommitTrans 
End If 

'Close the ADO objects 
cnt.Close 
Set rst = Nothing 
Set cnt = Nothing 
On Error GoTo 0 

End Sub 

目前的問題是,當我調試代碼,出現的編譯錯誤:「方法或數據成員不找到「功能」cnt.Open sConnect「。

如果這是可能的,任何幫助將不勝感激。

注:我使用的是Office 2010中

+0

現在,當我調試代碼時,該錯誤出現在該行中:New Keybord的使用無效 – meikse

回答

0

我只有打開Excel文件(而不是周圍的其他方法),Access數據庫但通過我的代碼的樣子,我想你應該直接去這個:

`Set cnt = OpenDatabase_ 
     (dbPath, False, True, "Access 8.0;") 

http://support.microsoft.com/kb/190195也找到了。

這有幫助嗎?

+0

錯誤仍然存​​在,同一行發生同樣的錯誤... – meikse

2

你的編譯錯誤是由於這2行:

Dim cnt As DAO.Database 
cnt.Open sConnect 

DAO.Database對象不具有.Open方法,這解釋了「方法或數據成員未找到」。錯誤消息經常會有些模糊,而且不是很有幫助。然而,在這種情況下,我想不出如何更清楚的錯誤信息。

還有一些我不明白的東西。 sConnect看起來像一個ADO連接字符串。但是cnt是一個DAO(數據庫)對象。你不能在一個語句中混合這樣的兩個對象模型。

你剛纔您的積極變量聲明之前,有這樣的:

'Dim cnt As New ADODB.Connection 

然後在你隨後的過程中,您有:

'Close the ADO objects 
cnt.Close 

因此,或許你原本打算cnt是一個ADO.Connection對象,沒在將其切換到DAO.Database對象後,不會調整其餘的代碼。

我建議你修改你的代碼來理清DAO和ADO的混淆,然後向我們展示新的代碼,如果你有任何剩餘的問題。並且請僅顯示重現您希望解決的問題所需的最低測試代碼。 TIA供您考慮。

+1

謝謝!我可以解決這個問題......就像你說的那樣,DAO和ADO之間出現了混亂。現在還有其他問題,首先我必須檢查現在失敗的情況。 – meikse

相關問題