如何使用 VBA 中的循环函数填充多个 texbox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2944693/
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-11 11:40:59  来源:igfitidea点击:

How to fill many texbox by using loop function in VBA

vbatextboxloops

提问by user90714

I made a user interface in VBA with many textbox. I read an excel sheet and I put all the value of this one in all the textbox of my user inteface. So the user can modify the values and then save it in the excel sheet.

我在 VBA 中制作了一个带有许多文本框的用户界面。我阅读了一张 excel 表,并将此表的所有值都放在用户界面的所有文本框中。因此用户可以修改这些值,然后将其保存在 Excel 工作表中。

Because we can't name the textbox like array (textBox(1), textbox(2)....) this is hard to fill the textbox by using a loop function.

因为我们不能像数组那样命名文本框 (textBox(1), textbox(2)....) 这很难通过使用循环函数来填充文本框。

I tried to use tag or tabindex property but I don't find the good way to proceed .... Is someone know an easy way to solve this !!!

我尝试使用 tag 或 tabindex 属性,但我没有找到继续进行的好方法......有人知道解决这个问题的简单方法吗!!!

Thanks

谢谢

采纳答案by jpinto3912

Iterate the form's control collection.

迭代表单的控件集合。

An example, say your user form is called myForm, then

例如,假设您的用户表单名为 myForm,则

myForm.Controls(i)

gets you a handle for any control in myForm. Now you can use control properties to identify which one you're looking at (label, textbox, button, etc). I'd suggest you use a Tagsuch as, hmmmm.... "TEXTBOX", to ease the process of id.

为您提供 myForm 中任何控件的句柄。现在,您可以使用控件属性来确定您正在查看哪一个(标签、文本框、按钮等)。我建议你使用一个标签,比如,嗯……“TEXTBOX”,来简化 id 的过程。

if myForm.Controls(i).Tag="TEXTBOX" then 'it's my textbox ! hurraay!!!

回答by GSerg

Yes you can.

是的你可以。

Name your textboxes Textbox1, Textbox2 etc, then access them with

将您的文本框命名为 Textbox1、Textbox2 等,然后使用

Form.Controls("Textbox" & ID)

回答by ahmed tharwat

you can use this code to fill multi textboxs

您可以使用此代码填充多个文本框

    Dim rs As Object
    Dim i As Integer
    Dim ctlr As Control
     Set rs = Me.Recordset.Clone
     For Each ctlr In Me.Controls
     If TypeOf ctlr Is TextBox Then
      For i = 0 To ctlr.Controls.Count
      On Error Resume Next
       ctlr.Value = rs!SomeField 
       rs.MoveNext
      Next i
    End If

Next