ACCESS 2016
I am trying to create a function to hide controls that are passed to it.
I have a textbox called NextCheck. I have the function:
Public Function hideControl(ctrl As Control)
Set ctrl.Visible = False
End Function
And the Sub on the form below.
Private Sub testBtn_Click()
hideControl (Me.Nextcheck)
End Sub
When I run this I get a message
'run-time error '424' Object required
Can anyone point me in the right direction (I'm a novice with functions) as to what the problem is here. My understanding that the (Me.Nextcheck) was the object being passed to the function.
Thanks.
I have the following two Sub defined in my Word Addin (.dotm) which I have put in StartUp directory
Public Sub SayHi1()
MsgBox "Hi......."
End Sub
Public Sub SayHi2(ByVal n As String)
MsgBox "Hi " & n
End Sub
Then from a new document I am able to call 1st Sub without argument as below:
Sub AppRun_AddIn_NoArg()
Application.Run "MyProject.Module1.SayHi1"
End Sub
But when I try to run the 2nd Sub with argument I get error saying "Object doesn't support this property or method"
Sub AppRun_AddIn_WithArg()
Application.Run "MyProject.Module1.SayHi2", "Tejas"
End Sub
Error Message:
This appears to be long-standing problem with Word.
As KB190235 suggests:
Cause:
You have included a template name as part of the Macroname argument string.
Resolution:
Remove the template name from the Macroname argument.
Workaround:
To avoid naming conflicts among referenced projects, give your procedures unique names, so that you can call a procedure without specifying a project or module.
I want to call a function in a form from a function in a modul
So I defined the function Init_StockForm() in the form (which is called Form):
Public Sub Init_StockForm()
MsgBox ("Bla")
End Sub
I call that function in the module as follows:
Form.Init_StockForm
but this results in a Runtime error 91
So what's wrong here?
Thanks form help
make a new instance of your form (VBA is object oriented Programming Language)
module:
Private Sub callFormSub()
Dim myForm As New UserForm1
myForm.subInUserForm
End Sub
Form:
Public Sub subInUserForm()
MsgBox "BLA"
End Sub
Thank you for your answers
Today the solution I posted also works ( :/ ) the reason might be a bug on my installation, since the macro shows also a very strange behaviour in an other function, but I will post that later in an other post if I can't solve it
I have the following two Sub defined in my Word Addin (.dotm) which I have put in StartUp directory
Public Sub SayHi1()
MsgBox "Hi......."
End Sub
Public Sub SayHi2(ByVal n As String)
MsgBox "Hi " & n
End Sub
Then from a new document I am able to call 1st Sub without argument as below:
Sub AppRun_AddIn_NoArg()
Application.Run "MyProject.Module1.SayHi1"
End Sub
But when I try to run the 2nd Sub with argument I get error saying "Object doesn't support this property or method"
Sub AppRun_AddIn_WithArg()
Application.Run "MyProject.Module1.SayHi2", "Tejas"
End Sub
Error Message:
This appears to be long-standing problem with Word.
As KB190235 suggests:
Cause:
You have included a template name as part of the Macroname argument string.
Resolution:
Remove the template name from the Macroname argument.
Workaround:
To avoid naming conflicts among referenced projects, give your procedures unique names, so that you can call a procedure without specifying a project or module.
I know this is a simple question for someone out there, but I have never really used function module at all because I did not understand what they were.
So I have a whole bunch of things I can use this for (cut down on redundancy), but I want to know how I call into a sub (like a button click) procedure from a form.
I tried this...
Sub Command_Click()
Call "pptCreator"
End Sub
I know that is pretty bad, but I have no idea how to bring this into a procedure.
Here are some of the different ways you can call things in Microsoft Access:
To call a form sub or function from a module
The sub in the form you are calling MUST be public, as in:
Public Sub DoSomething()
MsgBox "Foo"
End Sub
Call the sub like this:
Call Forms("form1").DoSomething
The form must be open before you make the call.
To call an event procedure, you should call a public procedure within the form, and call the event procedure within this public procedure.
To call a subroutine in a module from a form
Public Sub DoSomethingElse()
MsgBox "Bar"
End Sub
...just call it directly from your event procedure:
Call DoSomethingElse
To call a subroutine from a form without using an event procedure
If you want, you can actually bind the function to the form control's event without having to create an event procedure under the control. To do this, you first need a public function in the module instead of a sub, like this:
Public Function DoSomethingElse()
MsgBox "Bar"
End Function
Then, if you have a button on the form, instead of putting [Event Procedure] in the OnClick event of the property window, put this:
=DoSomethingElse()
When you click the button, it will call the public function in the module.
To call a function instead of a procedure
If calling a sub looks like this:
Call MySub(MyParameter)
Then calling a function looks like this:
Result=MyFunction(MyFarameter)
where Result is a variable of type returned by the function.
NOTE: You don't always need the Call keyword. Most of the time, you can just call the sub like this:
MySub(MyParameter)
if pptCreator is a function/procedure in the same file, you could call it as below
call pptCreator()
Calling a Sub Procedure – 3 Way technique
Once you have a procedure, whether you created it or it is part of the Visual Basic language, you can use it. Using a procedure is also referred to as calling it.
Before calling a procedure, you should first locate the section of code in which you want to use it. To call a simple procedure, type its name. Here is an example:
Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
msgbox strFullName
End Sub
Sub Exercise()
CreateCustomer
End Sub
Besides using the name of a procedure to call it, you can also precede it with the Call keyword. Here is an example:
Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
Sub Exercise()
Call CreateCustomer
End Sub
When calling a procedure, without or without the Call keyword, you can optionally type an opening and a closing parentheses on the right side of its name. Here is an example:
Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
Sub Exercise()
CreateCustomer()
End Sub
Procedures and Access Levels
Like a variable access, the access to a procedure can be controlled by an access level. A procedure can be made private or public. To specify the access level of a procedure, precede it with the Private or the Public keyword. Here is an example:
Private Sub CreateCustomer()
Dim strFullName As String
strFullName = "Paul Bertrand Yamaguchi"
End Sub
The rules that were applied to global variables are the same:
Private: If a procedure is made private, it can be called by other procedures of the same module. Procedures of outside modules cannot access such a procedure.
Also, when a procedure is private, its name does not appear in the Macros dialog box
Public: A procedure created as public can be called by procedures of the same module and by procedures of other modules.
Also, if a procedure was created as public, when you access the Macros dialog box, its name appears and you can run it from there
Procedures in a Module start being useful and generic when you pass in arguments.
For example:
Public Function DoSomethingElse(strMessage As String)
MsgBox strMessage
End Function
Can now display any message that is passed in with the string variable called strMessage.
To Add a Function To a new Button on your Form: (and avoid using macro to call function)
After you created your Function (Function MyFunctionName()) and you are in form design view:
Add a new button (I don't think you can reassign an old button - not sure though).
When the button Wizard window opens up click Cancel.
Go to the Button properties Event Tab - On Click - field.
At that fields drop down menu select: Event Procedure.
Now click on button beside drop down menu that has ... in it and you will be taken to a new Private Sub in the forms Visual Basic window.
In that Private Sub type: Call MyFunctionName
It should look something like this:
Private Sub Command23_Click()
Call MyFunctionName
End Sub
Then just save it.