How do I reference a Query in a Custom Function? - ms-access

I am trying to create a custom function by referencing the Excel Object Model. By first going to Create/Module/Tools/References/ and selecting Microsoft Excel 15.0 Object Library (Access 2010) I was able to allow Access to bring-in the Excel Object Model. From there, I created a basic function in Acces using the Excel functions ACOS and COS. I was then able to run this function in my Query without issue. Here is the function:
Public Function myTestFunction(x As Double) As Double
myTestFunction = Excel.WorksheetFunction.Acos(Cos(50))
End Function
This test function is generally meaningless to me, but was a good first step in achieving my ultimate goal. The next step was to reference a column in my Query within the function itself. Basically, I want to replace the 50 value with each row in my Query column [LAT]. Here is what I've tested:
Public Function myTestFunction(x As Double) As Double
myTestFunction = Excel.WorksheetFunction.Acos(Cos([Queries].[Data Query]![LAT]))
End Function
After attempting to run this function in my Query I receive the following error:
Run-Time Error 424
Object Required
My question is, how can I reference the Query column [LAT] within my function?

Revise your function to operate on its x argument.
Public Function myTestFunction(x As Double) As Double
'myTestFunction = Excel.WorksheetFunction.Acos(Cos(50))
myTestFunction = Excel.WorksheetFunction.Acos(Cos(x))
End Function
Then you can use the function in a query and give it your [LAT] field value.
SELECT dq.[LAT], myTestFunction(dq.[LAT])
FROM [Data Query] AS dq;

Related

Why am I getting error 3078 on my SELECT query?

I've started to try and write some VBA to execute some queries and I've got stuck at the first hurdle. This is giving an error 3078 which apparently means it can't find the table or query. The table definitely exists and is spelt properly. Indeed the SQL runs fine - I tested it. What am I doing wrong?
Public Function Tester()
str_tbl = "tblGames_atp"
str_mkvrec = "SELECT * FROM " & str_tbl
dbl_fs_pct = DSum("FS", str_mkvrec)
End Function
Cannot reference SQL statement in domain aggregate function, not even a variable set to that statement. Must reference table or query object name. Could reference variable with name string but variable not really needed in this code. If you want function to return a value to calling source, then need to set function value.
Public Function Tester()
Tester = DSum("FS", "tblGames_atp")
End Function

Call stored procedure with an output parameter using ExecuteDataset

I need to call a stored procedure in a MySQL (5.7) database from a VB.NET application. Said procedure makes a SELECT statement at the end which I need to retrieve in my app as a DataSet. Previously it worked fine, but I added an output parameter to the stored procedure, and I get the following error when I call it:
{"Incorrect number of arguments for PROCEDURE my_database.SP_MY_PROCEDURE; expected 3, got
2"}
Here's my current VB.NET code:
Public Shared Function CallStoredProcedure(ByVal stringParam As String,
ByVal intParam As Integer) As DataSet
Try
Dim ds As New DataSet
Dim params As New List(Of MySqlParameter)
Dim arrayParams() As MySqlParameter
params.Add(New MySqlParameter("#param1", stringParam))
params.Add(New MySqlParameter("#param2", intParam))
arrayParams = params.ToArray()
ds = MySqlHelper.ExecuteDataset(connString,
"CALL SP_MY_PROCEDURE(#param1, #param2);",
arrayParams)
Return ds
Catch objException As Exception
Return Nothing
End Try
End Function
I tried adding a third parameter to the CALL statement in the string, like this:
ds = MySqlHelper.ExecuteDataset(connString,
"CALL SP_MY_PROCEDURE(#param1, #param2, param3);",
arrayParams)
but that just returns a different error:
{"OUT or INOUT argument 3 for routine gw_my_database.SP_MY_PROCEDURE
is not a variable or NEW pseudo-variable in BEFORE trigger"}
How can I accomplish this? Maybe ExecuteDataset can't be used with output parameters; at least I haven't found examples or anything. It that's the case, what's a good alternative?
You can definitely use ExecuteDataset() with output parameter. You need to make sure all the mandatory parameters are added to your parameter list including output parameter. You need to confirm the type and size where applicable and set the direction of each parameter correctly. After executing ExecuteDataset() you can retrieve the value from our output parameter.
I could not see adding the third parameter in your code (or perhaps you haven't showed that part), setting direction/type/size.

Passing a Field for Access Query to a Module Function as an Array

I am trying to write a function in a module that we take a field from a MS ACCESS query that is passed to it and return the values of the field to another field in the Query.
The code that I have written in the module is provided below.
Public Function YearAmps(ByRef array1() As Double)
Dim array2(0 To UBound(array1())) As Double
Dim I
For I = 0 To UBound(array2())
array2(I) = array1(I)
Next I
YearAmps = array2
End Function
When I put the following in the Query field: YearAmps([SN]), where YearAmps is the function that I am calling and SN is the field that I am passing to the function and run the code, I get an error.
Does anyone know where I am going wrong. Is is possible to pass a field from an Query as an array in a Module and then return that array?
Any help would be greatly appreciated.

Is it possible to qualify a VBA function call with its module name in an Access query?

I have 2 public VBA functions Module1.Foo and Module2.Foo.
Is it possible to use either Foo function in an Access query? What is the syntax for qualifying the function call with the module name?
Not that I know of. But you could create a helper function for the purpose:
Public Function Module2_Foo(<params>)
Module2_Foo = Module2.Foo(<params>)
End Function

How to return a range object from a user defined function in vba

I have this piece of code in excel:
Private Function RelCell(NmdRng as String) as Range
Set RelCell = Range(NmdRng).Cells(1,1)
End Function
it gives the runtime error "91': object variable or with block variable not set.
I really don't know what is the problem with my function.. someone does?
I don't know if this is the problem but your are only setting the range and aren't returning anything from the function.
Try declaring a range variable with a different name as the function and return that.
Actually, you should be able to return a range from a UDF as described in this MSDN Thread.
Here is the code given by the MVP:
Function GetMeRange(rStartCell As Range, lRows As Long, iColumns As Integer) As Range
Set GetMe = rStartCell.Resize(lRows, iColumns) ' note the use of Set here since we are setting an object variable
End Function
(and it works)
Tiago's comment points out a very right thing, as you want to access a named range, it should be defined first.
You can try to set a breakpoint in your UDF and see if the Range(NmdRng) is defined.
Your named range already has a cell reference attached to it, so you shouldn't need to have the .Cells(1,1) at the end of it.
Using the .Range(nmdRng) property alone will return the range object you are looking for.
Try:
Private Function RelCell(NmdRng as String) as Range
Set RelCell = Range("NmdRng")
End Function
Please rewrite your code and test it as follows :
Private Function RelCell(NmdRng as String) as Range
Dim TestRange As Range
Set TestRange=Range(NmdRng)
TestRange.Activate 'I think that error will occur here because, NmdRng is somehow invalid
Set RelCell = TestRange.Cells(1,1)
End Function