Executing one subroutine without opening Access - ms-access

I would like to know if there is an easy workaround for my following question. I have an access database that have different modules in vba (and of course each module with different subroutines). How can I do to create an icon or an executable file that by clicking on it it runs one of the subroutines of one of the modules without opening access?
The reason of this is because when I am away people need to run some of these subroutines and these users don't have any experience with Access.

You can start Access with a command line option to run a named Access macro. (That means an Access macro object. Some people also call VBA procedures macros, but an Access macro object is different.)
An Access macro has a RunCode method which you can use to run a VBA function. Since the code you want to run is a subroutine, create a new function which calls that subroutine and shuts down Access afterward, and use the function with the macro's RunCode method.
After you have the macro working correctly, test it from a Windows Command Prompt session following this pattern:
"<path to MSACCESS.EXE>" "<path to db file>" -X <macro name>
After working out those details, you can create a desktop shortcut to do the same thing.
However, if your Access operation must be run by you or another user on a regularly scheduled basis (daily, weekdays only, etc.), you could create a Windows Scheduled Task to do it and forget about other users and desktop shortcuts.
Note this suggestion isn't exactly what you requested because it does open Access. But it could close Access after the operation is finished, so perhaps it will be acceptable.

Related

Access VBA: Update Reference Library with AutoExec macro, work's in accdb format but not when compiled?

Here is my VBA code to be executed in MS Access 2010. For simplicity I've removed error handling in this code. Function resides in a module with a different name.
Public Function ReAddLibrary()
Dim accessProj As Access.Application
Dim chkRef As Access.Reference '
Set accessProj = Access.Application
' Check through the selected references.
For Each chkRef In accessProj.References
' If the reference "MyReference" exists then remove.
If chkRef.Name = "MyReference" Then
accessProj.References.Remove chkRef
Exit For
End If
Next
‘ Add back the reference “MyReference” from specified location.
accessProj.References.AddFromFile "Access Database Path"
End Function
Adapted from
https://msdn.microsoft.com/en-us/library/aa221567(v=office.11).aspx
Premise
Users, use Access databases as the front end, naturally there’s an array of them. One thing in common is that they share reference to an accde database which houses shared functions. Naturally these databases are compiled to stop users getting at the VBA/Design mode. The database that is being referenced is also compiled. Currently work is being done to its accdb equivalent and every time it is published as an accde to the database location, an unresolved reference is created. The reference path hasn’t changed location (Nor will it). All recipient Accdb databases work fine, but their equivalent compiled versions throw a generic error when a button linked-subroutine is clicked for example.
If I use the above code as a subroutine linked to an on click event such as “On Load” for a form housing a button in question, this code works perfectly, even compiled. But if I convert this to a function and link it to a macro named “AutoExec,” it will execute the macro when opening the database but will.
Work in a non-compiled database
Will not work when database is compiled
The AutoExec macro route is because I just want this code pasted in once and executed once when the user opens the database, so the reference is recreated and not broken.
Macro
Action: RunCode
Function Name: ReAddLibrary ()
My thoughts
Initially, I’ve looked into late binding as a solution but I’m finding it hard to grasp as I don’t really code in VBA, I primarily code in sql and thus miss Intellisense and syntax highlighting. All examples are for excel workbooks making it difficult to relate. All my users and myself use the same Access 2010 so there is no version confliction. So I don’t know if this is the answer.
http://www.granite.ab.ca/access/latebinding.htm
I considered running a script to recompile all the user’s databases. But, work to the referenced database is incrementally on going and booting people off would be a nuisance.
Why, when the compiled version still retains the correct accde file
path does it not work, when simply the referenced database has been
recompiled again?
Is it programmatically possible to drop a reference (If it exists)
and add a reference, when a compiled version of Access boots up, such
as linking it to a macro named AutoExec?
Is there an alternative way to run the above code without having to
add it to an ‘Event’ for all form objects? Saving me time as I can
centralize the code in one place.
Any Ideas?
The simple and sad answer is that whenever your code and/or a reference is changed, the project has to be compiled.
So, when done, you have to distribute both the project accde file and its external files (those that are updated, in practice all) to the users.

Running Microsoft Access as a Scheduled Task

I am seeking comments on how to schedule auto updates of a database (.accdb) since I am not very comfortable with the process I have set up.
Currently, it works as follow:
Task Scheduler calls a .bat
.bat calls a .vbs
.vbs opens the database and calls a macro
The macro calls a function (VBA Level)
The function calls the update Subroutine
I consider there are too many steps and the fact that it requires 2 external files (.Bat and .vbs) related to the database and stored on the system increase the risk that the procedure would break.
Apparently (but please tell me that I am wrong and how I can change it) .vbs cannot call a subroutine but only a macro. Identically, an access macro cannot call a subroutine but only a function if the user is expecting to enter the VB environment of the database. This is the reason why I called a function (VBA Level) that then calls the subroutine.
Hope some of you know how to shorten the steps and eventually get ride of the .bat and .vbs
To the best of my knowledge the shortest path for a Windows Scheduled Task to "do something useful in Access VBA" is:
Create a Public Function (not Sub) in the database. For example:
Option Compare Database
Option Explicit
Public Function WriteToTable1()
Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.Execute "INSERT INTO Table1 (textCol) VALUES ('sched test')", dbFailOnError
Set cdb = Nothing
Application.Quit
End Function
Create a Macro in the database to invoke the function:
Create a Windows Scheduled Task to invoke MSACCESS.EXE with the appropriate parameters
In the above dialog box the values are:
Program/script:
"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE"
Add arguments (optional):
C:\Users\Public\schedTest.accdb /x DoSomething
A VBS script can call any standard VBA SUBROUTINE with the following:
dim accessApp
set accessApp = createObject("Access.Application")
accessApp.OpenCurrentDataBase("C:\MyApp\MultiSelect.mdb")
accessApp.Run "TimeUpDate"
accessApp.Quit
set accessApp = nothing
Note that the sub TimeUpDate is a standard VBA subroutine. This means no Autoexec macros, and no macros at all - only pure VBA sub calls + this VBS script.
There is a little known trick dating back to the earliest years of access to allow it to run as a process which still works. Access will always on startup look for a macro called "Autoexec". If it finds it it will immediately start executing this macro. I find this is extremely useful if I need to initialise the program before opening forms or, as in the case of the original questioner, run access as a scheduled process with no user I/O.
After beating my head against the wall for about four hours, I finally got this to work:
1) Create a DOS batch file with one line it. The line is composed of three parts a) the full path to Microsoft Access (msaccess.exe), b) the full path of the Microsoft Access database with the code in it, and c) the Access command line argument "/x MacroName". The first two items should be surrounded with quotes. Mine looks like this:
"C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.EXE" "C:\MyPrograms\ProdDB Reports\ProdDB Reports.accdb" /X DailyTestReportsRun
2) Create a macro inside of Access with the name you used in your batch file. It has one command, RunCode, with an argument of the name of a VBA function you want to call. This should be followed by open/close parenthesis "()". I didn't try passing any parameters to the function; I think this would be problematic.
4) Make sure the VBA function you call has a Docmd.Quit command at the end, or that you add this as a second line to your macro. These will make sure that Access doesn't stay open after your process runs.
5) In Windows Task Scheduler, select "create a basic task" (which invokes a wizard). Set the program name to the name of your DOS batch file. There's a helpful check box labeled something like "Open the properties window when I'm finished." Check that so you that go to the properties window.
6) Set the task to run regardless of whether the user is logged on or not. Also check on the "Run with highest privileges" box, which one friend on here suggested.
You can now test everything by right-clicking the scheduled task and selecting the Run command.
I liked Albert Kallal's script and tried it. Everything worked great until I tried to schedule it. Then, for some mysterious reason the scheduler would not kick it off.
none of the above work
ms access DB with task scheduler will not work as to open the db run code and quit the application
the solution I found is to avoid task scheduler and have the ms access db open all time and have a timer in msaccess do the job

MS Access Compile and Run

I am new to MS Access. I am building a simple project and I already have created the tables, forms and queries. My question is, how would I compile and run my program ? Is there "F5-like" to run my project on MS Access ? Or how am I going to do this? Thank you.
In other Microsoft environments, F5 is the shortcut key for "Start Debugging", or "Run", or words to that effect. When the application starts it performs a default action:
Windows forms-based applications will display the startup form
Windows console applications will start running at static void Main()
ASP.NET applications will display the default page (Default.aspx).
Unfortunately, while the VBA development environment does have an F5 shortcut key to run code (and it can be very handy during development), it doesn't mean the same thing as it does in the other environments listed above.
To "run your project" in Access you need to manually perform whatever action you have specified as the default action when the database is opened. That is normally one of two things:
If you have created a Macro named AutoExec then Access will run that, or
If you have specified a startup form (see screenshot below) then Access will open it.
So, to launch your Access project simply open the startup Form or run the AutoExec Macro. (Or, you could always just close the database and re-open it, and let Access perform the default action for you.)
make sure you have ODBC access support to run your program

Running MS-Access macro using batch file when not logged in

I have the following set up on a server
Two MS-Access queries
Exported the data from the queries and saved the export steps
Created a macro to run these export steps then exit MS-Access
Created a batch file to open ms-access and run the macro
This works fine and dandy if I log into the server and double click the batch file, but if I set up a scheduled task to "run even if not logged in" then the ms-access macro doesn't seem to run....so it seems like access is never ran.
Does anyone have any ideas? Can you do this sort of process when "not logged in"? Is there something else I need to do? I am really stuck and need to have this automated.
Thanks!
My best guess is that you are trying to access a mapped network location that is not available.
Many networks have logon scripts that map network drives to local drive letters. These logon scripts are not run when you set up a scheduled task to "run even if not logged on."
So if you either:
A) are exporting your query results to a file on a mapped network drive (ie, N:\MyExport.txt) or,
B) have one or more tables in the source of your query that are linked to .mdb's on mapped network drives (ie, CurrentDb.Tables("MyTable").Connect = "N:\MyBackend.mdb")
then the process will fail.
If you are expecting to see an error message when you log on, you won't see one because the Access process is not visible when run through a scheduled task. However, if you go to Task Manager you should see an MSACCESS.EXE process listed.
If this is the problem there are two possible workarounds:
Use UNC paths (ie, \\fileserver\folder\MyExport.txt instead of N:\MyExport.txt)
Set up your scheduled task to run a batch file that:
maps the network drives using NET USE commands
opens your MS Access program with the appropriate command line arguments

ms-access: doing something on database open

is it possible to run a sub or function as soon as the user opens an access database file? if so, how?
Create your function:
Public Function DoSomething()
' do stuff '
End Function
Then create a macro with the run code action which calls your DoSomething function. Name the macro autoexec. Then, every time the data base starts up, it will run your autoexec macro.
Another thing you can do is set a form to open whenever the database starts. You could then call your DoSomething function from a form event (on open, or on load).
Choose one of those approaches. Either way, if you ever want to start the database without DoSomething running, hold down the shift key as the database opens to bypass your automatic startup routine.
You could open a hidden form on start up like in Access 2007 Startup.
This is also possible in older Access version.
You may use this hidden form for logging or other 'system'-related tasks.