字“大小”从MS访问中检索数据时,不用作为列名称

发布于 2025-02-12 22:27:53 字数 812 浏览 1 评论 0原文

尝试从MS Access数据库中检索VBA中的数据时,我遇到了一个奇怪的错误。我得到错误:

运行时错误'-2147467259(80004005)':方法'execute'对象'_connection'失败。

最初,当我遇到错误时,我的SQL语句中有一长串列列表,但是通过反复试验,我发现问题与名为“ size”的列有关。此后,我尝试将列命名为重命名,除了任何版本的“大小”(大小,大小,大小等)的任何版本外,似乎都可以使用。

有人知道为什么吗?

代码:

Sub test()
Dim connStr, objConn
Dim sPath As String
sPath = "[path]"
fullPath = sPath & "\[dbname].accdb"

connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & fullPath
 
'Define object type
Set objConn = CreateObject("ADODB.Connection")
 
'Open Connection
objConn.Open connStr

Dim queryStr As String

queryStr = "SELECT ID, Size FROM primary_table"

Set rs = objConn.Execute(queryStr) 'This is were the error occurs

objConn.Close
Set rs = Nothing
Set objConn = Nothing

End Sub

I have encountered a strange error when trying to retrieve data in VBA from an MS Access database. I get the error:

Run-time error '-2147467259 (80004005)': Method 'Execute' of object '_Connection' failed.

I initially had a long list of columns in my SQL statement when I got the error but with trial and error I discovered that the issue was with the column named "Size". I since tried to rename the column and it seems like anything works except any versions of the word "size" (Size, size, sizE etc.).

Anyone have an idea of why?

Code:

Sub test()
Dim connStr, objConn
Dim sPath As String
sPath = "[path]"
fullPath = sPath & "\[dbname].accdb"

connStr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & fullPath
 
'Define object type
Set objConn = CreateObject("ADODB.Connection")
 
'Open Connection
objConn.Open connStr

Dim queryStr As String

queryStr = "SELECT ID, Size FROM primary_table"

Set rs = objConn.Execute(queryStr) 'This is were the error occurs

objConn.Close
Set rs = Nothing
Set objConn = Nothing

End Sub

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

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

发布评论

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

评论(1

你げ笑在眉眼 2025-02-19 22:27:53

正如问题评论中指出的那样:“ size>”是访问中的保留单词,因此必须是:

  • 包装在方括号中:[size];或
  • 通过将表名的名称前缀为“完全资格”:primary_table.size

最佳实践是避免首先使用保留的单词作为列或表名称。有关保留单词的全面列表,请参阅艾伦·布朗(Allen Browne)的出色站点:

问题名称和访问中的单词

As was pointed out in the question comments, "Size" is a reserved word in Access, so it must be EITHER:

  • wrapped in square brackets: [Size]; OR
  • be "fully qualified" by prefixing it with the table name: primary_table.Size

Best practice is to avoid using reserved words as column or table names in the first place. For a comprehensive list of reserved words, refer to Allen Browne's superb site:

Problem names and reserved words in Access

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