VBA在运行时创建的文本框无法引用。

huangapple go评论109阅读模式
英文:

VBA Textboxes created at runtime cannot be referenced

问题

我有一些代码,它在运行时创建了一整行的文本框,文本框使用预先建立的框的尺寸来创建一整行的文本框,每次单击"Add Line"时都会这样做,效果如下 -

VBA在运行时创建的文本框无法引用。

这是通过以下代码完成的 -

Public Sub AddLine_Click()
Static i As Integer
Dim txtbox As Object

DocNameArr = Array("Type", "Status", "Reference", "Units", "Quantity", "ValidityDate", "IssuingAuthority", "Reason")

AbsoluteHeight = Me.DocType.Top
prevWidth = 0

Dim cCont As Control
contCount = 1

For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" And InStr(1, cCont.Name, "Type") = 1 Then
contCount = contCount + 1
End If
Next cCont

     For Each DocName In DocNameArr
     Set txtbox = DocumentsForm.Controls.Add("Forms.Textbox.1", True)
     currTxtbox = "Doc" & DocName
     
     txtboxWidth = DocumentsForm.Controls(currTxtbox).Width
     
            With txtbox
            .Name = DocName & contCount
            .Left = 10.5 + prevWidth
            .Height = 26.25
            .Top = 47.25 + (26.25 * contCount)
            .Width = txtboxWidth
            .SpecialEffect = fmSpecialEffectFlat
            .BorderStyle = fmBorderStyleSingle
            .BorderColor = &H80000000
            End With
            
    prevWidth = prevWidth + txtboxWidth
    Next
End Sub

我在运行时根据代码命名文本框,每个文本框都有一个用于标识行的编号和一个用于标识框的名称。这一切都没问题,直到我想保存用户输入到框中的信息。

如果我直接通过名称引用在运行时创建的文本框,并尝试将文本直接插入其中,它会引发"对象所需"错误,告诉我找不到该名称的对象。例如,根据代码,我应该在第一行创建一个名为"Type1"的框,但我无法引用它。

目前,出于测试目的,我只是直接引用文本框,看看是否可以通过名称访问它,例如:

Type1.Value = "Test"
Type2.Value = "Test"
Type3.Value = "Test"

只是尝试将单词"Test"解析到创建的任何文本框中。

如何保存以以上方式在运行时创建的文本框的数据?

我不明白为什么它不让我通过在运行时分配的名称调用文本框,我将所有控件输出到消息框中,对于所有运行时命名的控件,它都显示为""空字符串?我在哪里出错了.. 任何帮助将不胜感激。

英文:

I have some code that creates a whole line of textboxes at run-time, the textboxes use the dimensions of the pre-established boxes to create a whole line of textboxes each time 'Add Line' is clicked and it looks like this -

VBA在运行时创建的文本框无法引用。

This is done with the following code -

Public Sub AddLine_Click()
Static i As Integer
Dim txtbox As Object


DocNameArr = Array("Type", "Status", "Reference", "Units", "Quantity", "ValidityDate", "IssuingAuthority", "Reason")

AbsoluteHeight = Me.DocType.Top
prevWidth = 0

Dim cCont As Control
contCount = 1

For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" And InStr(1, cCont.Name, "Type") = 1 Then
contCount = contCount + 1
End If
Next cCont


     For Each DocName In DocNameArr
     Set txtbox = DocumentsForm.Controls.Add("Forms.Textbox.1", True)
     currTxtbox = "Doc" & DocName
     
     txtboxWidth = DocumentsForm.Controls(currTxtbox).Width
     
            With txtbox
            .Name = DocName & contCount
            .Left = 10.5 + prevWidth
            .Height = 26.25
            .Top = 47.25 + (26.25 * contCount)
            .Width = txtboxWidth
            .SpecialEffect = fmSpecialEffectFlat
            .BorderStyle = fmBorderStyleSingle
            .BorderColor = &H80000000
            End With
            
    prevWidth = prevWidth + txtboxWidth
    Next
End Sub

I name the textboxes at runtime as per the code and each separate box in each line of boxes has a number to identify the line and a name to identify the box. This is all fine until I want to save the information that the user enters into the box.

If I reference a textbox directly via name that was created during run-time and as a test insert text directly into it, it drops an Object Required error telling me that the object of that name cannot be found. For example based on the code, I should have a "Type1" box created on the first line, but I can't reference it.

For now, for testing purposes all I've done is reference the textbox directly to see if its accessible via name by doing

Type1.Value = "Test"
Type2.Value = "Test"
Type3.Value = "Test"

Just trying to parse the word "Test" into any of the textboxes that are created.

How do I save the data of the textbox if its created as above during run-time?

I don't see why it doesn't let me call the textbox via the name its getting allocated at run-time, I output all controls into a msgbox and it comes up with "" for all the run-time named controls? Where am I going wrong here.. any help would be appreciated.

答案1

得分: 2

循环遍历Controls集合并提取所需的TextBox,例如:

Function GetTextBox(sName As String) As MSForms.TextBox
    Dim ctrl As Control
    For Each ctrl In Controls
        If TypeOf ctrl Is MSForms.TextBox Then
            If ctrl.Name = sName Then
                Set GetTextBox = ctrl
                Exit Function
            End If
        End If
    Next ctrl
End Function

...将TextBox的名称传递给此函数,只要它存在于UserForm中,它将返回TextBox对象给您。

英文:

Loop through the Controls collection and pull out the TextBox you want eg

Function GetTextBox(sName As String) As MSForms.TextBox
    Dim ctrl As Control
    For Each ctrl In Controls
        If TypeOf ctrl Is MSForms.TextBox Then
            If ctrl.Name = sName Then
                Set GetTextBox = ctrl
                Exit Function
            End If
        End If
    Next ctrl
End Function

... pass the name of the TextBox into this Function and, so long as it exists in the UserForm, it will return the TextBox object to you

答案2

得分: 1

使用控件集合,例如:

Dim i As Integer
For i = 1 To contCount
     Debug.Print Me.Controls(DocName & contCount).Text
Next
英文:

Use the controls collection e.g.

Dim i As Integer
For i = 1 to contCount
     Debug.Print Me.Controls(DocName & contCount).Text
Next

huangapple
  • 本文由 发表于 2023年6月19日 17:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505196.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定