vba 循环访问访问数据库中的文本框

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

Loop through Textboxes in access database

databasevbams-accessloopstextbox

提问by designspeaks

I have an Access database that I want to loop through certain textboxes in order to do a calculation and display the answer in a seperate textbox. When I attempt to loop it loops through every control on my form (with the Form.Controls) method. I would like to only loop through 4 specific textboxes (txtbx1, txtbx2, txtbx3, txtbx4) when my button is clicked.

我有一个 Access 数据库,我想遍历某些文本框以进行计算并在单独的文本框中显示答案。当我尝试循环时,它会遍历表单上的每个控件(使用 Form.Controls)方法。当我的按钮被点击时,我只想循环 4 个特定的文本框(txtbx1、txtbx2、txtbx3、txtbx4)。

Explanation... TextBox_A contains a Number Upon button click take the number from TextBox_A, Multiply by 2800, then Divide by 12 Display the answer to the calculation in txtbx1.

说明... TextBox_A 包含一个数字 单击按钮后,从 TextBox_A 中取出数字,乘以 2800,然后除以 12 在 txtbx1 中显示计算结果。

I would do this for each of the 4 textboxes named above. Then have a "Total" textbox that adds up the total from each textbox (txtbx1, txtbx2, txtbx3, txtbx4). Please help, new to this and at a complete loss.

我会为上面命名的 4 个文本框中的每一个执行此操作。然后有一个“总计”文本框,将每个文本框(txtbx1、txtbx2、txtbx3、txtbx4)的总数相加。请帮助,这个新手并且完全不知所措。

回答by Andy Brown

Every control has got a Tag property. Set the Tag property for textboxes whose values you want to include in your algorithm (say to "INClUDE"). It's free format text, so you can put what you like.

每个控件都有一个 Tag 属性。为要包含在算法中的值的文本框设置 Tag 属性(比如“INClUDE”)。它是自由格式文本,因此您可以放置​​您喜欢的内容。

Then write code attached to some form event similar to this:

然后编写附加到一些类似于此的表单事件的代码:

 Dim c As Control
Dim txt As TextBox

For Each c In Me.Controls

    'check it's a text box ...
    If TypeOf c Is TextBox Then

        'see if including ...
        Set txt = c
        If Len(txt.Tag) > 0 Then

            'do something here (I've coloured, to show works)
            txt.BackColor = 10

        End If

    End If

Next c

I've set an extra variable txt to refer to the textbox in question, so that I can get at the TAG property using autocompletion (I believe this is called a narrowing conversion!).

我已经设置了一个额外的变量 txt 来引用有问题的文本框,以便我可以使用自动完成来获取 TAG 属性(我相信这称为缩小转换!)。

回答by KekuSemau

It would be easier if you rename TextBox_A (_B / _C etc) to TextBox_1 / _2 / _3 etc. And then try this:

如果您将 TextBox_A (_B / _C 等) 重命名为 TextBox_1 / _2 / _3 等会更容易,然后试试这个:

Dim i As Integer
For i = 1 To 4
    Controls("txtbx" & i) = Val(Controls("TextBox_" & i)) * 2800 / 12
Next

Otherwise, this would work too, but it is not well readable:

否则,这也可以,但可读性不好:

= Val(Controls("TextBox_" & Chr(Asc("A") + i - 1))) * 2800 / 12