VBA Excel: Call sub in form from module - vba

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

Related

VBA How To Set Form In Function

I add Form1.vb in my project but ı couldn't show it. Form1.show or Form1.ShowDialog didn't worked beacuse it type is class.
Here is the error message;
bc30109 'form1' is a class type and cannot be used as an expression
Solution must be some think like that.
Private Sub Form1Func()
Dim f1 As Object
f1 = Form1
f1.ShowDialog
End Sub
Private Sub OnButtonClick(cmd As Commands_e)
Select Case cmd
Case Commands_e.Export_As_SheetMetal
Form1Func()
Case Commands_e.AboutUs
Application.ShowMessageBox("AboutUS")
End Select
End Sub
Without knowing more about other actions in your program, I cannot pinpoint why you're being asked to provide an argument for show (possibly a named function, etc.), but this is a way to ensure a form is called as an object:
Dim obj As Object: Set obj = VBA.UserForms.Add("userform1")
obj.Show vbModeless
Specific to your error, the above code directly generates the userform object through vba.
Private Sub Form1Func()
Dim f1 As New Form1
f1.Show()
End Sub

Modify form property within a Public Function

I have a form called "One" and a module called "Module".
Inside the module I have the following function
Public Function justTesting()
MsgBox (Forms!One.innerWidth)
End Function
and inside the form "One" I have an "onLoad" event with the following code:
Private Sub Form_Load()
Call justTesting
End Sub
I am getting the following error:
"Run-time error '2450': Microsoft Access cannot find the referenced form 'One".
I have tried several solutions I found on the internet.
A form cannot have the name Form_One so, my guess is that it is named One only.
Thus the code should read:
Public Function justTesting()
MsgBox Forms!One.Width
End Function

How do I get a reference to the Application object of a UserForm?

If I want to write a Sub that positions a UserForm relative to the Application object that displays it, How would I go about doing it?
I want to write a sub that goes like this:
Sub PositionForm(WhichForm as Object)
WhichForm.Left = <WhichForm Application Object>.Left
End Sub
I understand that there are many workarounds to this. I am interested in knowing whether there is a way to getting that reference.
in Excel the following works:
Sub PositionForm(WhichForm As Object)
WhichForm.Left = Application.Left
End Sub
to be called from any UserForm code as:
Private Sub CommandButton2_Click()
... any code
PositionForm Me
... any code
End Sub

Access function to hide controls on a form

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.

Global reference to object on Click() sub (VBA)

I was wondering if there's a way to refer to the object of the "Click()" sub.
To make it clearer, let's say we have a button named foo1 and this button has a click sub "foo1_Click()". Does vba has a keyword to get the reference to foo1 that is global?
Something like:
Public Sub foo1_Click()
GlobalKeyword.Property
End Sub
P.s.: something like the word "this" from java refering to its own instance of class
Edit: In the example, the "GlobalKeyword" would refer to "foo1"
I think you're looking for the Application.Caller property found here.
In your case you would want to do something like....
Public Sub foo1_Click()
Dim button As Shape
Set button = ThisWorkbook.Sheets("sheetname").Shapes(Application.Caller)
End Sub
Of course after that you would want to do some error checking to make sure button is not nothing.
If you want to use the same code for a lot of buttons, then you may be better of using a separate subroutine.
Private Sub foo1_Click()
Call do_something
End Sub
Private Sub foo2_Click()
Call do_something
End Sub
Sub do_something() 'called by the foo _Click event
MsgBox Application.Caller
End Sub
This way, you it is easy to maintain the core functionality for all buttons simply by updating the do_something procedure.