|
|

Using Excel Functions from within Access
There are times when Access doesn't have the required functions built-in. In such cases, you have several options:
- Create the function yourself to be used from within Access (or else use a function that somebody else has created - this might be a commercial library add-in);
- Use an existing function from another Office program, such as Excel, that meets your requirements.
Using Excel, or any other Office program, has several implications. Firstly, if Excel isn't present on the user's machine the function will fail, and even if Excel is there, if you are using Early Binding and setting a reference to the Excel Object Library in your database, you need to ensure that the versions of Excel are the same. Secondly, there will be a noticeable performance loss.
When using Excel, you can either use Early Binding or Late Binding. In the former, which is faster, you ensure that a reference to Excel is set in the database. This allows you to also use the Object Browser and Intelli-sense, making the coding easier. Using Late Binding makes the process slower still, and should be used if you're not too sure which version of Excel will be on the user's computer.
One thing that you will have to ensure is that there is preventative checking of values and datatypes, rather than using error handling as you can with native Access functions.
Below are various Excel functions, using Early Binding. Some of the functions won't work using Late Binding, apparently if they require the use of a function from the Excel Analysis ToolPak:
Function fXLRoman(intIn As Integer, Optional bytForm As Byte) As String
On Error GoTo E_Handle
Dim objXL As Excel.Application
Set objXL = CreateObject("Excel.Application")
If IsMissing(bytForm) Then bytForm = 0
If intIn < 1 Or intIn > 3999 Then
fXLRoman = intIn
Else
fXLRoman = objXL.WorksheetFunction.Roman(intIn, bytForm)
End If
fExit:
objXL.Quit
Set objXL = Nothing
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function |
Top
Function fXLYield(dtmSettlement As Date, dtmMaturity As Date, dblRate As Double, dblPR As Double, dblRedemption As Double, bytFrequency As Byte, bytBasis As Byte) As Double
On Error GoTo E_Handle
Dim objXL As Excel.Application
Set objXL = CreateObject("Excel.Application")
objXL.Workbooks.Open (objXL.Application.LibraryPath & "\Analysis\atpvbaen.xla")
objXL.Workbooks("atpvbaen.xla").RunAutoMacros (xlAutoOpen)
fXLYield = objXL.Application.Run("atpvbaen.xla!yield", dtmSettlement, dtmMaturity, dblRate, dblPR, dblRedemption, bytFrequency, bytBasis)
fExit:
objXL.Quit
Set objXL = Nothing
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function |
Top
Function fXLDays360(dtmStart As Date, dtmEnd As Date, Optional blnMethod As Boolean) As Long
On Error GoTo E_Handle
Dim objXL As Excel.Application
Set objXL = CreateObject("Excel.Application")
If IsMissing(blnMethod) Then blnMethod = False
fXLDays360 = objXL.WorksheetFunction.Days360(dtmStart, dtmEnd, blnMethod)
fExit:
objXL.Quit
Set objXL = Nothing
Exit Function
E_Handle:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
Resume fExit
End Function |
Top
HOME |
NEW |
TABLES |
QUERIES |
FORMS |
REPORTS |
GENERAL |
API |
DOWNLOADS |
TUTORIAL |
RESOURCES
E-MAIL
Copyright & Disclaimer
|