views:

294

answers:

2

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.)

+1  A: 

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.

shahkalpesh
This works for replacing the Acos function, but it still won't let me run the query from VBA. I will try inlining this, but the X I'm running Acos on is a very complicated expression so it's going to be a tremendous mess.
Daniel Straight
Actually, the expression in my call to Acos is so long Access won't let me inline this function...
Daniel Straight
shahkalpesh
How do I refer back to a previously calculated field? I tried just putting the field name in the expression and Access asked for the value as if it was a parameter.
Daniel Straight
I ended up having to make a temporary column and then calculate the real result, but then it wouldn't let me sort on that without doing what I said before about asking for the calculated field as if it was a query, so I had to make a second query which queries the first query then sorts.... BUT IT WORKS.
Daniel Straight
shahkalpesh
A: 

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.

Daniel Straight