访问VBA - 我怎么会在VBA循环,让我来遍历控件名称我来、遍历、会在、控件

2023-09-08 11:10:07 作者:陪你听梦

我有大约10文本框实际上是用于显示未录入表单上。它们被命名为 txt_001_Name txt_002_Title ,etc..what的循环方式被用于此目的。

I have about 10 text boxes on a form that are actually used for display not entry. They are are named txt_001_Name, txt_002_Title, etc..what kind of loop is used for this.

我要通过的文本框的名称使用实际环路什么样的VBA?所以,如果我是debug.print它看起来像:

What kind of VBA should I use to actually loop through the names of the text boxes? So if I was to debug.print it would look like:

txt_001_Title
txt_002_Title
txt_003_Title

这大概是pretty的简单的事 - 更有理由,我应该学会如何

This is probably pretty simple to do - all the more reason that I should learn how!

编辑:对不起,我本来应该更具描述这个

Sorry, I should have been more descriptive about this.

由于上述命名约定,我期待通过这些文本框迭代,这样我可以进行一些与每个。各有什么这10个文本框实际上是重新present是数值,每一个在窗体的的onload 事件在他们身后的SQL语句。我也有另一套10的那认为是更静态的数值,最后另一个10使用一个前pression简单地将每个所述第一10的,对相对的第二10,并且该值两端在相对3所以基本上它结束了看起来像一个仪表盘表。

Because of the above naming convention, I am looking to iterate through these text boxes so that I can perform something with each. What each of these 10 text boxes actually represent is numeric values, each having a SQL statement behind them in the form's onload event. I also have another set of ten that hold numeric values that are much more static, and finally another ten that use an expression to simply divide each of the first ten, against the relative "second" ten, and the value ends up in the relative 3. So basically it ends up looking like a dashboard table.

'first ten'       'second ten'     'resulting ten'
---------------------------------------------------
txt_001_value      txt_001_calc     txt_001_result
txt_002_value      txt_002_calc     txt_002_result

等。

所以,其实我是想用这个为'导致'文本框。我想遍历前十位,并执行这个简单的计算:

So I actually want to use this for the 'resulting' text boxes. I want to loop through the first ten and perform this easy calculation:

 me.txt_001_result = me.txt_001_value / me.txt_001_calc     

所有的命名约定投其所好,这样我就可以手动输入了10行以上这一点,但我相信有一个更好的方法(环通这一点),而我也许应该学习它。

All the naming conventions "match up", so I can manually type out the 10 lines of the above for this, but I am sure there is a better way (loop through this), and I should probably learn it.

推荐答案

您可以列出的文本框控件的名称以一个简单的程序是这样的:

You can list the names of textbox controls with a simple procedure like this:

Public Sub TextBoxNames(ByRef pfrm As Form)
    Dim ctl As Control
    For Each ctl In pfrm.Controls
        If ctl.ControlType = acTextBox Then
            Debug.Print ctl.Name
        End If
    Next ctl
    Set ctl = Nothing
End Sub

您可以从窗体的Open事件调用它:

You could call it from the form's Open event:

Private Sub Form_Open(Cancel As Integer)
    TextBoxNames Me
End Sub

不过,我不明白你要完成的任务。我知道你想要做什么用的 ctl.Name 的以外 Debug.Print ,但我不知道那是什么。

However, I don't understand what you're trying to accomplish. I realize you want to do something with ctl.Name other than Debug.Print, but I don't know what that is.

而不是计算结果的 me.txt_001_result 的,然后该值赋给文本框,考虑设置控制源的 txt_001_result 的到的 txt_001_value / txt_001_calc 的,让访问把正确的值到的 txt_001_result 的你。

Rather than computing a result for me.txt_001_result and then assigning that value to the text box, consider setting the control source for txt_001_result to txt_001_value / txt_001_calc and let Access put the proper value into txt_001_result for you.

在回答您的意见,我会建议这个程序为出发点,为您建立在:

In response to your comments, I'll suggest this procedure as a starting point for you to build upon:

Public Sub MyTextBoxValues()
    Const cintLastTextBoxNum As Integer = 10
    Dim i As Integer
    Dim strValueControl As String
    Dim strCalcControl As String
    Dim strResultControl As String
    Dim strPrefix As String

    For i = 1 To cintLastTextBoxNum
        strPrefix = "txt_" & Format(i, "000")
        'txt_001_value      txt_001_calc     txt_001_result '
        strValueControl = strPrefix & "_value"
        strCalcControl = strPrefix & "_calc"
        strResultControl = strPrefix & "_result"
        'me.txt_001_result = me.txt_001_value / me.txt_001_calc '
        'Debug.Print strResultControl, strValueControl, strCalcControl '
        Me.Controls(strResultControl) = Me.Controls(strValueControl) / _
            Me.Controls(strCalcControl)
    Next i
End Sub