Access function to hide controls on a form - vba

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.

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

Set userform width from a module by looping through userforms

Been having a very frustrating problem that I can't seem to find a solution for anywhere - help would be greatly appreciated!
I know how to change the width of a userform from within the userform itself (Me.Width).
The problem is that I am writing a module that will format userforms dynamically. The module is passed a form and it does some manipulations on that form. So far everything works as long as I'm referring to objects on the form.
But I'm now trying to refer to the form itself, and when I try to change the width (of the form, not of a control on the form) I get the error: "Object doesn't support this property or method".
I've tried declaring it as both a UserForm and an Object and all of the other code works in both cases, but resizing the width doesn't. I can see in the immediate window that width isn't available, which is obviously why the code won't execute.
Here's the code that I've tried without succses:
Sub frmLoadLayoutTest()
frmLoadLayout frmTest
frmTest.Show
End Sub
Private Sub frmLoadLayout(frmActive As Object) 'Also tried (frmActive as UserForm)
'Setting the width of a control works
frmActive.Controls("labelTest").Width = 100
'Setting the width of the form itself doesn't work
frmActive.Width = 100
End Sub
If anyone can provide an elegant solution, that would be fantastic. Alternatively, is there a way I can refer to the userform by name? (Something like Userform(frm1.name).Width = 100)
Thanks in advance!
Try this:
Sub frmLoadLayoutTest()
Dim frm As frmTest
Set frm = New frmTest '<<< create an instance of the form
frmLoadLayout frm '<<< pass in the instance
frm.Show '<<< show the instance
End Sub
Private Sub frmLoadLayout(frm As Object)
With frm
.Controls("labelTest").Width = 100
.Width = 300
End With
End Sub
See also why it's good practice to avoid the "default instance" and create your own explicit instance of a form before using it:
https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/

VBA Excel: Call sub in form from 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

Call SetFocus for a textbox with Excel VBA in Excel 2013

When I call SetFocus for my textbox, it throws this error:
Run-time error '438':
Object doesn't support this property or method.
Is SetFocus readily available in Excel 2013 or do I have to patch or update any component of my Excel?
I think Setfocus() probably never throw an error like that!!
Object doesn't support this property or method (Error 438)
This method or property does not exist for this OLE automation object. See the object's documentation for more information on the object and to check the spellings of properties and methods.
Here you have a method SetFocus(), So you need to know when this method does not exist!
In Office 2013 if object or one of its containers is not visible or enable, it throw this error:
Run-time error '2110':
Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
And as I tested making loop by raising Enter Event is also don't throw error, And also for similar using SetFocus() method:
Private Sub TextBox1_Enter()
TextBox2.SetFocus
End Sub
Private Sub TextBox2_Enter()
TextBox1.SetFocus
End Sub
And I can't figure any other reason !
Please see this answer: https://www.vbforums.com/showthread.php?646411-RESOLVED-How-can-I-make-setfocus-ot-Textbox-in-vba
You are using Excel after all, but you have the textbox on a worksheet rather than a userform.
Setfocus will work for textboxes on a Userform, but as you have discovered, not if they are on the actual worksheet.
Instead of setfocus try activate:
Private Sub CommandButton1_Click()
TextBox1.Activate
End Sub