Passing text box control to private sub/function
Asked Answered
J

5

11

I have a series of text boxes that I want to evaluate on form load to change some other elements of the form. Rather than processing them line by line I want to create a function. How can I pass the name of the text box to a function. For example:

Private Sub Form_Load()

    Call SetIntialToggleValue(MyTextboxName)

End Sub

Private Sub SetIntialToggleValue(ByRef ControlName As Control)

    MsgBox ControlName.Value

End Sub

This give me "Object required" error on the MsgBox line. Which makes me think I'm not passing the textbox control properly.

Jennette answered 5/11, 2014 at 17:53 Comment(0)
T
12

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

If you haven't explicitly declared your variable, MyTextboxName, it will be of type variant. You can't pass a variant to an object parameter. This might be enough:

dim MyTextBoxName as Control

But in this case, you wouldn't be working with names at all. If you want to work with names, you have to change your function. Change the signature to take a string and then use the string as the index into the Controls collection (if there is one on Access Forms):

Private Sub SetIntialToggleValue(ByRef ControlName As String)

    MsgBox Form.Controls(ControlName).Value

End Sub
Thebault answered 5/11, 2014 at 21:7 Comment(0)
O
5

How about something like the following:

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
Oballa answered 5/11, 2014 at 21:7 Comment(1)
Please don't declare a variable as global if you're only going to use it one method... That Dim statement should go in the Form_Load() method.Sunnysunproof
B
5

I found the answer here. You can declare:

Private Sub SetIntialToggleValue(ByRef ControlName As MSForms.TextBox)
Brood answered 15/2, 2018 at 8:53 Comment(0)
S
0

Currently, you're not passing in the name of the textbox. You're passing in the textbox object itself. You did reference it as a Control in your method, but if you're only using textboxes, you could replace Control with Textbox. Indeed, Textbox is its own type.

Sunnysunproof answered 5/11, 2014 at 22:21 Comment(0)
S
0

The answer as I'm sure you have all figured out is given here: https://www.access-programmers.co.uk/forums/threads/passing-a-control-as-parameter.213322/

Calling a function with brackets causes the compiler to force a conversion to ByVal which replaces your "control" with the "value" of the control and passes it with the wrong type.

Correct answer is simply SetIntialToggleValue MyTextboxName

Solvency answered 8/6, 2022 at 14:54 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Albatross
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewSahara

© 2022 - 2024 — McMap. All rights reserved.