473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using Excel Functions from within Access

119 100+
Hi there. I need to perform a number of financial calculations within a database I am creating. Rather than writing my own functions, I figured it would be worthwhile to use the functions that already exist within excel.

I have found sample code on the web for calling an excel function (being new to Access I tend to borrow bits and pieces from anywhere and everywhere):

Expand|Select|Wrap|Line Numbers
  1. Function fXLCoupDayBS(dtmSettlement As Date, dtmMaturity As Date, frequency As Integer, basis As Integer) As Long
  2. '   Uses the XL function DAYS360 to return the number of days between 2 dates based on a 360 day year.
  3. '   Accepts:
  4. '   dtmStart - the start date
  5. '   dtmEnd - the end date
  6. '   blnMethod - whether to use the US or European Method in the calculation
  7. '   Returns:
  8. '   a string containing the converted number
  9.     On Error GoTo E_Handle
  10.     Dim objXL As Excel.Application
  11.     Set objXL = CreateObject("Excel.Application")
  12.     If IsMissing(blnMethod) Then blnMethod = False
  13.     fXLCoupDayBS = objXL.WorksheetFunction.COUPDAYBS(dtmSettlement, dtmMaturity, frequency, basis)
  14. fExit:
  15.     objXL.Quit
  16.     Set objXL = Nothing
  17.     Exit Function
  18. E_Handle:
  19.     MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
  20.     Resume fExit
  21. End Function
However, I am not quite sure how to call this from Access. Ideally, I would like the result of the function (an integer) to appear in the same row as the inputs to the function, and all of these to be in the same query.

In short, I am not sure how to call a function from a query, passing in the arguments of the function from the query. Can anyone help? Again, if anyone can suggest a better method I am all ears!
Dec 8 '06 #1
5 4078
NeoPa
32,556 Expert Mod 16PB
You don't need to open Excel (as in posted code) to access the Excel library.
Just add the Excel library to the References under Tools of the VBA window.
However, I'm pretty sure you can't access objects and/or their methods from within SQL.
Global (Public) functions can be used but nothing else :(.
objExcel.WorksheetFunction.* functions are, therefore, unavailable.
Dec 8 '06 #2
billelev
119 100+
Hmmm...So if I can't access the function from within an SQL statement, how should I execute the code?

I have two tables:

Security and
TradeInfo

For a particular Trade (defined within TradeInfo) of a Security I need to calculate accrued interest. This takes information from the two tables, and uses the function outlined above. I am able to execute the function and calculate the accrued interest by calling the function from an Event Procedure on the TradeInfo form. Is there a better way?

Would it then be possible to insert this data into an empty field within the TradeInfo table, or should I enter the data into a new table, and link the two with a query?
Dec 11 '06 #3
NeoPa
32,556 Expert Mod 16PB
Basically it's not accessible to SQL.
However, you could define a global (Public) function in your database that could be called by the SQL code.
This could access the Excel Worksheet functions and return the result you need.
Encapsulation, if you like.
Dec 11 '06 #4
billelev
119 100+
So I can called a VBA sub-routine or function from an SQL query? Do you know the format? I have been searching for the syntax but can't find anything on it.
Dec 11 '06 #5
NeoPa
32,556 Expert Mod 16PB
So I can called a VBA sub-routine or function from an SQL query? Do you know the format? I have been searching for the syntax but can't find anything on it.
Expand|Select|Wrap|Line Numbers
  1. Public Function YourFunctionName(...) As Variant
  2.     ...
  3.     YourFunctionName = <Value to be returned>
  4.     ...
  5. End Function
Dec 12 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
0
by: Alexander Mandl | last post by:
Hello I have a pivot table in an Access form and open the pivot Table (Excel OLE Object) from within Access. In the Excel Table is a macro (signed) (in teh open event) running when opening the...
0
by: Jack | last post by:
Windows 2K Pro Access/Excel 2003 Hi there, I have a large number of password protected Excel Workbooks. The files are protected by code that runs when the workbook opens. The code asks the...
3
by: info | last post by:
After using clipboard functions in Excel controlled from Access VBA, Excel doesn't quit when I use the following ExcelApp.Quit Set ExcelApp = Nothing If I don't use the clipboard functions in...
1
by: Scott P | last post by:
I am looking for a way to use several Excel functions (e.g. LINEST, LOGEST and RSQ) in C#. Do Microsoft or any third parties offer good tools to enable the use of such functions from within C#...
4
by: David_from_Chicago | last post by:
What I am trying to do is to simulate the LINEST functionality from Excel in Access through VBA. When I use LinEst in Excel I can get back five statistical results. Here are is the formula array...
5
by: Louis | last post by:
I am trying to use php to update a spreadsheet (MS Excel or OpenOffice Calc) with data from MySQL. I looked into PEAR::Spreadsheet_Excel_Writer if it will do the job. If I understand it...
4
by: drt | last post by:
NEDERLANDS: Hallo, Ik heb eigen functies gemaakt in access, die werken perfect in de access query. Zodra ik echter vanuit excel een draaitabel maakt naar de access query (als een externe...
3
by: keirnus | last post by:
Hello once again... I made a function in Excel. The function does some error checking within the Excel file. To be easy for me, I want my code in MS Access to simply call the function in Excel....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.