访问 VBA - 从函数内更改 TextBox 值(ByRef)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16928322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:45:29  来源:igfitidea点击:

Access VBA - Changing TextBox value from within a function (ByRef)

functionvbatextboxbyref

提问by Zephram

I'm trying to write a sub that will get two parameters - a textbox in a form and a text. My intention is that the function will append the text into anytextbox.

我正在尝试编写一个将获得两个参数的子程序 - 表单中的文本框和文本。我的意图是该函数会将文本附加到任何文本框中。

Sub AppendTextBox([any textbox in my form], text As String)

[code that appends the text parameter to the textbox]

End Sub

Please note that I'm not trying to append text to a specific textbox, but to create a function that can receive any textbox in the form and append it with any text.

请注意,我不是要尝试将文本附加到特定文本框,而是要创建一个函数,该函数可以接收表单中的任何文本框并将其附加到任何文本。

Thanks for your help.

谢谢你的帮助。

回答by Zephram

I've found the answer and it's much simpler than I though it is:

我找到了答案,它比我要简单得多:

Private Sub AAA(A)

A.Value = "Desired text"

End Sub

Or if you want to append:

或者,如果您想附加:

Private Sub AAA(A)

A.Value = A.Value & vbnewline & "Desired text"

End Sub

回答by Pedro

Hi Zephram have you managed to find the solution for this? I have one but is a bit heavy because it uses loops. If you have one better let me know.

嗨,Zephram,您是否设法找到了解决方案?我有一个但有点重,因为它使用循环。如果你有更好的告诉我。

Private Function change_TextBox2(altera As String, textbox As ctl, valor As Variant)
Dim ctl As Control
If altera = "Popula" Then
    For Each ctl In Me.Controls
        With ctl
         If (InStr(.Name, textbox)) > 0 Then
             .Value = valor
         End If
       End With
    Next ctl
ElseIf altera = "hide" Then
    For Each ctl In Me.Controls
        With ctl
         If (InStr(.Name, textbox)) > 0 Then
             .Visible = False
         End If
       End With
    Next ctl
End If
End Function

回答by Andreas Dietrich

can be accomplished by:

可以通过以下方式完成:

dim appendTxt as String:  appendTxt = "appended text"
dim ws as Worksheet:  for each ws in ActiveWorkbook.Worksheets
    dim shape as Shape:  for each shape in ws.Shapes
        if shape.Type = msoTextBox then
            'you can move this code parameterized to a separate function then as req by OP:
            with shape.TextEffect:  .Text = .Text & appendTxt
        end if
    next shape
next ws