将 Excel 中的数据插入 Access

发布于 2024-10-03 00:49:02 字数 653 浏览 0 评论 0原文

我编写了一些代码来将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无人问我粥可暖 2024-10-10 00:49:02
xlFilepath = Application.ThisWorkbook.FullName
Sql = "INSERT INTO tblSales " & _

"SELECT * FROM [Excel 12.0 Macro;HDR=YES;DATABASE=" & xlFilepath & "].[datasheet$A1:AK10011] "

cnn.Execute Sql

数据表表前 8 行中出现警告。假设有一个标题(HDR=YES)
接下来的 6 行应包含虚拟数据来定义与访问表列定义等效的列。

xlFilepath = Application.ThisWorkbook.FullName
Sql = "INSERT INTO tblSales " & _

"SELECT * FROM [Excel 12.0 Macro;HDR=YES;DATABASE=" & xlFilepath & "].[datasheet$A1:AK10011] "

cnn.Execute Sql

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.

泪眸﹌ 2024-10-10 00:49:02

如果在工作表名称后添加 $ 符号会发生什么
像这样[数据表$]

What happens if you put a $ sign after the sheet name
like this [datasheet$]
?

清旖 2024-10-10 00:49:02

我认为您不能对任何打开的工作簿执行查询。它必须针对文件运行,这意味着您必须提供工作表的完整路径,包括文件名。如果您的工作簿是“脏”的,您需要先保存它。
我宁愿

  • 循环工作表行并一一添加记录
  • ,或者使用您刚刚从 Access 编写的代码(如果合适)

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

  • loop the worksheet lines and add the records one by one
  • or use the code you just wrote from Access (if it is suitable)
难得心□动 2024-10-10 00:49:02

据我所知,所缺少的只是数据源的路径和数据表上的字符串:

Data Source=sample.xls;

应该读取,例如:

Data Source=c:\docs\sample.xls;

并且:

SELECT * FROM [datasheet$]

All that is missing, as far as I can see, is the path to your data source and a string on datasheet:

Data Source=sample.xls;

Should read, for example:

Data Source=c:\docs\sample.xls;

And:

SELECT * FROM [datasheet$]
落叶缤纷 2024-10-10 00:49:02

SELECT 语句在数据库本身上运行,但您希望从 EXCEL 发送值。因此,您必须

cn.Execute "INSERT .... VALUES (" & excelcell_or_variable & ");"

在循环中最终使用来处理所有行/列等。

希望这有助于祝您

好运

编辑...
不要忘记 CHAR 和标点符号周围的引号;我使用

' ....
' .... "...VALUES (" & T(Q(MyStringCell)) & T(MyNumCell) & Q(MyLastTextCell) & ");"
' ....

' surrounds a string by single quotes
Private Function Q(Arg as String) As String
    Q = "'" & Arg & "'"
Return

' appens a comma to a string
Private Function T(Arg as String) As String
    T = Arg & ","
Return

编辑2
我假设在 EXCEL 中要插入到数据库中的值都在 1 行中...

假设您有一个包含多于 1 行的 source_range,则必须为该范围中的每一行触发 INSERT 语句。您可以使用 .Rows 属性从范围中返回单行。您可以使用 .Cells(1, 1).Cells(1,2) .... 等将多列发送到同一行中的 INSERT 语句举例

Sub Test()
Dim MyRange As Range, MyRow As Range

    Set MyRange = Range([B4], [C8])   ' source range

    For Each MyRow In MyRange.Rows    ' get one row at the time from source range 
        Debug.Print MyRow.Cells(1, 1), MyRow.Cells(1, 2)
        ' replace the above by your INSERT statement
    Next MyRow

End Sub

The SELECT statement runs on the database itself, but you want to send values from EXCEL. So you must use

cn.Execute "INSERT .... VALUES (" & excelcell_or_variable & ");"

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

' ....
' .... "...VALUES (" & T(Q(MyStringCell)) & T(MyNumCell) & Q(MyLastTextCell) & ");"
' ....

' surrounds a string by single quotes
Private Function Q(Arg as String) As String
    Q = "'" & Arg & "'"
Return

' appens a comma to a string
Private Function T(Arg as String) As String
    T = Arg & ","
Return

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 on

example:

Sub Test()
Dim MyRange As Range, MyRow As Range

    Set MyRange = Range([B4], [C8])   ' source range

    For Each MyRow In MyRange.Rows    ' get one row at the time from source range 
        Debug.Print MyRow.Cells(1, 1), MyRow.Cells(1, 2)
        ' replace the above by your INSERT statement
    Next MyRow

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文