Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从excel导出数据以访问VBA_Vba_Excel_Ms Access - Fatal编程技术网

从excel导出数据以访问VBA

从excel导出数据以访问VBA,vba,excel,ms-access,Vba,Excel,Ms Access,我正在尝试使用以下代码将数据集从excel上载到access数据库 但是,我不断得到运行时错误3078-应用程序定义或对象定义错误。为此,我使用dao,并添加了以下参考: Microsoft office 16.0 access数据库引擎对象库, Microsoft access 16.0库 我无法添加Microsoft DAO 3.6对象库(名称与现有模块、项目或对象库冲突) 代码如下: Sub access_upload() Dim strMyPath As String, strDBNa

我正在尝试使用以下代码将数据集从excel上载到access数据库

但是,我不断得到运行时错误3078-应用程序定义或对象定义错误。为此,我使用dao,并添加了以下参考:

Microsoft office 16.0 access数据库引擎对象库, Microsoft access 16.0库

我无法添加Microsoft DAO 3.6对象库(名称与现有模块、项目或对象库冲突)

代码如下:

Sub access_upload()

Dim strMyPath As String, strDBName As String, strDB As String
Dim i As Long, n As Long, lLastRow As Long, lFieldCount As Long
Dim daoDB As DAO.Database
Dim recSet As DAO.Recordset

strDBName = "Database8.accdb"
'presume to be in the same location as the host workbook:

 strMyPath = ThisWorkbook.Path

 strDB = strMyPath & "\" & strDBName
 Set daoDB = DBEngine.Workspaces(0).OpenDatabase(strDB)
 Dim ws As Worksheet
 Set ws = ActiveWorkbook.Sheets("Sheet2")
 Set recSet = daoDB.OpenRecordset("Database8")

 lFieldCount = recSet.Fields.Count
 lLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row

 For i = 2 To lLastRow
 recSet.AddNew
 For n = 0 To lFieldCount - 1
 recSet.Fields(n).Value = ws.Cells(i, n + 1)
 Next n
 recSet.Update
 Next i

 recSet.Close
 daoDB.Close

 Set daoDB = Nothing
 Set recSet = Nothing

 End Sub

请告诉我这里缺少什么。谢谢!

从Excel访问

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents\"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
'       Kill strPathFile

      strFile = Dir()
Loop

此外,如果路径和excel名称是固定的,则可以使用功能区中的外部功能手动单步执行导入操作,最后会提示将这些步骤保存为已定义的导入


然后,您可以通过docmd重新运行导入以运行保存的导入

在哪一行出现错误?Set recSet=daoDB.OpenRecordset(“Database8”)'这是您的表Database8吗?我的access数据库名称是Database8Yup,但在
OpenRecordset
中,您需要输入要插入数据的表名,而不是数据库名