1

I'd like to do some auditing of VBA programs in Access 2010 and Excel 2010. One of the main concerns is the library references that are required for particular programs. Is there a way to programmatically determine which references are required for a particular program?

I did a test in Access. I wrote some code which required a reference to the Excel library. I made sure that I didn't have the reference and compiled the code. I received no errors. My hope was that I would get errors and I would be able to compile programmatically and save the associated error that mentioned lacking the Excel reference.

Another thought I had to achieve the goal, is to parse the VBA in each module. This seems like it would be a pretty big task.

2
  • I'm guessing it's difficult for Access to know exactly what library it's missing, just from the code. Dec 8, 2014 at 5:39
  • It is possible to loop through the references in a project using VBA.
    – Rory
    Dec 8, 2014 at 7:59

2 Answers 2

2

"Is there a way to programmatically determine which references are required for a particular program?"

No. Access can throw a compile error when it finds something it doesn't understand in your VBA code. But it can't identify whether that something is available in an unreferenced object or type library.

Consider this example:

Dim xlApp As Excel.Application

That statement will not throw a compile error if your project includes a reference to the Excel Object Library. But, without the reference, Access will complain "User-defined type not defined" when it encounters Excel.Application. It can't tell you "Include a reference to the Excel Object Library to resolve this problem."

The same thing happens when your code includes an Excel named constant, such as xlEdgeTop. It is OK with the reference. Without the reference, Access hasn't a clue about that name.

Incidentally, the result you reported for your test was only possible because your code module did not include Option Explicit in its Declarations. You should always include Option Explicit in all your code modules so that Access can warn you when it doesn't recognize something.

2
  • Here is the code I tested: mySum = Excel.WorksheetFunction.Sum(100, 200) MsgBox mySum. I tried the following too: mySum = Excel.Application.WorksheetFunction.Sum(100, 200) I received no compile errors.
    – Tyrone
    Dec 9, 2014 at 0:56
  • See Tom's answer regarding Option Explicit. Your code would complain if you had included that declaration but not a reference to Excel.
    – HansUp
    Dec 9, 2014 at 1:54
1

In your situation, I remove the references one at a time, compiling after each removal. If that is not working for you, there are a couple of reasons why:

It is important to have Option Explicit at the top of every module, including the code behind forms, reports, userforms, worksheets etc. Without Option Explicit, many undefined symbols won't be caught at compile time.

It is also important to declare object variables with the actual object type, rather than as Variant or as Object. For example, this code compiles fine but it won't run:

    Dim RS
    Set RS = CurrentDb.OpenRecordset("MyTable")
    If RS.EndOfFile Then MsgBox "No data"

Changing Dim RS to Dim RS As Recordset then causes the non-existent .EndOfFile property to be detected at compile time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.