I have a very large amount of code written in VBA in Access .mdb files, containing forms and connecting to an Access data base. I need to migrate this code to a VB6 application. It isn't practical to rewrite all of it as there is just too much.
Is there a way to call that VBA file from a VB6 form with buttons, that would allow users to launch each one of these VBA modules, via a simple click on its corresponding button? What line of code in VB6, allows to launch a VBA file?
Update
I finally succeeded to figure out how to launch a VBA mdb file from my VB6 form using this instruction :
Call Shell("C:\Program Files (x86)\Microsoft Office\OFFICE11\MSACCESS.EXE C:\presto.mdb /runtime /cmd", 1)
So the VBA project is now launched from the VB6 form, but it crashes, at start up, showing this error "Runtime error ‘3024’ Could not find file ‘C:\db.mdb’".
I feel like I'm half way to thee solution. But I don't know what am I missing here?
Without knowing the structure of your VB6 application and your access files and what they do, it is virtually impossible to give any sort of useful answer.
What you want to do is not easily possible, as stated in comments VBA projects are not designed to work in this way.
However here are some suggestions.
Investigate the RunMacro command and see if this is of use to you.
You could try to use a tool like autohotkey to record mouse clicks where you want them and replay from your VB6 app.
Look at pulling out sections of VBA that are business logic and directly copy and paste code into your VB6 app. This may or may not work depending on the code.
Begin the slow process of rewriting (but don't rewrite in VB6, use VB.Net, C# or do a web app or something at least resembling a 2015 application).
If you want my honest advice begin to migrate to a more modern solution such as VB.NET so you won't have an even worse problem in 2020.
There is NO silver bullet for your problem.
It would be better to design a new form in VB6 with button as you said but also with some text(s)/list(s)/whatever(s) you want to display the results, then you copy/paste VBA code into button_click(s) event(s) to execute it and add to that vba code some output to the text(s)/list(s)/whatever(s) you decided to put on the VB6 form.
Edit : Ok, so taking your comment in consideration perhaps that trick could help avoiding re-write thousands of line code : add in the VBA code a while loop that trigger some Sub or Function on the presence of a flag on the system (a specific c:\temp\fileFlag.txt for example) so as soon as it appear the VBA code is called... if the VB6 control the flag would it do the job ? :)
Related
I have a excel workbook. I need to show it as a HTML page that will be refreshed automatically(say every 2 minutes). For that i need to run the calculations on every sheet and then save as HTML. I will figure out later how to swap the new HTML file for the old one later. I already have a macro in excel that does all the calculations and then saves as HTML every 2 min. All i need to do is call that macro
The problem is that the machine on which all this is to take place is a server machine and thus MS office cannot be installed on it. Thus i cannot use Excel COM Inter-op.
One was is if i could write a .net program or a Vb Script that will run the macro. But a .net program always uses inter-op.
Another way is if i could save the book as a HTML and run the macro in the HTML itself(Dont know if the HTML has the VB A code). Here also i found that i require Visual Basic Editor which i cant install( as it comes with excel only).
I would prefer not to use third party soft wares.
Can any of the above two procedures be implemented in the given conditions? if not is there any other way?
UPDATE:
Have been looking into open XML for a solution and have finally found that open XML 2.0 cannot run excel calculations as the SDK does not have the calculation engine. Does open XML SDK 2.5 have the calculation engine??
In short: You cannot run a macro without Excel installed. Worse: You should not intsall Excel on a Server. It is not recommended and impossible to do this in a stable way.
My best bet would be something like this: Use a :net library which can read xlsx files, like EPPLUS and "translate" this macro into net, which the EPPLUS library as its foundation. Also, this is a lot fastrer than Interop (factor 10 - 100) and fits better into a Server infrastructure with ASP.NET (for example).
Have you considered putting the macro as a .NET Managed AddIn on a shared machine, executing the macro from the server using a shell command to open Excel on the shared machine and the macro runs on Open.
Then you can get the macro itself to export the data to HTML and copy the pages to your server.
As has been said, avoid installing Office on a server (though it has been done in the past, it's never advisable)
Most robust option would be to re-write your vba in .NET as a .NET Managed AddIn - then you have more control over processes, but as has been pointed out, NOT on a Server.
Thanks for the resonses. Found a way to show the excel as HTML in the given conditions.
Just FYI, I used a project known as NPOI. It is the .net version of POI used in java. It allows me to open edit and save excel workbooks without installing excel. plus it is opensource so i customized it according to my requirements.
https://npoi.codeplex.com/
https://github.com/tonyqus/npoi
I am about to release an Access Database application where the UI is used exclusively to interact with the database tables.
Whilst I am interacting with the forms during run time, the Visual Basic Editor appears with a line of code highlighted in debug mode, even when no breakpoints exist and no run time error has occurred.
Has anyone else come across this issue?
I need to ensure that the editor does not appear (under normal operating conditions) while the user is interacting with the application.
Thanks.
Yes, I have had this problem too and it has driven me batty. The quick and dirty way of fixing it is to create blank database and import all of the objects from you old database into the new one.
Are your users working with an .accdb file? (or .mdb, depending on the Access version)
If yes, you should convert it into an .accde/.mde before giving it to your users.
Quote from the link:
Additionally, if the database design needs to be secured to prevent changes, Access databases can be locked/protected (and the source code compiled) by converting the database to a .MDE file. All changes to the VBA project (modules, forms, or reports) need to be made to the original MDB and then reconverted to MDE. In Access 2007 and Access 2010, the ACCDB database is converted to an ACCDE file. Some tools are available for unlocking and "decompiling", although certain elements including original VBA comments and formatting are normally irretrievable.
--> since .accde/.mde files are compiled, it's not possible to view the source code at all.
So the VBA editor can never appear accidentally like you experienced...be it because of a breakpoint, some Stops in the code or some strange breakpoint error like yours.
I dont want the buttons to produces "embed macro" . I tries to press the convert icon to VBA , but still when i use the wizard of buttons it remains to be macro instead of vba procedure , why ?
thank you
Create a button without the wizard.
Right-click on the button and select Build Event. Choose "Code Builder"
The Microsoft Visual Basic IDE should open.
You can also access the buttons events from the Properties Sheet | Event tab.
Beginning with Access 2007, Microsoft built a stronger, more capable macro system that they hope will be able to mostly replace VBA due to the inherent security differences between VBA and macros. There is not, as of yet, a simple way to make Access 2007 or 2010 create VBA code instead of macros when using wizards.
There are two ways some people have had success getting Access to create VBA or converting macros to VBA.
Create an Access 2003 database (.mdb) file. This will create a db compatible with the older versions of Access that don't have the robust macro system and so the code created will be VBA for the most part. The drawbacks of this will be if other functions in your database require the newer Access functions.
After the macro is created, save it as a separate macro rather than an embedded macro. Then in the Design ribbon in the Tools group, the Convert Form's Macros to Visual Basic should work more reliably. The converter is still not very reliable, and if it errors out, there is usually not a way to fix the issue.
I have to deal with a few macros (not VBA) in an inherited Access application.
In order to document them, I would like to print or list the actions in those macros, but I am very dissatisfied by the way Access does it.
Therefore I want to make my little piece of code to list the macros, their instructions list and parameters to the debug window or to a text file.
I googled but did not find much on what object(s) contain those macros.
Any clue to put me on track ?
Have you tried the built-in database documenter that will printout all of the macro code in one shot for you?
Go tools->analyze->documentor
This feature allows you to printout all macro code in the whole application
I have a question for all of you and please any ideas are welcome. We have alot of MSAccess database projects in our repository on which developers work and do there tests in Dev Envoirment on there own. As you all know compiling a databse project is itself a wrong statement, cause you actually need to test your scritps and reports and everything else againt a databse. SO what can we do in TFS build script in case of MSAccess Projects , i am out of ideas. Anybody who has come across this issue and have done something about it, i would really appriciate if you share.
Any example or link which will explain how to go about it would be even more better. Please understand that its an MS Access Projects.
Thanks
BOB
Compiling is not entirely an wrong statement.
In the VBA side, there is a an option under the Debug menu called Compile (I don't believe its truly a compile like in C, but it is pre-parsing your code, etc.). This combined with building an MDE in the GUI side equates to compiling.
Typically, when working with the VBA I'll run the Compile frequently to make sure everything is syntactically correct, etc.
As far as testing goes, once you know that it compiles you still aren't done. If can compile and still be wrong. That's why you need automated test scripts. I'd consider a little script/form within the MDB that you can run manually to open and close all the Reports are concerned about and exercise the code. Actual form functionality would be more tricky to automate, but could be done as well.