ADezii 8,834
Recognized Expert Expert
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: - Created a Public Function called fStripNonPrinta bleCharacters() 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.
- Create an Instance of Excel and assign it to an Object Variable.
- 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.
- Set the return value of the Excel Function to the return value of the Access Function.
- 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. - The actual Function which does the dirty work:
- Public Function fStripNonPrintableCharacters(strStringToStrip As String) As String
-
-
'Make sure to 1st set a Reference to the Microsoft Excel XX.X Object Library
-
Dim objExcel As Excel.Application
-
-
Set objExcel = CreateObject("Excel.Application")
-
-
fStripNonPrintableCharacters = objExcel.Application.Clean(strStringToStrip)
-
-
End Function
- The Function call and logic for proving its success:
- Dim strStringWithNonPrintables As String, strCleanedString As String
-
-
Dim intCharCounter As Integer, strTempUnclean As String, strTempClean As String, strProveClean As String
-
-
'Create a String with 5 Non-Printable Characters contained in it
-
strStringWithNonPrintables = Chr(7) & "ne" & Chr(13) & "a" & Chr$(10) & "to" & Chr(9) & "!" & Chr(5)
-
-
'Code Segment not required, used only to demonstrate the presence of Non-Printable characters in String
-
strTempUnclean = "|"
-
-
For intCharCounter = 1 To Len(strStringWithNonPrintables)
-
-
'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.
-
strTempUnclean = strTempUnclean & Asc(Mid$(strStringWithNonPrintables, intCharCounter, 1)) & _
-
"-" & Mid$(strStringWithNonPrintables, intCharCounter, 1) & "|"
-
Next
-
-
'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
-
Debug.Print strTempUnclean
-
-
'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.
-
strTempClean = fStripNonPrintableCharacters(strStringWithNonPrintables)
-
-
Debug.Print strTempClean '==> Prints neato!
-
-
'Code Segment not required, used only to verify that Non-Printable characters were removed from String
-
strProveClean = "|"
-
-
For intCharCounter = 1 To Len strTempClean)
-
strProveClean = strProveClean & Asc(Mid$(strTempClean, intCharCounter, 1)) & _
-
"-" & Mid$(strTempClean, intCharCounter, 1) & "|"
-
Next
-
Debug.Print strProveClean
- String before running through Clean() Function (ASCII Codes & Characters):
-
-
|7-|110-n|101-e|13- from Line #19
-
|97-a|10-
-
|116-t|111-o|9- |33-!|5-|
- Output of String after running through Clean() Function (Characters/ASCII Codes/Characters)
- neato! from Line #24
-
|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.
5 15139 FishVal 2,653
Recognized Expert Specialist - Public Function fStripNonPrintableCharacters(strStringToStrip As String) As String
-
-
'Make sure to 1st set a Reference to the Microsoft Excel XX.X Object Library
-
Dim objExcel As Excel.Application
-
-
Set objExcel = CreateObject("Excel.Application")
-
-
fStripNonPrintableCharacters = objExcel.Application.Clean(strStringToStrip)
-
-
End Function
The weak side of fStripNonPrinta bleCharacters function is that it is extremely slow as soon as it creates Excel.Applicati on 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 -
strResult = Excel.Application.Clean("......")
-
will create static Excel.Applicati on 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.
ADezii 8,834
Recognized Expert Expert
The weak side of fStripNonPrinta bleCharacters function is that it is extremely slow as soon as it creates Excel.Applicati on 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 -
strResult = Excel.Application.Clean("......")
-
will create static Excel.Applicati on 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).
FishVal 2,653
Recognized Expert Specialist 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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 and extract
information from specific worksheets and cells.
I'm not really sure how to get started with this process.
I ran the COM Makepy utility from my PythonWin (IDE from ActiveSTate),
|
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 find a
free tool that can do the above and therefore I'm forced to do
everything from scratch.
Till now have developed two different versions of the programs and
both somehow work but still have a small problem that I can't
understand.
|
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 my Access code, Excel is
terminated properly.
I have seen discussions on this subject by can't find the answer to my
|
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 everything I can find on COM Interop
and .NET. I've also tried many of the 'Walkthroughs' on the MSDN site
relating to COM add-ins, .NET and Office XP but am unable to get even these
working in Excel 2002 or Word 2002.
I've installed the Office XP...
|
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 I use:
Slope {=LINEST(H72:H94,I72:I94,TRUE,TRUE)}
SE {=LINEST(H72:H94,I72:I94,TRUE,TRUE)}
RSquared {=LINEST(H72:H94,I72:I94,TRUE,TRUE)}
F-Stat {=LINEST(H72:H94,I72:I94,TRUE,TRUE)}
| |
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 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):
Function fXLCoupDayBS(dtmSettlement As Date, dtmMaturity As Date,...
|
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 in a database. Is this possible to use Excel functions in Access?
|
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 recently (and I know others have come across this recently too) is rounding up and down. I know the Excel ROUNDUP() function rounds away from zero rather than upwards specifically, but is still useful in most circumstances.
To allow an Access...
|
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 database) krijg ik de foutmelding dat er een
'ongedefinieerde functie' in de access database aanwezig is.
Kan iemand mij op weg helpen dit probleem op te lossen?
Alvast bedankt voor de reactie,
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |