将参数传递给 vba 函数时获取错误消息:“参数不可选(错误 449)”

发布于 2024-09-15 19:46:40 字数 758 浏览 8 评论 0原文

我试图从 MS-Access 2007 中的函数检索 ADODB 记录集,但收到一条非常烦人的错误消息:“参数不可选(错误 449)”。

我真的不明白我做错了什么,请帮忙!

问候,

Stefan

函数:

Function Rs(sourceSQL As String) As ADODB.Recordset

' Create New Disconnected Recordset

Dim rsConnection As ADODB.Connection
Dim rsRecordset As ADODB.Recordset

Set rsConnection = New ADODB.Connection
rsConnection.Open CurrentProject.Connection

Set rsRecordset = New ADODB.Recordset

rsRecordset.CursorLocation = adUseClient
rsRecordset.Open sourceSQL, rsConnection

Set Rs = rsRecordset

Set rsRecordset.ActiveConnection = Nothing

End Function

函数调用:

Private Sub Form_Load()

Call Rs("tblDocumentCode")

Debug.Print Rs.txtDocumentCode(0).Value

End Sub

I am trying to retrieve an ADODB recordset from a function in MS-Access 2007 but get a very annoying error message thrown in my face saying: "Argument not optional (Error 449)".

I really can't figure out what I am doing wrong, please help!

Regards,

Stefan

FUNCTION:

Function Rs(sourceSQL As String) As ADODB.Recordset

' Create New Disconnected Recordset

Dim rsConnection As ADODB.Connection
Dim rsRecordset As ADODB.Recordset

Set rsConnection = New ADODB.Connection
rsConnection.Open CurrentProject.Connection

Set rsRecordset = New ADODB.Recordset

rsRecordset.CursorLocation = adUseClient
rsRecordset.Open sourceSQL, rsConnection

Set Rs = rsRecordset

Set rsRecordset.ActiveConnection = Nothing

End Function

FUNCTION CALL:

Private Sub Form_Load()

Call Rs("tblDocumentCode")

Debug.Print Rs.txtDocumentCode(0).Value

End Sub

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

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

发布评论

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

评论(2

束缚m 2024-09-22 19:46:40

您使用 rs 两次,一次作为函数,一次作为记录集的名称:

Private Sub Form_Load()

Set Myrs= Rs("tblDocumentCode")

Debug.Print MyRs(0).Value

End Sub

You are using rs twice, once as a function, once as the name of a recordset:

Private Sub Form_Load()

Set Myrs= Rs("tblDocumentCode")

Debug.Print MyRs(0).Value

End Sub
弱骨蛰伏 2024-09-22 19:46:40

假设“txtDocumentCode”是记录集中的一个字段,则:

  Private Sub Form_Load()
    Call Rs("tblDocumentCode")
    Debug.Print Rs.txtDocumentCode(0).Value
  End Sub

...应该更改为:

  Private Sub Form_Load()
    Debug.Print Rs("tblDocumentCode").Fields("txtDocumentCode").Value
  End Sub

据我所知,这应该可以工作,而不需要将函数返回的记录集分配给变量。

但让我退后一步,建议修复这个语法错误引出了一个问题:她所做的事情是非常不可取的。每次调用此函数时,都会打开一个新连接,但 Access 在反复使用的单个连接中效果更好。 Jet/ACE 后端和服务器后端都是如此。初始化连接所需的开销将使您的表单加载非常缓慢。

但更糟糕的是,这不是 Access 编程——这是“来自缺乏绑定表单/控件的编程环境的难民”编程。您应该使用带有链接表的 ODBC,然后您可以将记录源分配给您的表单,而不必费力使用 ADO 记录集。如果您坚持做您正在做的事情,那么您可能根本不使用 Access,因为您放弃了 Access 75% 或更多的优势,并且这样做不会带来任何性能或并发优势(并给自己带来了一个充满问题的世界)。

当然,现在我再次查看它,您正在使用 CurrentProject.Connection,并且我不确定它如何与 ODBC 链接表交互。事实上,我想这可能是一个 ADP 而不是 MDB/ACCDB,但这更没有意义,因为您根本不需要在 ADP 中做任何额外的事情来使用 ADO 记录集填充您的表单 - 它是默认。

所以,一般来说,除了语法错误之外,还有一些问题——你所做的根本没有多大意义。

Assuming that "txtDocumentCode" is a field in the recordset, this:

  Private Sub Form_Load()
    Call Rs("tblDocumentCode")
    Debug.Print Rs.txtDocumentCode(0).Value
  End Sub

...should be changed to this:

  Private Sub Form_Load()
    Debug.Print Rs("tblDocumentCode").Fields("txtDocumentCode").Value
  End Sub

So far as I can tell, that should work without needing to assign the recordset returned by the function to a variable.

But let me step back a bit and suggest that fixing this syntactical error begs the question: what's being done her is pretty inadvisable. Every time this function is called, you're opening a new connection, but Access works better with a single connection that is used over and over again. That's true of both Jet/ACE back ends and server back ends. The overhead required to initialize the connection is going to make your forms load really slowly.

But even worse, this is not Access programming -- this is "refugee from a programming environment that lacks bound forms/controls" programming. You should be using ODBC with linked tables and then you can assign recordsources to your forms without having to muck about with ADO recordsets. If you insist on doing what you're doing, you might as well not be using Access at all, because you're discarding 75% or more of the advantages of Access, and you're getting no performance or concurrency benefits from doing so (and buying yourself a world of problems).

Of course, now that I look at it again, you're using the CurrentProject.Connection, and I'm not sure how this interacts with ODBC linked tables. Indeed, I guess it's possible this is an ADP and not an MDB/ACCDB, but that makes even less sense, since you don't need to do anything extra at all in an ADP to populate your forms with ADO recordsets -- it's the default.

So, in general, something is wrong beyond the syntax error -- what you are doing simply doesn't make a lot of sense.

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