将 Excel 中的数据插入 Access
我编写了一些代码来将 Excel 表中的数据插入到 Access 数据库中 - 我的代码如下:
Sub AddData()
Dim Cn As ADODB.Connection
Set Cn = New ADODB.Connection
'lets connect to the workbook first, I tested this, it works for me
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sample.xls;Extended Properties=Excel 8.0;" _
& "Persist Security Info=False"
' Append data from Sheet1 of workbook to Table1 of mydb.mdb:
Cn.Execute "INSERT INTO tblSales IN 'C:\Users\User\Documents\access.mdb' SELECT * FROM [datasheet]"
Cn.Close
Set Cn = Nothing
End Sub
我的问题是在执行此代码时出现错误“Microsoft Jet Engine 找不到对象“数据表”的路径”。数据表只是我的工作簿中数据所在的工作表的名称,非常感谢任何帮助。
I've made some code to insert data from an excel table in to an access database - my code is as follow:
Sub AddData()
Dim Cn As ADODB.Connection
Set Cn = New ADODB.Connection
'lets connect to the workbook first, I tested this, it works for me
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sample.xls;Extended Properties=Excel 8.0;" _
& "Persist Security Info=False"
' Append data from Sheet1 of workbook to Table1 of mydb.mdb:
Cn.Execute "INSERT INTO tblSales IN 'C:\Users\User\Documents\access.mdb' SELECT * FROM [datasheet]"
Cn.Close
Set Cn = Nothing
End Sub
My problem is when executing this I get the error "Microsoft Jet Engine could not find the path to object "datasheet" . Datasheet is just the name of the sheet where the data is located in my workbook. Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
数据表表前 8 行中出现警告。假设有一个标题(HDR=YES)
接下来的 6 行应包含虚拟数据来定义与访问表列定义等效的列。
Warning in your Datasheet Table The first 8 rows. Assuming theres a Heading (HDR=YES)
the next 6 rows should contain a dummy data to define your columns equivalent to your access table column definition.
如果在工作表名称后添加 $ 符号会发生什么
像这样[数据表$]
?
What happens if you put a $ sign after the sheet name
like this [datasheet$]
?
我认为您不能对任何打开的工作簿执行查询。它必须针对文件运行,这意味着您必须提供工作表的完整路径,包括文件名。如果您的工作簿是“脏”的,您需要先保存它。
我宁愿
I think you cannot execute a query on just any open workbook. It MUST be run against a file, that means you have to provide a full path to you sheet, including the filename. If your workbook is "dirty", you need to save it first.
I would rather
据我所知,所缺少的只是数据源的路径和数据表上的字符串:
应该读取,例如:
并且:
All that is missing, as far as I can see, is the path to your data source and a string on datasheet:
Should read, for example:
And:
SELECT 语句在数据库本身上运行,但您希望从 EXCEL 发送值。因此,您必须
在循环中最终使用来处理所有行/列等。
希望这有助于祝您
好运
编辑...
不要忘记 CHAR 和标点符号周围的引号;我使用
编辑2
我假设在 EXCEL 中要插入到数据库中的值都在 1 行中...
假设您有一个包含多于 1 行的 source_range,则必须为该范围中的每一行触发 INSERT 语句。您可以使用
.Rows
属性从范围中返回单行。您可以使用.Cells(1, 1)
、.Cells(1,2)
.... 等将多列发送到同一行中的 INSERT 语句举例:
The SELECT statement runs on the database itself, but you want to send values from EXCEL. So you must use
eventually in a loop to proces all rows/columns etc.
Hope that helps
good luck
EDIT ...
don't forget quotation marks surrounding CHAR's and interpunctations; I use
EDIT 2
I asume that in EXCEL the values you want to insert into the DB are all in 1 row ...
Suppose you have a source_range which contains more than 1 row, you must fire the INSERT statement for each row in that range. You use the
.Rows
property to return a single row from a range. And you send multiple columns to the INSERT statement within the same row by using.Cells(1, 1)
,.Cells(1,2)
.... and so onexample: