473,421 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,421 developers and data experts.

How to Use Excel Functions in Access

ADezii
8,834 Expert 8TB
Periodically, the same or similar question appears in our Access Forum: How can I use Excel Functions within Access? For this reason, I decided to make this Subject TheScripts Tip of the Week. In order to demonstrate the use of Excel Functions within the context of Access, I performed the following steps in sequence:
  1. Created a Public Function called fStripNonPrintableCharacters() which will encapsulate the logic for executing the Excel Function. This Access Function will accept a single Argument (STRING) upon which the Excel Function will operate. The Excel Function chosen will be introduced shortly.
  2. Create an Instance of Excel and assign it to an Object Variable.
  3. Execute the Excel Function via the Object Variable with the single Argument passed to the Access Function. This Argument will consist of a String Value that is predefined.
  4. Set the return value of the Excel Function to the return value of the Access Function.
  5. Destroy the Instance of Excel as referenced by the Object Variable, then set the Variable to Nothing in order to release all resources assigned to it.
Careful thought went into the selection of an Excel Function to use for this Tip. I wanted to use a Function that would accept a single Argument for simplicity, it had to have no equivalent counterpart in Access, and it had to have a practical application. I decided to use Excel's Clean() Function which accepts a single String Argument and removes all Non-Printable characters from the String. The Non-Printable characters that I am referring to are ASCII Control Characters with ASCII numbers ranging from 0 to 31. These characters are low level computer codes and are typically used to control some Peripheral Devices such as Printers. Within this range (0 - 31) are such familiar fellows as Line Feeds, Form Feeds, Carriage Returns, Escape Character, Start and End of Text markers, Acknowledge, etc.

Enough of the Overview already! I'll now display the relevant code and subsequent output. The code is well documented, but if you need any clarification on any aspect of this Tip, please feel free to ask.
  1. The actual Function which does the dirty work:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fStripNonPrintableCharacters(strStringToStrip As String) As String
    2.  
    3. 'Make sure to 1st set a Reference to the Microsoft Excel XX.X Object Library
    4. Dim objExcel As Excel.Application
    5.  
    6. Set objExcel = CreateObject("Excel.Application")
    7.  
    8. fStripNonPrintableCharacters = objExcel.Application.Clean(strStringToStrip)
    9.  
    10. End Function
  2. The Function call and logic for proving its success:
    Expand|Select|Wrap|Line Numbers
    1. Dim strStringWithNonPrintables As String, strCleanedString As String
    2.  
    3. Dim intCharCounter As Integer, strTempUnclean As String, strTempClean As String, strProveClean As String
    4.  
    5. 'Create a String with 5 Non-Printable Characters contained in it
    6. strStringWithNonPrintables = Chr(7) & "ne" & Chr(13) & "a" & Chr$(10) & "to" & Chr(9) & "!" & Chr(5)
    7.  
    8. 'Code Segment not required, used only to demonstrate the presence of Non-Printable characters in String 
    9. strTempUnclean = "|"
    10.  
    11. For intCharCounter = 1 To Len(strStringWithNonPrintables)                                               
    12.  
    13. 'Build a String containing the ASCII Codes for all characters within the String as well as the character itself delimited by a '|'. Remember Non-Printable Characters have ASCII Codes between 0 and 31 and in this case will be in positions 1, 4, 6, 9, and 11.          
    14.   strTempUnclean = strTempUnclean & Asc(Mid$(strStringWithNonPrintables, intCharCounter, 1)) & _ 
    15.                    "-" & Mid$(strStringWithNonPrintables, intCharCounter, 1) & "|"                     
    16. Next                                                                                                   
    17.  
    18. 'Dump this String to the Immediate Window to see the Non-Printable Characters represented by ASCII Codes 7, 13, 10, 9, and 5. The Characters will also be represented. We'll ompare this afterwards, when this String is run through Excel's Clean() Function                             
    19. Debug.Print strTempUnclean                                                                     
    20.  
    21. 'Run the String with Non-Printable Characters through the Clean() Function, print out the result, then prove it worked by using the same logic that was used on the original String.
    22. strTempClean = fStripNonPrintableCharacters(strStringWithNonPrintables)
    23.  
    24. Debug.Print strTempClean       '==> Prints neato!
    25.  
    26. 'Code Segment not required, used only to verify that Non-Printable characters were removed from String 
    27.  strProveClean = "|"                                                                                    
    28.  
    29. For intCharCounter = 1 To Len strTempClean)                                                             
    30.  strProveClean = strProveClean & Asc(Mid$(strTempClean, intCharCounter, 1)) & _
    31.                "-" & Mid$(strTempClean, intCharCounter, 1) & "|"                                      
    32. Next                                                                                                                
    33. Debug.Print strProveClean                                                                      
  3. String before running through Clean() Function (ASCII Codes & Characters):

    Expand|Select|Wrap|Line Numbers
    1.  
    2. |7-|110-n|101-e|13-        from Line #19
    3. |97-a|10-
    4. |116-t|111-o|9- |33-!|5-|
  4. Output of String after running through Clean() Function (Characters/ASCII Codes/Characters)

    Expand|Select|Wrap|Line Numbers
    1. neato!        from Line #24
    2. |110-n|101-e|97-a|116-t|111-o|33-!|        from Line #33
NOTE: Don't let all the code in Item #2 fool you, the only lines needed are actually 3 and 22. All the other lines of code generate the Strings consisting of the ASCII Codes and Characters both before and after the Function call. They can be elimnated.
Oct 21 '07 #1
5 15029
FishVal
2,653 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Public Function fStripNonPrintableCharacters(strStringToStrip As String) As String
  2.  
  3. 'Make sure to 1st set a Reference to the Microsoft Excel XX.X Object Library
  4. Dim objExcel As Excel.Application
  5.  
  6. Set objExcel = CreateObject("Excel.Application")
  7.  
  8. fStripNonPrintableCharacters = objExcel.Application.Clean(strStringToStrip)
  9.  
  10. End Function
The weak side of fStripNonPrintableCharacters function is that it is extremely slow as soon as it creates Excel.Application process when called and terminates it on exit when private object variable goes out of scope.
Query based in this function will be veeeeery slow.

On the other hand call like
Expand|Select|Wrap|Line Numbers
  1. strResult = Excel.Application.Clean("......")
  2.  
will create static Excel.Application process and all subsequent calls will use it without load time charge. The process stays in memory until Access is closed. Its not very good, but better than query returning 2 records/sec. :) Maximum, disc format cures everything.

P.S. Good tip anyway.
Oct 21 '07 #2
ADezii
8,834 Expert 8TB
The weak side of fStripNonPrintableCharacters function is that it is extremely slow as soon as it creates Excel.Application process when called and terminates it on exit when private object variable goes out of scope.
Query based in this function will be veeeeery slow.

On the other hand call like
Expand|Select|Wrap|Line Numbers
  1. strResult = Excel.Application.Clean("......")
  2.  
will create static Excel.Application process and all subsequent calls will use it without load time charge. The process stays in memory until Access is closed. Its not very good, but better than query returning 2 records/sec. :) Maximum, disc format cures everything.

P.S. Good tip anyway.
Excellant point, FishVal, thanks for making me aware of this shortcoming. My intent, which I obviously should have stated, was for a 1 time/single use only for demonstration purposes only. In this scenario there would have been no performance penalties, but in all reality, this functionality would be used multiple times, probably involving Text File Imports. Again, thank you for your input.

BTW, objExcel could be declared Publically in a Standard Code Module as opposed to a Static Declaration within the Procedure for multiple use of the Function. I really not sure which approach would have the greater overhead. If I were a guessing man, I'd have to go with the Static Declaration as being the costlier. What's your opinion?

P.S. - I'm sure glad that someone actually reads these Tips. (LOL).
Oct 23 '07 #3
FishVal
2,653 Expert 2GB
BTW, objExcel could be declared Publically in a Standard Code Module as opposed to a Static Declaration within the Procedure for multiple use of the Function. I really not sure which approach would have the greater overhead. If I were a guessing man, I'd have to go with the Static Declaration as being the costlier. What's your opinion?
Actually I don't now what is better:
1) to declare global object variable on startup or before query runs
2) to let Access automatically create it.
Anyway I would go with the last option as soon as the first doesn't provide any additional safety for the cost of additional efforts.

Just like an infant joke.
Q. Who is better? King or emperor?
A. Both 'em worse.

P.S. - I'm sure glad that someone actually reads these Tips. (LOL).
I read each. :)

Regards,
Fish
Oct 23 '07 #4
puppydogbuddy
1,923 Expert 1GB
P.S. - I'm sure glad that someone actually reads these Tips. (LOL).
ADezii,
More people read this stuff than you think! Excellent tip/demo. Do have a tip index that would enable me or another interested party to go back and read previous tips of interest?

Also here are additional references on this topic if anyone is interested.

http://support.microsoft.com/kb/198571
http://www.fabalou.com/Access/Queri...l_functions.asp

http://www.cpearson.com/excel/ATP.htm
an add-in for scientific and engineering calculations
Oct 27 '07 #5
ADezii
8,834 Expert 8TB
ADezii,
More people read this stuff than you think! Excellent tip/demo. Do have a tip index that would enable me or another interested party to go back and read previous tips of interest?

Also here are additional references on this topic if anyone is interested.

http://support.microsoft.com/kb/198571
http://www.fabalou.com/Access/Queri...l_functions.asp

http://www.cpearson.com/excel/ATP.htm
an add-in for scientific and engineering calculations
  1. Articles
  2. Access
  3. Full List of Articles and Code in this Section
  4. Tips on VBA and Access Programming
    http://www.thescripts.com/forum/thread632608.html
Oct 27 '07 #6

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

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: v_verno | last post by:
Good day, I have a web page that shows info retrieved from a MySQL db and I would like to convert all these data into an .XLS file. Have searched the net high and low but seems that I'm unable to...
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...
10
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied...
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: billelev | last post by:
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...
2
by: paigenoel | last post by:
I was wondering how you can add a function (acos) which exists in MS Excel into MS Access 2003? I need several functions, one of which is ACOS. My formula work well in Excel and now I need to use it...
0
NeoPa
by: NeoPa | last post by:
Many of us have noticed that there are some very useful functions available to you when using Excel, but these same functions are not available, as standard, in Access. A particular issue I had...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.