Calling a Public Sub from a form in MS Access - ms-access

Very simple problem: I have a Public Sub (in a module) that I want to call from a button on a form. The name of the function I want to call from the module is GenerateKML.
I've read this post:
How do I call a VBA Function into a Sub Procedure
And tried all of the suggested methods, none of which are working for me. There may be a problem with my code, but when I'm in Code view (editing the module) and press the 'play' button the code runs properly (a KML file is created).
If I use the second method suggested (call a subroutine in a module from a form) I get this error message:
Compile Error
Expected variable or procedure, not module
And if I use the third method (call a subroutine from a form without using an event procedure) I get this:
The expression On Click you entered as the event property...: The expression you entered has a function name that [my DB name] can't find.
So I suspect there's something wrong with how I'm calling the code I want to run.
This is how the code for my module starts:
Option Compare Database
Public Sub GenerateKML()
'
' GenerateKML Macro
' Macro recorded 26/09/2006 by simon_a
' Adapted and imported to Access by SAA
' 03 aug 2007 - v3.0 - 2007 08 06 19 24
'
' DECLARE VARIABLES
Dim filename As String
Dim docname As String

Maybe you have named your code modules the same as the procedures within it.
(just a thought)
i.e. the sub GenerateKML, sits in a module you have named GenerateKML. This creates a conflict & resulting error message.

If you have named your module GenerateKML as well as your sub, you need to call it using:
GenerateKML.GenerateKML arguments
(or just rename one or the other, which is probably easier)

try renaming your module with a mod prefix: modGenerateKML.
You don't reference the module name from forms, just the name of your public sub or function.

Related

Can't capture /cmd value in Access Macro

MS Access (2007 - 2016) in Office 365
I'm trying/failing to capture a value passed into Access from the command line using the Command() function in a Macro. THis is the macro that I created with the wizard...
If Command()="Update_Burndown_Metrics" Then
RunSQL
SQL Statement insert .... blah, blah
End If
No error when I save the macro, but when I run...
The expression you entered has a function name that Microsoft Access can't find
If I replace the...
If Command()="Update_Burndown_Metrics"
with
If 1=1
It runs fine. IOW, it's not the SQL. It's the "Command() function that it can't find.
I got the idea to use Command() from Opening Microsoft Access with parameters . Doesn't seem to work for me. But that coding approach is also confirmed here... http://www.utteraccess.com/wiki/Command-Line_Switches . So I think it's something else.
Eventually, I would like to pass the Update_Burndown_Metrics arg using /cmd on the command line.
Why can't it find Command() as a valid function ? Is it a scoping thing? Do I have to give Command() context somehow, maybe with some sort of prefix ?
I can't seem to reproduce the issue that you are describing, though, I am using an earlier version of MS Access, and so there may be some differences in the behaviour of this function.
The Command function should be globally accessible, even outside of VBA (it can be referenced by the ControlSource property of a text box, for example), and so this isn't an issue of scope.
I do observe from the Office 365 documentation, that the Command function is being invoked without the use of parentheses in the sample VBA Sub provided; therefore it may be worth you trying your code without including such parentheses, e.g.:
If Command = "Update_Burndown_Metrics" Then
MsgBox "Test succeeded."
Else
MsgBox "Test failed."
End If
The workaround I created is to create a function in vba that gets and returns the command...
Public Function GetCommand() As String
GetCommand = Command()
End Function
Then...
If GetCommand() = "Update_Burndown_Metrics" Then ...

Error Trying to run Procedure from Another Form

I'm trying to call a Sub from another Form, but I get:
Error 2465: Application defined error or object defined error
I already know that to call a procedure from another form I can use the syntax:
Form(Name).Procedure
But, in this case I want to refer to this form from a variable (Type Form).
Code from form Parametro_Cadastro:
Dim formCaller As Form
Set formCaller = Application.Forms("Cadastro_Aluno").Form
If (Not formCaller Is Nothing) Then
'
formCaller.SetDadosParametroCadastro Me.txtKey.Value, dadoAlterado
Set formCaller = Nothing
End If
Code from form Cadastro_Aluno:
Sub SetDadosParametroCadastro(ByVal KeyValue As String, ByVal DataValue As String)
'
'...
End Sub
The code in Parametro_Cadastro gives Error 2465 on the line:
formCaller.SetDadosParametroCadastro Me.txtKey.Value, dadoAlterado`
...even before the Sub SetDadosParametroCadastroin the form Cadastro_Alunos to be called.
In the Immediate Window, the variable formCaller is already signed as Form\Form_Cadastro_Alunos as expected.
What can I do in this case?
EDIT
The only way it worked was to assign the variable formCaller to Access.Forms(formName) and not through Application.Forms(formName) or simply Forms(formName), so the code now is like below:
Code from form Parametro_Cadastro:
Dim formCaller As Form
Set formCaller = Access.Forms("Cadastro_Aluno")
If (Not formCaller Is Nothing) Then
'
formCaller.SetDadosParametroCadastro Me.txtKey.Value, dadoAlterado
Set formCaller = Nothing
End If
When I check the variable formCaller in the Imediate Window, I also get (as before) Forms\Form_Cadastro_Alunos.
The interesting thing is this only change make the code work as expected. Maybe the Forms member to be called is from Access Object instead of Applications?
Although you can call a procedure from a form, it's generally avoided and is considered bad practice.
Any Sub or Function that need to be shared should be placed in a separate, shared module.
Some excellent advice from Stewart # Bytes.com:
Form code modules are essentially 'private' in scope. Functions and subroutines defined in them are normally unavailable outside of the form concerned - although it is actually possible to call the functions and subroutines defined as Public in scope within a form code module from outside of that module.
Use of the keyword Public is not just a matter of suddenly adding one as a change of style. It reflects that you wish the subroutine, function or variable to be available outside of the scope of the code module concerned...
The code modules which can be created or opened from the Modules tab are the usual locations of public subroutines and functions. Named modules provide a means of grouping tasks by logical function, and if used consistently can be an aid to maintainability.
I use many several named modules in my applications, separating custom linked table maintenance from username parsing, e-mail handling and so on. I also include a "General" module which contains general bespoke functions that I find useful and re-use across multiple databases (for example, to return the current financial year or the current quarter within it).
All subroutines and functions defined using the Public keyword in the publicly-accessible code modules are available outside of the module concerned, whereas those defined as Private cannot be used outside of the scope of the code module in which they reside.
"If you must..."
If, for some reason, you must call a public sub from a form, the syntax is:
Call Forms!FormName.SubName
Note that the sub being called must have the Public keyword specified.
More Information:
MSDN : Understanding Scope and Visibility
Bytes.com : How and where to use Private or Public sub or function?

Why does Access VBA prompt me for a Macro when I press F5 or Run Sub?

I found this question Why does Excel VBA prompt me for a Macro name when I press Run Sub but the answers don't help me because my sub doesn't have arguments and is not triggered by an event.
I have two subs. One in a Module and one in a Form.
Sub ModuleSub()
MsgBox ("Hello World")
End Sub
Sub FormSub()
MsgBox ("Hello World")
End Sub
The Module Sub DOES RUN when I press F5, but the Form Sub doesn't. It brings up the Macro dialog. What is the problem here?
Macros stored in modules are independent and can be ran just like that.
However, all the functions/subs stored in Form modules or in Class modules are in fact the methods of this form/class and they cannot be ran without instance of this form/class.
If you open Immediate window (CTRL + G) you can run this code:
Call ModuleSub
without any issues.
This code:
Call FormSub
will return Compile error: Sub or function not defined.
However, if you add the name of form before the name of the function (like below):
call Form_FormName.FormSub 'replace FormName with the name of your form.
this sub will be also invoked without any issues, since you dynamically create an instance of form and VBA compiler can use its methods.

Application.MacroOptions and Error 1004

I want to register a function kind of CalculateHours(work_hour, rest_hour) to Excel VBA, in an Addin like Works.xla
I tried using Application.MacroOptions to register the function. The following code is in a Class file, the function is in another module file. They would load when we open Excel and the Addin.
Code:
Private Function AddFunctions()
With MyFunction
Application.MacroOptions .Name, _
.Description, , , , , .Category, , , .HelpFilePath
End With
End Function
Expectation:
I wanna get the argument help, function description in Excel function wizard as other built-in functions. With the help button link to my help file.
Result:
Error number: 1004 Application-defined or object-defined error
or
Method "MacroOptions" of object "_Application" failed
Is there anything (or everything) wrong?
I have kind of 10 functions and need to add them automatically to Excel function wizard every time load the Addin.
07/12/2016 well after dragging my function around following pieces of advise from some posts and doing a number of other pointless things, I found this error happens if Application.MacroOptions Description:=FuncDesc exceeds 255 characters. So essentially don't get too verbose with the description of your user defined function, or just add a
If Len(FuncDesc) > 255 then
Scary warning message about Run-time error '1004'
End if
Another possible issue (and solution) that was affecting me... The function code needs to be written in a module - if it it written in the ThisWorkbook page, Excel won't be able to find the code.
For ease of future readers, here is a compilation of the multiple answers (i.e. list of potential things to check)...
The function code needs to be written in a module (not ThisWorkbook)
Make sure the function description does not exceed 255 characters
If defined in another workbook, try including the workbook name - e.g. Macro:="'PERSONAL.xlsb'!Macro/UDF_Name"
If defined in an Excel add-in, call ThisWorkbook.Activate before Application.MacroOptions
Hope this helps, please one-up the respective solution poster if their answer helped you 👍🏼
I understand that question is old, but will post my solution to this error, as it may be common and exception message is not informative.
I fixed it with passing a macro/UDF-holder workbook name into "Macro" parameter, like "'Workbook.xls(x/m/b)'!Macro/UDF_Name":
' Adding a macro from Personal.xlsb
Application.MacroOptions _
Macro:="'PERSONAL.xlsb'!Macro/UDF_Name", _
Description:="Description", _
ArgumentDescriptions:=ArgumentsDescription()
Another observation: if the Application.MacroOptions in being run from VBA add-in with ThisWorkbook.IsAddin = True e. g. from Workbook_AddinInstall or Workbook_AddinUninstall, and there is no other workbook opened, Excel throws error 1004. Problem can be solved if one calls ThisWorkbook.Activate before calling Application.MacroOptions.

Microsoft office can't find the object "A_Z Schaltfläche"

I migrated a database from access 2003 to access 2007.
It seems to work but when I clicked the buttons from A-Z I'm getting this error message:
"Microsoft office can't find the object 'A_Z Schaltfläche'
If 'A_Z Schaltfläche is a new macro or macro group, make sure you have saved it and that you have typed it's name correctly"
I didn't make a new macro but I deleted a word in one of the tables which I think causes the problem: "Like [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium]"
I found it under the "Record Source" and under one field, that expression is written in the criteria field. If I don't delete this I'm getting a box which says: "Enter Parameter Value" Formulare!Frm_Fahrzeugdaten!Schaltflächenkriterium
My skills in VBA is not really so good, and I need some help how to correct the problem
Thanks for your help.
=======
additional info's:
When I open the VBA code under the Form_Fahrzeugen, here is what I saw:
Private Sub Auswahl_Click()
On Error GoTo Auswahl_Click_Err
' Programmablauf
Me.Filter = "[A_Fahrzeugtyp] like '*'"
Me.Namen_Filter.Value = 27
Me.Schaltflächenkriterium = "*"
Schaltflächenkriterium = "*"
Me.Requery
Me.lfd_Name.Requery
DoCmd.GoToRecord acDataForm, "Frm_Fahrzeugdaten", acGoTo, 1
Me.lfd_Name.Selected(1) = True
Me.A_Inventarnummer.SetFocus
GoTo Auswahl_Click_End
Auswahl_Click_Err:
Call ErrorHandler("Form_Frm_Fahrzeugdaten", "Auswahl_Click", Error$, Err, Erl)
Resume Next
Auswahl_Click_End:
'AusfĂźhrungen vor den verlassen der Routine
End Sub
Does it have something to do with the macro name? This is the macro name by the way: "A_Z Schaltfläche"
If I go to the design view of "A_Z Schaltfläche" this is what I got, the conditions are marked yellow I'm not really sure if this is a good sign though:
Thanks
==============
Updates about my problem:
I was able to find the solution of my problem. It was the version of my microsoft office which was causing it. The original database was written in German and when I did the migration, I migrated it to the english version of Access 2007. The reason why it can't find the object because of the name "Ereignisprozedur" in German and "Event procedure" in English. I changed it to Event Procedure because the error says:
"Microsoft Office Access can't find the object "Ereignisprozedur"
If Ereignisprozedur is a new macro or macro group, make sure you have saved it and that you have typed its name correctly
Access was unable to locate the macro or VBA function. If you are trying to call a macro, make sure that the name of the macro and the name of the macro group are spelled correctly.
If you are trying to call a user-defined VBA function, be sure to use the following syntax:
=FunctionName() or =FunctionName(argument1,argument2,...)
Make sure that the function is either:
Defined in the code for the form or report.
- or -
A public function that is in a module (not a class module)."
And the word "Formulare" to Forms. Then the program works.
I can't understand why microsoft programs are language independent???
Do you mean you deleted a word in one of the Queries? What it is is a reference to a form called Frm_Fahrzeugdaten and a control (field, column) called Schaltflächenkriterium. It is under criteria, so the query is saying:
Select such and such where this field (column) is Like this form and this control
It is usual to have
Like "*" & [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium] & "*"
Or
Like [Formulare]![Frm_Fahrzeugdaten]![Schaltflächenkriterium] & "*"
That is, with wild cards (*)
[Schaltflächenkriterium] seems to me to be the name of an edit control (textbox) in form [Frm_Fahrzeugdaten] for the user to filter a record set.
If you deleted the textbox control, just re-insert it and name it correctly.