简体   繁体   English

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

[英]How can I use an Access query which uses Access-VBA-defined functions in Word VBA?

State of the question: 问题的状态:

I no longer think it is relevant that I'm referencing Excel or that the query has parameters. 我不再认为我引用Excel或查询具有参数是相关的。 I think question boils down to this: How can I use an Access query which uses Access-VBA-defined functions in Word VBA? 我认为问题归结为:我如何使用在Word VBA中使用Access-VBA定义函数的Access查询?

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 ? 我想要做的是不可能的 ,根据shahkalpesh的回答 ,如何使我的查询的内联版本的Acos函数可以工作?


I have a function in Access VBA: 我在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). WorksheetFunction来自引用Excel(我只是因为它定义了acos函数)。

I use this function in a query that has three parameters. 我在具有三个参数的查询中使用此函数。 Then I run this query in Word VBA as follows: 然后我在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

I get an error in Word VBA that the function Acos is not defined. 我在Word VBA中收到错误,即未定义函数Acos。

Ideas? 想法?

UPDATE UPDATE

In response to comment: Yes, the query works perfectly in Access. 回复评论:是的,查询在Access中完美运行。

Also, just a note, this is all Office 2007. 另外,只需注意,这就是Office 2007。

UPDATE 2 更新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. 我们将从Access转到Word,因为VBA程序已经在Word中,但需要进行一些数据处理,这在VBA中实际上并不实际。

Changing to creating an Excel Application object has no effect aside from dramatically slowing down the query. 除了显着减慢查询速度之外,更改为创建Excel应用程序对象没有任何效果。

UPDATE 3 更新3

I have the reference to Excel in both Word and Access. 我在Word和Access中都引用了Excel。 (If there is a better way to get an acos function, I'm certainly open to it.) (如果有更好的方法来获得acos功能,我当然愿意接受它。)

Instead of using Excel to get the result of ACos , try this 不要使用Excel来获得ACos的结果,试试这个

Where X = field which contains value that will be passed to Acos 其中X =包含将传递给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. 这是页面 ,我提到了ACos公式。

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 . 访问中有其他功能,如AtnSqr它可以帮助你获得什么是需要ACos Hence, you will not need to ask Excel to calculate things for you. 因此,您无需要求Excel为您计算内容。

Note: You will have to do the error handling for values not supported by ACos . 注意:您必须对ACos不支持的值进行错误处理。
eg =ACOS(1.25) gives you #NUM! 例如=ACOS(1.25)给你#NUM! (not a number) (不是数字)

In a similar way, if the parameter to the above query is 1.25, it will return an error. 以类似的方式,如果上述查询的参数是1.25,它将返回错误。
So, be careful and validate the input to make sure that query doesn't crash. 因此,请小心并验证输入以确保查询不会崩溃。

Per this page: 按此页面:

http://bytes.com/topic/net/answers/124351-custom-function-vba-access-stored-query-called-asp-net 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. 如果有人能提供更多关于如何使shahkalpesh的答案工作的信息,那么我会在那里给出答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM