访问 VBA - 我如何在 VBA 中有一个循环来允许我循环控制名称 [英] Access VBA - How would I have a loop in VBA that allows me to loop through control names

查看:30
本文介绍了访问 VBA - 我如何在 VBA 中有一个循环来允许我循环控制名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个表单上有大约 10 个文本框,它们实际上用于显示而非输入.它们被命名为txt_001_Nametxt_002_Title 等等.为此使用什么样的循环.

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

这可能很简单 - 我更应该学习如何做!

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 个文本框中的每一个实际上代表的是数值,在表单的 onload 事件中,每个文本框后面都有一个 SQL 语句.我还有另外一组十个,它们包含更加静态的数值,最后另外十个使用表达式简单地将前十个中的每一个除以相对的第二个"十,并且该值以相对值结束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

您可以从表单的 Load 事件中调用它:

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

Private Sub Form_Load()
    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 并让 Access 为您将正确的值放入 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

这篇关于访问 VBA - 我如何在 VBA 中有一个循环来允许我循环控制名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆