使用VBA将数据从Excel导出到Access

我有一个Excel文件中的一些表的数据,我想要导出这些数据到我的数据库上的Access(在我的数据库水质量具体表中)与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 

目前,问题是当我debugging代码时,在函数“cnt.Open sConnect”上出现编译错误:“未find方法或数据成员”。

如果这是可能的,任何帮助将不胜感激。

注意:我正在使用Office 2010。

你的编译错误是由于这两行:

 Dim cnt As DAO.Database cnt.Open sConnect 

一个DAO.Database对象没有.Open方法,它解释“ 找不到方法或数据成员 ”。 错误消息常常会有些模糊,而且不是很有帮助。 但是,在这种情况下,我想不出如何更清楚的错误信息。

还有一些我不明白的东西。 sConnect看起来像一个ADO连接string。 但是cnt是一个DAO (数据库)对象。 你不能在一个语句中混合这样的两个对象模型。

您在活动variables声明之前有这个:

 'Dim cnt As New ADODB.Connection 

然后在你的程序中,你有:

 'Close the ADO objects cnt.Close 

所以也许你最初打算cnt成为一个ADO.Connection对象,并且在将其切换到一个DAO.Database对象后,并没有调整其余的代码。

我build议你修改你的代码来解决DAO和ADO混淆的问题,然后向我们展示新的代码,如果你有任何问题。 并请只显示我们所需的最低限度的testing代码来重现您希望解决的问题。 TIA供您考虑。

我只有Access数据库,打开Excel文件(而不是其他方式),但通过查看我的代码,我认为你应该直接到这个:

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

http://support.microsoft.com/kb/190195上也find了这个&#x3002;

这有帮助吗?