How do I force VBA/Access to require variables to be defined? - ms-access

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.

Related

Can I force LibreOffice basic to report an error for undefined variables?

Just as the title says: can I force LibreOffice basic to report an error for undefined variables? I am so used to sane languages that I keep blundering into this: when I misspell a variable, LibreOffice just creates it with an initial value.
Ideally I would like to force this in one place for all macros I use in documents I create. If that is not possible, what is the most reliable and least onerous way to force it?
I have realised the basic answer is quite straightforward – one might say Check the Fing Manual – and I provide it below – but this has caused me enough grief that it seems worth documenting here
Yes, you can do this per module:
Add Option Explicit at the start of every module you write. For details, see the LibreOffice help under Macros and Programming – Using Variables – Forcing Variable Declarations (link for version 6.1).
I think that: no, you cannot do it for all your documents and modules in one place:(

How to make Ms-Access Add-ins? [duplicate]

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.

How do I find where a missing reference is used in MS Access

I have a missing reference listed in my References list (in the VBA Code view). It's a reference to an OCX for a product that we no longer use.
I'm fine with removing(unchecking) the reference, but I'm wondering if that is going to come back to bite me.
Is there a way to find out in which forms/reports it might have been used?
Is removing it and then doing a compile of the MDB sufficient?
I believe if you are using Option Strict, then the compile should catch any issues where you have referenced an object that no longer exists
The compile might not catch it if it's using late binding, but usually it'll catch it in the compile. You can also do some testing by running the recompiled MDB.
If the product is a control, then Access doesn't (by default) let you look at the "source" like that.
Otherwise I would do a search in the VBA code to see where it is created.
eg:
Dim p as New Old_Product
Then do a search for Old_Product across the project.
Depending on how mission critical your application is, I would remove it and see what happens.
I'm assuming that you have decommissioned the old product?
In my experience, Access Applications aren't mission critical. If they go down for an hour (while you try and fix the old reference) it isn't the end of the world.

Access ADP Corupt wont compile

I have a Access ADP Project that has been a continus project for the last 6 years, so it has hundreds of forms and thosands line of VBA code. After a recent update it not wont compile to create the ADE file. It tells me one of the text boxes does not exist but it does. I deleted the for and re imported it from a working backup and still the same error. I have tryed Importing the whole project into a new ADP file and still wont compile, tryed compact and repair and also /decompile
Does anyone no of any tricks or software package to help fix this problem?
I don't work with ADPs, but in MDBs, there are differences between these two syntaxes for referring to controls on a form:
Me.MyControl
Me!MyControl
The first causes Access to create a hidden property that returns the control. The result of this is compile-time checking of references to controls.
The second uses the default collection of the current form and does not provide compile-time checking.
I assume that VBA works the same in ADPs as in MDBs, so why not try converting the offending control references to bang instead of dot? This would eliminate the compile-time checking and might allow the project to compile without having to reconstruct it laboriously.
If that works, I think I would then try deleting the control (to removed the hidden property definition) and add the control back with a new name and then compact (I don't know if ADPs can be decompiled, but if they can, it should be decompiled as well). Theoretically, this should remove the problematic hidden property definition permanently and if that's the cause of the problem, you should be able to revert to the dot operator and get compile-time checking back.
For what it's worth, I've seen too many corruption problems with the dot operator and always use the bang in all my projects. I'm OK with not having compile-time checking of control references.
And, oh, BTW, with the bang you lose automatic Intellisense (which in some cases is a blessing as Intellisense can get in your way in some contexts), but you can invoke a different Intellisense list with CTRL-SPACE. This list is not limited to the control type, but once you start typing, you get the usual autocomplete that jumps you to the appropriate location in the list.
Sometimes it helps to start a new, empty project and then import all forms/reports/modules from the old one.
If birger's approach doesn't do the trick, consider saving each module using the SaveAsText method. Then, as birger suggested, import, but ONLY the forms, and tables. Then, recreate the modules, using the LoadFromText method.
Sometimes, I found out that I need to manual click Debug -> Compile before making ADE files.
This following steps may help for unable-compile ADP file:
Debug -> Compile
Compact/Repair
then make ADE

All possible uses for the Application.SysCmd Method

Is there a place to find all possible uses of the syscmd method in MS Access? I know Microsoft has a developer reference, but I have found there are many other uses for this method that are not listed here.
Access itself provides an interface to the full object model of all libraries in use. In the VBE, hit F2 on the keyboard (or, from the VIEW menu, choose OBJECT BROWSER). Type "syscmd" in the search box and you'll get the full details on it. The variable names are verbose enough to explain just about everything you need to know.
EDIT: The object browser doesn't give you anything but the SysCmd functions that have been documented by assigning named constants. But the recommendation to familiarize yourself with the object browser is a good one, especially if you right click on the CLASSES list and choose SHOW HIDDEN MEMBERS -- you can learn a lot from that.
Here's a comprehensive list, including which Access versions each command applies to, translated into English.
http://www.excite-webtl.jp/world/english/web/?wb_url=http%3A%2F%2Fwww.f3.dion.ne.jp%2F%7Eelement%2Fmsaccess%2FAcTipsUnDocumentedSysCmd.html&wb_lp=JAEN&wb_dis=2
Here are a few of the "undocumented" functions, I know from experience that you can basically run anything that windows can do using syscmd once you understand how to structure the commands from examples like these.
http://www.everythingaccess.com/tutorials.asp?ID=Undocumented-SysCmd-Functions
From google search: syscmd access