This question already has an answer here:
Develop MS Access 2016 AddIn (Ribbon / VSTO) with Visual Studio 2015
(1 answer)
Closed 3 years ago.
I searched a lot for docs to make add-ins for Microsoft Access, Microsoft Docs Access don't have a way to make an add-ins, even visual studio for office developer add-ins don't have Ms-Access Template,
Can I make add ins for Ms-Access?, if so can someone reference a link for how to do that?
Well, you can go to all the trouble to create VSTO add-ins, but if you just need to call + use some .net code from Access, then why bother with all that work?
Just create a COM object in .net, and you can then use + call that code from Access VBA with ease.
So, create a .net class.
Force the project to x86. (assuming access x32)
Check the box [] register for COM inter-op. (this is only required on your development computer).
You also under assembly need to check the box:
[x] Make assembly COM visible
However, above is set by default. So really only ONE check box setting is required here for all of this to work on your part.
Now, just create a class in .net, say like this:
Imports System.Runtime.InteropServices
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class Class1
Private m_Company As String = ""
Public Function MyHello()
MsgBox("Hello world")
End Function
Public Property Company As String
Get
Return m_Company
End Get
Set(value As String)
m_Company = value
End Set
End Property
End Class
In Access, you can now set a reference to the .net class (tools->references in VBA editor).
Of course, once you have this all working, you change your code to late binding in VBA as all good developers do.
With the above class, then in VBA, you see that even intel-sense works for the .net class methods and properties.
So in VBA, note this screen shot:
Note how all of the subs/functions appear as methods of the object in VBA.
So, calling + consuming .net code you write is rather easy and FAR less code then attempting to setup a VSTO add in.
In fact, I recommend the above not only for Access, but for word, Excel etc.
So the above is oh so much less work then messing around with the tools and setup to create an office add-in.
The end result is with VERY simple coding in .net, such code can be consumed by VBA + Access. And the added bonus is that such code can also with great ease be used in Excel, Word and even windows VBS script files. In fact FoxPro, or even say sage 300 accounting system can thus directly consume your .net code in their provided programing languages. So any language or system that supports COM objects (ActiveX) can thus use the above simple add-in.
In other words, the code you write and create can now be used by all of office with ease, and you don’t have to wire up a messy add-in for each office program.
So, to save world poverty and starving children, just create simple class in .net, and consume it from Office as per above.
About the only issue is now distribution. You have to supply the one .dll from .net, and execute regasm on the target machine for this to work. However, that is a one line batch file, and if you using any kind of installer, such installers have provisions for executing (registering) the .net object via regasm anyway. If you created a true office add-in, you will STILL be required to build and setup an installer – and that installer can be a lot of work as compared to a very simple regasm command to register the .net object.
And if you really want to make this simple, you can side load .net and not even have to register the .net object.
At the end of the day the above super simple class example works just fine in Access + VBA, and works with less work and hassle then it will take you to get some VSTO add-in template working with office.
Related
I have an access-vba application that also makes use of word-vba. While running the application on my local machine, it functions well. Once it is moved to others (same versions of access and word) it will crash when it comes to the vba portion of word. Commands such as document.open or .saveas2 fail: Method 'SaveAs2' of object failed for example.
I've also noticed that libraries that I've referenced in the application are required by any other end user. I'm used to just compiling with the libraries and from that point they are always included in the .jar/.exe/etc, but, it seems when you move the application to other's computers it's always trying to recompile?
I'm not well versed in VBA so I'm speculating that my failing word-vba functions are because of a referencing error, any other ideas?
The "libraries" that VBA can reference are actually COM objects, usually packaged as DLL files. They are objects which are dynamically instantiated at runtime (if they aren't already) when requested. They are loaded by Windows into memory and your program uses the COM standard to interact with them, calling methods and getting or setting properties (interprocess communication). There are generally two ways of interacting with them: early binding and late binding.
With early binding, you add a reference to the library while you are still writing code, which allows the VBA IDE to provide autocompletion and some compile-time error checking. You instantiate objects with the "new" keyword and by directly typing the object name. However, early binding requires that you select a specific dll and possibly a specific version of the interface. This can lead to issues if you reference a specific interface version which one of your users doesn't have.
With late binding, you instantiate objects using CreateObject or GetObject, requesting them by name from Windows. Windows will look the name up and return a reference to the object. The variables in your code are simply objects and calling methods is a bit dangerous because the compiler allows you to type in whatever method name you want and provides no compile-time warnings. This has the advantage that as long as you are calling well established methods and nothing new or deprecated, the code will work regardless of the user's version.
As for the error you are getting, you may want to check the version of Office on the user machines - SaveAs2 was added in Office 2010.
A new requirement has come down from the top: implement 'proprietary business tech' with the awesome, resilient Elixir database I have set up. I've tried a lot of different things, such as creating an implib from the provided interop DLL (which apparently doesn't work like COM dlls) which didn't work at all. CPython doesn't like the MFC stuff either, so all attempts to create a Python lib have failed (using C anyway, not sure you can create a python library from .NET directly).
The only saving grace is the developer saw fit to provide VBA, .NET and MFC Interop C++ hooks into his library, so there are "some" choices, though they all ultimately lead back to the same framework. What would be the best method to:
A) Keep my model definitions in one place, in one language (Python/Elixir/SQLAlchemy)
B) Have this new .NET access the models without resorting to brittle, hard-coded SQL.
Any and all suggestions are welcome.
After a day or so of deliberation, I'm attempting to load the new business module in IronPython. Although I don't really want to introduce to python interpreters into my environment, I think that this will be the glue I need to get this done efficiently.
I'm making some significant changes to some VBA code, and some variables are being deleted and/or renamed. It would be a lot easier to find all the locations I need to update if the compiler would complain to me that the variables don't exist instead of creating it on the fly.
How do I force VBA/Access to require variables to be declared?
You need to use Option Explicit at the top of each VBA code module including forms and reports.
You can set this for all future created modules and VBA code behind forms and reports by going into the VBA editor >> Tools >> Options >> Editor tab and ensuring Require Variable Declaration is checked.
From Access 2003 help:
Require Variable Declaration — Determines whether explicit variable declarations are required in modules. Selecting this adds the Option Explicit statement to general declarations in any new module.
I also use camel case when I Dim my variables. ThisIsAnExampleOfCamelCase. As soon as I exit the VBA code line if Access doesn't change the lower case variable to camel case then I know I've got a typo.
Some History on OPTION EXPLICIT and Access VBA
To follow on from Tony's answer, here's some explanation of why there are issues with OPTION EXPLICIT not being on in some Access code modules.
In Access 95 and Access 97 (the first two Office versions with VBA), Access had a different code editor than the other office programs. In Access 2000, Microsoft implemented the VBE from the other Office apps in Access. At the same time, MS chose to make Access VBA modules behave like the modules in the other apps, which defaulted to not having OPTION EXPLICIT.
Thus, in Access 2000, by default, modules were created without OPTION EXPLICIT.
This was, of course, a really stupid design decision on MS's part, and they reversed it later (I can't remember if it was Access 2002 or 2003 that rectified the problem and defaulted to OPTION EXPLICIT in all new modules again). The reason it was dumb (and MS should have known this) is because Access is a DATABASE APPLICATION DEVELOPMENT tool, and thus is operating on data that is strongly typed. Thus, the code environment should be strongly typed by default so that it is in harmony with the data it is working with.
In Excel or Word, the data is not strongly typed, and it thus makes more sense to use variant data types for just about everything, simply to make it easier for everyone. The downside of implementing that by not using OPTION EXPLICIT is that you can end up with typos that automatically intrdoduce new variables [such as the variable "intrdoduce" -- if I was writing this post with OPTION EXPLICIT, that wouldn't have happened without producing a runtime error! :)]. This is a problem with all such languages that work this way (I pull my hair out working in PHP, where variable names can be distinct by case, i.e., $Var is not the same variable as $var; but I digress), but MS made the decision to implement it that way in Word and Excel on the theory that the people writing code there are going to have an easier time of it if they aren't forced to declare their variables.
So, MS made the mistake of making Access's version of the VBE like the other apps, even though there was no logic internal to Access's own purposes that supported that move. And MS then backed out that change and returned to the previous status quo (i.e., OPTION EXPLICIT by default in all modules).
Thus, you will often see apps that began life in Access 2000 that have modules all over the place without OPTION EXPLICIT. When I have to work on such an app, my first task is to implement OPTION EXPLICIT in all modules and then fix the damned thing so it will compile (which is often quite tough, given that the thing was programmed without it).
This question was quite helpful for Excel, you might see if it'll work for Access:
Lost Variables
Essentialy, MZ-Tools will search through your code and tell you what is not being used. The version for VBA can be found here. Use the Review Source Code feature.
I have somewhat experience with what people call excel "macros". The vba code that controls the activeX components, right?
But I still do not know the true meaning of the term macro :). What kind of code is called macro? Is it something like vba or js implemented into a program? Like vba to excel or vba to autocad or js to flex?
Wikipedia has the answer, under Macro.
Definition:
The term originated with macro-assemblers, where the idea is to make available to the programmer a sequence of computing instructions as a single program statement, making the programming task less tedious and less error-prone.
Usage:
Keyboard and mouse macros that are created using an application's built-in macro features are sometimes called application macros. They are created by carrying out the sequence once and letting the application record the actions. An underlying macro programming language, most commonly a Scripting language, with direct access to the features of the application may also exist.
There's lots more, if you follow the link.
When it comes to MS Office products (Excel, Access, Word, PowerPoint etc), "macro" is VBA (Visual Basic for Applications) code.
NOTE: In Access databases macro can also mean another type of automation besides VBA code.
Since you mentioned Excel...I will talk about Excel.
If you suspect (or know) there is an Excel file with macro, try hitting the Alt + F11 key when you have the Excel file open.
You should see a VBA screen open.
This is where you might be able to see the VBA code (ie, if it is not locked behind a password by the programmer who wrote the VBA code).
Macro (VBA code) is used to automated certain tasks.
So in Excel VBA programmer could put a button on a sheet and when you click the button some activities might take place in the Excel file - activities like calculations, copying, pasting, deleting etc..
Read up a little on "recording a macro in Excel".
I spent this morning in trying to figure out where the system.linq.expressions namespace is. The following is what I did:
In VS 2008, Create a new C#/Smart Device/Windows Mobile 6 Professional SDK/.NET CF v3.5/Class Library
Used SqlMetal (in Program Files/Microsoft SDKs/Windows/v6.0A/Bin) to generate the data context.
Added the data context .cs file into the project.
Compile and many errors for missing namespaces: System.Data.Linq, System.Data.Linq.Mapping, System.Linq.Expressions
After some research added System.Data.Linq.dll in c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5 (The dll was not directly listed when I choose to add reference and I used "browse" tab to finally located the one, which is for normal framework)
Compile again, less errors, but still System.Linq.Expressions namespace is missing.
The document says System.Linq.Expressions is in System.Core.dll but it seems my System.Core.dll (located in Program Files\Microsoft.NET\SDK\CompactFramework\v3.5\WindowsCE) contains much less namespace than document says.
Thanks in advance!
The Compact Framework does not support LINQ to SQL. All objects in the documentation for System.Data.Linq confirms this by being completely devoid of the "supported in the CF" icon. For example, look over at the docs for DataTable, which is supported. You'll see a little icon by each supported method/property.
You cannot "add" support by simply referencing a desktop assembly like you did in your step 5. The CF cannot consume full framework assemblies, for a variety of reasons.
Dynamic code generation (Reflection.Emit) is not available in NETCF. What this means is that a lot of features that depend on this is not available, this includes DLR (dyanmic language runtime and hence languages like IronRuby), Linq-to-SQL/
If you just want the Linq.Expressions and you are doing your own stuff with it i.e. not trying to get linq to sql working then you can use the System.Linq.Expression stuff from the db4o guys.
I am using it on my project using linq to objects.
db4o linq implementation