views:

331

answers:

6

Although I've done VBA projects within a single application for both MS Access 2007 and Excel 2007, I haven't automated multiple applications at the same time. The generalized project is to open access, run some update queries that appends data to various tables. Then Excel needs to get the data. Some formating changes are needed in Excel, such as grouping that doesn't automatically change the date range. Finally, I plan to build it out such that the excel file will be emailed automatically.

Some parts of this are clear how to accomplish it, such as Excel will be getting the data by ODBC from Access. But where should the master VBA live? I could have a button in Access that would start running Access VBA, but is it a good practice to have the Access VBA start manipulating Excel? Does that make it difficult to debug?

A: 

Not to sound like a programming snob, but is there a reason you would do this in Excel and Access versus Visual Studio and MSDE?

If you take that route it opens up a LOT more possibilities and makes debugging and programming a lot simpler.

Visual Studio makes debugging a breeze, and with MSDE you run into a lot fewer problems with database performance and file locking.

If you're set on using Excel, you might still look into VSTO (Visual Studio Tools for Office) for some level of integration with Visual Studio.

Mxyzptlk
I'm distributing Excel files to my customers. It's trivially easy to have Access act as a front end to a line-of-business software based on the Advantage Database and then have Access talk to that. The amount of code we're talking about is probably a dozen to two dozen lines. So for this I don't want to set up other environments or deal with VSTO.
Knox
you're right of course but this means that he needs Visual Studio (maybe he or his company doesn't have it) unless you can do this with the VS Express-edition
moster67
Well in that case I'd probably recommend going with Access?You wouldn't have to worry about macros getting sent around in Excel files, and Access is already your established front end for your Advantage Database. Seems like that way you could limit yourself to one place with VBA code to debug and maintain.of two
Mxyzptlk
-1 I think the suggestion that VS and MSDE (do you mean SQL Server Express?) would be faster than building in Access with VBA is just ludicrous. And debugging in Access couldn't be easier.
David-W-Fenton
Depending on what you're doing, development can absolutely be faster, more stable and far more scalable.If you're just looking for a simple script with a few dozen lines of code, then sure... go with VBA and you'll be done in a day instead of a week. But if you really want a mature product built for an enterprise of hundreds or thousands of users, you are going to hamstring yourself with Access and Excel.
Mxyzptlk
The concept of automating Office apps by definition negates the assumption that you're building an enterprise app for thousands of users. There is nothing in the original question to suggest anything of the sort.
David-W-Fenton
Unfortunately not everyone shares your opinion about using Office apps to build apps for thousands of users. I've encountered several in my career and had to migrate a few to vb and web applications. There is also nothing in the original question to suggest it is NOT being developed for that purpose. I didn't submit my answer to pick a fight with an MS Access zealot.
Mxyzptlk
A few dozen lines of code? Some of my apps have tens of thousands of lines of code. As well as dozens of tables. One Access app had 70K lines of code and 150 tables. And they work well.
Tony Toews
@Tony Toews: ...and thousands of concurrent users? Thought not.
onedaywhen
Ouch! Thousands of users, not for this. My application is for one user (me) to automate the distribution of excel to dozens of users.
Knox
A: 

I haven't done VBA for ages although Access was my entry-point into the world of programming. Moving from Access 97 to Visual Basic was easy and I remember that I wrote a lot of stuff using Access more like a VB-form for doing many kinds of tasks (not necessarily database-stuff).

This is the reason why I think you should stick to Access and from there, with help of VBA, do your stuff in Excel etc..

Good luck

moster67
A: 
Allan Simonsen
+4  A: 

To get started from Access, add a reference to the Excel object library. Then use the object browser to familiarize yourself with how the Excel object hierarchy looks from within Access. It is going to be somewhat difference, because the top-level object in Excel code is implicit (as it is in Access), and has to be explicitly referenced when coding in Access.

The Access Developers Handbook has excellent chapters on automating the rest of Office from Access.

Last of all, it's best once you've coded using the reference to the other app's automation library to help you program, you want to switch to late binding so you can remove the reference. This means not using any of the external library's specific data types (you mostly use plain object variables) and using none of the constants defined in the external library. My production code with late binding usually includes the early binding version commented out, alongside the late binding version.

David-W-Fenton
Thank you, David. I don't find a book with exactly that title on Amazon; can you give me a pointer? I appreciate it.
Knox
Visit http://www.mcwtech.com/Books.aspx Yes, the last version is for Access 2002 but it's still very relevant.
Tony Toews
A: 

I have recently done something quite similar to this, and have found that I can output HTML with built-in CSS for formatting that loads quite nicely into Excel. I used Access to allow users to build their required output, only opening Excel to display the results. You may find that HTML output makes for nicer emails.

Remou
+2  A: 

Do all the work in Access VBA. See the following URLs for some sample code

Modules: Sample Excel Automation

Modules: Transferring Records to Excel with Automation

Also note that if you are dealing with multiple versions of Excel late binding becomes a necessity. Late binding means you can safely remove the reference and only have an error when the app executes lines of code in question. Rather than erroring out while starting up the app and not allowing the users in the app at all. Or when hitting a mid, left or trim function call.

This also is very useful when you don't know version of the external application will reside on the target system. Or if your organization is in the middle of moving from one version to another.

For more information including additional text and some detailed links see the Late Binding in Microsoft Access page.

As far as emailing goes there are a number of options at the Microsoft Access Email FAQ

Tony Toews
Very informative. Thank you.
Knox