How to use the "Locals" window in VBA IDE - ms-access

When I'm coding VBA in Access 2003, I keep the Immediate window visible and I use Debug.Print and Stop to solve my challenges. But I've never known how to get help from the "Locals" window. I understand what's in there for the most part. But it usually seems like I'd have to dig around for the item I want, and it could take a while to find it in all those folded structures.
(And "Locals" doesn't bring up anything from the built-in VBA help files. Maybe I'm missing a file ...)
I bet I could start using Locals, if I knew what it's good for in a practical way. Do do you have an explanation or anecdote that would clue me in?

Locals provides a couple of benefits: You can F8 through the code and watch how a variable changes by looking at the locals window as opposed to issuing debug.print statements or hovering over the variables. You can see the contents of an array a heck of a lot quicker than issuing lbound and ubound and a bunch of other statements to check values. Not just arrays either, but any complex data structure such as a custom class module.

Related

SSIS 2012 - script task won't run second time (unless debugging)

I'm getting a really tough error in SSIS 2012.
I am just running in SSDT.
I have a script task inside a For...Each block.
It runs fine the first time it is reached.
The second time it is reached, I get a generic "Exception thrown by object of invocation error", attributed to the script, at the script task.
It is a small script, all inside Main(), and with a Try...Catch block.
I am not hitting the Catch, which adds custom text.
It seems like it is behaving as if it never enters the Script...
except
if I actually set a breakpoint in it.... in which case it runs fine,
whether I step line-by-line or just hit F5.
I know this isn't terribly specific, but I'm hoping someone has seen this.
Has anyone seen anything like this before?
As mentioned, I have tried debugging (obviously), but then I don't get any error.
I have tried changing my variable access from the basic to through VariablesDispenser.LockOneForRead, in case it is something with variables that happens before Main().
I think I got all the places the variables are used in the loop, but that didn't help.
Because this was so killer, I'm going ahead and answering it.
It was actually an un-"declared" variable, but in my Catch block.
Copy-paste error :/
I was using a variable as
"Dts.Variables["TaskName"]"
in the Catch block, but I had not selected it in the Script Task window.
I have no idea why it didn't give me the specific "not found in collection" error.
I have certainly run into this before and seen that. :/
Just ran into that and it was a bear to figure out.
What it was was that I had a static variable (actually a singleton class) defined. Evidently, SSIS does NOT re-initialize a program on second and subsequent invocation, but holds the image and simply re-launches at its entry point.
My Singelton class (and I've verified for several static variables now) does NOT get re-initialized. It still exists. The issue was that it was created with the Dts Variable set that existed on first invocation of the script. Since it's "self" values was not null it never re-instantiated.
When I recognized what was happening, it was of course easy to fix, but one gets used to a stand-alone environment where every program instance has its static values null or set with a static initial value. We presume automatically that a new "run" of the program will likewise have its global spaces "clean" .... in point of fact I'm fairly sure that was what I read as part of the C# "contract" that I'd never need to worry about historical cruft in memory spaces for variables.
Well it turns out that that "contract" is about as good as any Microsoft will make you sign.
It's actually a mixed blessing. Knowing that it happens I can use it to save a lot of overhead in scripts invoked in loops ... but as it's not well, or perhaps un- documented I'll need to be careful to have work-arounds and default loading tests if it turns out not to be true in some future release or version.
(Be gentle in your criticism... I'm new to SSIS. Not so new to program paradigms. CICS mainframe programs would re-init global spaces unless you did things in the linkage to signal it not to ... if you're going to re-invent wheels at least look at old wheels).
-- TWZ

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

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.

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