Im new to VBA and Im trying to run the following module called quit from the standard switchboard:
Public Function quit()
On Error GoTo quit_Err
DoCmd.quit acSave
quit_Exit:
Exit Function
quit_Err:
MsgBox Error$
Resume quit_Exit
End Function
In the standard switchboard I am using the Command: Run Code and a Function name: quit. However, I get the error message 'There was an error executing the command'. Is there anyway of using the standard switchboard to achieve this?
You're problem is probably caused by the use of "quit" as your function name. It is not a good idea to use "real words" for naming anything you use in programming because they can clash with reserved words.
for example if you simply change the word the name of your function from "Quit" to fQuit" I believe that will solve your problem.
BTW, Calling functions from the switchboard is a very powerful way of using this built in feature. I use the switchboard all the time. I have seen many derogatory comments about the switchboard, however with the use of functions it is very powerful. The only limitation is you cannot pass variables through your functions, so in effect it only works with "Subs" (subroutines) which incidently is what your function is.
Functions invariably pass variables, and can return variables whereas subroutines just carry out a routine.
I have one module called "basSwitchboard" where I keep all the code I call from the switchboard. This is an example of how I use it to open a particular form.
TIP ::::::::::::::::::::::::::::::::::::::::::::::::
When entering information into the switchboard wizard, it is sometimes difficult to remember the particular function you want to call, what I do is enter some dummy text for example "XXXXX" where the switchboard wizard is expecting the function name, then I opened the switchboard table and cut and paste the function (sorry! Sub) name into where the "XXXXX" marker is.
My guess he is confusing the acSave option with the notion it is saving the current record when in fact the acSave option saves any unsaved "object" changes, not "record" changes.
Code:
Public Sub CloseDatabase()
On Error GoTo CloseDatabase_Err
Application.Quit acQuitSaveNone
CloseDatabase_Exit:
Exit Sub
CloseDatabase_Err:
MsgBox Err.Number & " - " & Err.Description
Resume CloseDatabase_Exit
End Sub
Thanks everyone, its working now as the following subroutine:
Public Sub fQuit()
On Error GoTo fQuit_Err
Application.quit acQuitSaveNone
fQuit_Exit:
Exit Sub
fQuit_Err:
MsgBox Error$
Resume fQuit_Exit
End Sub
However, I was still getting the error message until I changed the module name from fquit to converted-fquit. The code that I first produced was from converting a macro, but after reading your posts I have changed it to the code above.
Thanks everyone, its working now as the following subroutine:
Public Sub fQuit()
On Error GoTo fQuit_Err
Application.quit acQuitSaveNone
fQuit_Exit:
Exit Sub
fQuit_Err:
MsgBox Error$
Resume fQuit_Exit
End Sub
However, I was still getting the error message until I changed the module name from fquit to converted-fquit. The code that I first produced was from converting a macro, but after reading your posts I have changed it to the code above.
You should not have a module for every function. Don't put all your eggs [public functions/subs] in one basket but do not create a module for every public function/sub. But Bat17 is correct that you can not name a module and a function/sub with the same name. Why did you name the sub fQuit? What does the f stand for? Also, your error message is lacking the runtime error number for which is very helpful to debugging and trapping for specific errors. In my biased opinion this is a better way to code your shutdown sub...
Code:
Public Sub CloseDatabase()
On Error GoTo CloseDatabase_Err
Application.Quit acQuitSaveNone
CloseDatabase_Exit:
Exit Sub
CloseDatabase_Err:
MsgBox Err.Number & " - " & Err.Description
Resume CloseDatabase_Exit
End Sub
For clarity I thought I should explain the problem I had in detail.
The switchboard will run code, in the form of a function or a subroutine (sub) however you cannot pass arguments through your functions.
However Peter describes a method of allowing you to pass an argument through your function using the Eval Function.
I changed my switchboard code as suggested by Peter, however it would not work I kept getting an error message. Some of my other code called by the switchboard stopped working as well.
I discovered it was because I was calling subroutines (subs) the subs work fine when called from the switchboard, however once you carry out the changes as described by Peter, you have to change your subroutines into functions. This is quite simple process just rename them "Function" instead of "Sub"