如何使用Excel VBA从Access数据库中提取备注字段?

发布于 2024-08-26 19:27:49 字数 907 浏览 8 评论 0原文

我有一个 Excel 电子表格。我正在通过 ODBC 连接到 Access 数据库。然后

Set dbEng = CreateObject("DAO.DBEngine.40")
Set oWspc = dbEng.CreateWorkspace("ODBCWspc", "", "", dbUseODBC)
Set oConn = oWspc.OpenConnection("Connection", , True, "ODBC;DSN=CLIENTDB;")

我使用查询并获取结果集来获取一些表数据。

Set oQuery = oConn.CreateQueryDef("tmpQuery")
oQuery.Sql = "SELECT idField, memoField FROM myTable"
Set oRs = oQuery.OpenRecordset

现在问题就出现了。我的字段是 dbMemo,因为最大内容长度可达几百个字符。它并没有那么长,事实上我正在读取的值只有十几个字符。但 Excel 似乎根本无法处理备注字段内容。我的代码...

ActiveCell = oRs.Fields("memoField")

...给出错误运行时错误'3146':ODBC调用失败。

有什么建议吗? Excel VBA实际上可以获取备注字段数据吗?或者说这完全不可能。我也从 GetChunk 得到完全相同的错误。

ActiveCell = oRs.Fields("memoField").GetChunk(0, 2)

...还给出错误运行时错误'3146':ODBC-调用失败。

转换为文本字段使一切正常。然而,有些数据当然会被截断为 255 个字符,这意味着这不是一个可行的解决方案。

I have an Excel spreadsheet. I am connecting to an Access database via ODBC. Something along then lines of:

Set dbEng = CreateObject("DAO.DBEngine.40")
Set oWspc = dbEng.CreateWorkspace("ODBCWspc", "", "", dbUseODBC)
Set oConn = oWspc.OpenConnection("Connection", , True, "ODBC;DSN=CLIENTDB;")

Then I use a query and fetch a result set to get some table data.

Set oQuery = oConn.CreateQueryDef("tmpQuery")
oQuery.Sql = "SELECT idField, memoField FROM myTable"
Set oRs = oQuery.OpenRecordset

The problem now arises. My field is a dbMemo because the maximum content length is up to a few hundred chars. It's not that long, and in fact the value I'm reading is only a dozen characters. But Excel just doesn't seem able to handle the Memo field content at all. My code...

ActiveCell = oRs.Fields("memoField")

...gives error Run-time error '3146': ODBC--call failed.

Any suggestions? Can Excel VBA actually get at memo field data? Or is it just completely impossible. I get exactly the same error from GetChunk as well.

ActiveCell = oRs.Fields("memoField").GetChunk(0, 2)

...also gives error Run-time error '3146': ODBC--call failed.

Converting to a text field makes everything work fine. However some data is truncated to 255 characters of course, which means that isn't a workable solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

漫雪独思 2024-09-02 19:27:49
  • 尝试 Range.CopyFromRecordset 看看它是否有效。
  • 尝试使用 CStr(oRs.Fields("memoField")) 并分配给 Range/ActiveCellValue2
  • 尝试将备注字段设置为表中的最后一个物理列。备注字段仅在检索时为只读。备注字段曾经/可能仍然存在问题,这些字段实际上不在表的末尾。

我现在能想到的就这些了。

  • Try Range.CopyFromRecordset to see if it works.
  • Try using CStr(oRs.Fields("memoField")) and assign to Value2 of the Range/ActiveCell.
  • Try making the memo field the last physical column in table. A memo field is read only when retrieved. There was/may-still-be an issue with memo fields that are not at physically at the end of a table.

All I can think of right now.

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