The Linux Page

Variables and Functions in VBA not like in C++!

I have been writing C/C++ code for... well... over 10 years.

I'm always surprised when I use another language and some basic rules are completely different.

With MS-Access, I created a module, added functions and tested running the code. I use the Option Explicit (so should you) so whenever a variable is not defined, it breaks (otherwise, it just creates a local variable automatically and in most cases that's not what you want...) and I got that error telling me that my variable did not exist. I explicitly created a Global variable not even 10 lines before the function!!! What's wrong?

Pay attention here:

1. Declare Options

2. Declare Variables

3. Declare functions and subs

Yes. You cannot declare a global variable, a function, a global variable, a function, etc. You have to declare all your options, all your variables, then all your functions and subs (the order of each set is important, within the set, however, you do whatever you want)

So if you have an error tell you that a variable does not exist, try moving it at the top of your file.

And if you have a function that does not seem to do what it needs to do, look into adding Option Explicit if you do not have it yet.

Note that this is quite confusing because within a function or sub you can create new variables (with Dim) anywhere in your code...

SMS From Me Logo

SMS From Me

To automatically start one on one conversations with your online leads.