如何在 Word VBA 中使用使用 Access-VBA 定义函数的 Access 查询?

发布于 2024-08-24 09:22:42 字数 1783 浏览 4 评论 0原文

问题的状态:

我不再认为我引用 Excel 或查询具有参数是相关的。我认为问题可以归结为:如何使用在 Word VBA 中使用 Access-VBA 定义的函数的 Access 查询?

我想要做什么 不可能,我该怎么办根据 shahkalpesh 的答案


我在 Access VBA 中有一个函数:

Public Function Acos(radians As Double) As Double
     Acos = WorksheetFunction.Acos(radians)
End Function

WorksheetFunction 来自引用 Excel(我这样做只是因为它定义了 acos 函数)。

我在具有三个参数的查询中使用此函数。然后,我在 Word VBA 中运行此查询,如下所示:

Dim command As New ADODB.command
With command
    .ActiveConnection = connection
    .CommandText = "MyQueryName"
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter( _
        "Param1", adDouble, adParamInput, , param1Value)
    .Parameters.Append .CreateParameter( _
        "Param2", adDouble, adParamInput, , param2Value)
    .Parameters.Append .CreateParameter( _
        "Param3", adDouble, adParamInput, , param3Value)
End With

Dim records As New ADODB.Recordset
records.Open command, , adOpenKeyset, adLockOptimistic

我在 Word VBA 中收到错误,指出函数 Acos 未定义。

有想法吗?

更新

回复评论:是的,查询在 Access 中完美运行。

另外,请注意,这都是 Office 2007。

更新 2

我们将从 Access 转到 Word,因为 VBA 程序已经在 Word 中,但需要进行一些数据处理,这并不实际在 VBA 中执行。

更改为创建 Excel 应用程序对象除了显着减慢查询速度之外没有任何影响。

更新3

我在Word 和Access 中都引用了Excel。 (如果有更好的方法来获得 acos 函数,我当然愿意。)

State of the question:

I no longer think it is relevant that I'm referencing Excel or that the query has parameters. I think question boils down to this: How can I use an Access query which uses Access-VBA-defined functions in Word VBA?

What I want to do is impossible, how can I make an inlined version of the Acos function work with my query, as per shahkalpesh's answer?


I have a function in Access VBA:

Public Function Acos(radians As Double) As Double
     Acos = WorksheetFunction.Acos(radians)
End Function

WorksheetFunction is coming from referencing Excel (which I do simply because it defines the acos function).

I use this function in a query that has three parameters. Then I run this query in Word VBA as follows:

Dim command As New ADODB.command
With command
    .ActiveConnection = connection
    .CommandText = "MyQueryName"
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter( _
        "Param1", adDouble, adParamInput, , param1Value)
    .Parameters.Append .CreateParameter( _
        "Param2", adDouble, adParamInput, , param2Value)
    .Parameters.Append .CreateParameter( _
        "Param3", adDouble, adParamInput, , param3Value)
End With

Dim records As New ADODB.Recordset
records.Open command, , adOpenKeyset, adLockOptimistic

I get an error in Word VBA that the function Acos is not defined.

Ideas?

UPDATE

In response to comment: Yes, the query works perfectly in Access.

Also, just a note, this is all Office 2007.

UPDATE 2

We are going from Access to Word because the VBA program is already in Word but needs to do some data crunching which it isn't really practical to do in VBA.

Changing to creating an Excel Application object has no effect aside from dramatically slowing down the query.

UPDATE 3

I have the reference to Excel in both Word and Access. (If there is a better way to get an acos function, I'm certainly open to it.)

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

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

发布评论

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

评论(2

错爱 2024-08-31 09:22:42

不要使用 Excel 来获取 ACos 的结果,而是尝试使用

其中 X = 字段,该字段包含将传递给 Acos 的值。

SELECT X, IIF(X = 1, 0, Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1))
FROM myTable;

这是 页面,我引用了ACos公式。

尝试&保存以上查询。
Access 中还有其他函数,例如 AtnSqr,它们可以帮助您获得 ACos 所需的内容。因此,您不需要让 Excel 为您计算。

注意:您必须对 ACos 不支持的值进行错误处理。
例如 =ACOS(1.25) 为您提供 #NUM! (不是数字)

类似地,如果上述查询的参数是 1.25,它将返回错误.
因此,请小心并验证输入以确保查询不会崩溃。

Instead of using Excel to get the result of ACos, try this

Where X = field which contains value that will be passed to Acos

SELECT X, IIF(X = 1, 0, Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1))
FROM myTable;

Here is the page, I referred for ACos formula.

Try & save the above query.
Access has other functions in it such as Atn and Sqr which can help you get what is needed for ACos. Hence, you will not need to ask Excel to calculate things for you.

Note: You will have to do the error handling for values not supported by ACos.
e.g. =ACOS(1.25) gives you #NUM! (not a number)

In a similar way, if the parameter to the above query is 1.25, it will return an error.
So, be careful and validate the input to make sure that query doesn't crash.

慕巷 2024-08-31 09:22:42

根据此页面:

http://bytes.com/topic/net/answers/124351-custom-function-vba-access-stored-query- Called-asp-net

我不认为我想要什么 em> 做是可能的。如果有人可以提供有关如何使 shhkalpesh 的答案发挥作用的更多信息,我将给予那里的答案以信用。

Per this page:

http://bytes.com/topic/net/answers/124351-custom-function-vba-access-stored-query-called-asp-net

I don't think what I want to do is possible. If anyone can give more information on how to make shahkalpesh's answer work, I'll give credit for the answer there.

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