Passing textbox control to a private sub / function

I have a number of text fields that I want to evaluate when loading a form to change some other form elements. Instead of processing them line by line, I want to create a function. How to pass the name of a text field to a function. For instance:

Private Sub Form_Load()

    Call SetIntialToggleValue(MyTextboxName)

End Sub

Private Sub SetIntialToggleValue(ByRef ControlName As Control)

    MsgBox ControlName.Value

End Sub

This will give me the "Object required" error in the MsgBox line. This makes me think that I am not passing textbox control correctly.

+4
source share
4 answers

I would ask for clarification if I have enough comments to comment, but here are some thoughts:

MyTextboxName, . . :

dim MyTextBoxName as Control

. , . , , Controls ( Access Forms):

Private Sub SetIntialToggleValue(ByRef ControlName As String)

    MsgBox Form.Controls(ControlName).Value

End Sub
+5

:

Option Explicit
Dim ctl  as Control

Private Sub Form_Load()

    set ctl = Me.MyTextboxName
    Call SetIntialToggleValue(ctl)

End Sub

Private Sub SetIntialToggleValue(ByRef ControlName As Control)

    MsgBox ControlName.Value

End Sub
+3

. . Control , , Control Textbox. , Textbox .

0
source

I found the answer here . You can declare:

Private Sub SetIntialToggleValue(ByRef ControlName As MSForms.TextBox)
0
source

All Articles