使用 vba 访问表单中的字段

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

Access fields in form using vba

formsvbams-access-2010

提问by user969113

I created a query and a form in Microsoft Access 2010. The form, named TEST, looks as follows:

我在 Microsoft Access 2010 中创建了一个查询和一个表单。名为 TEST 的表单如下所示:

Field1   Field2
a        200
b        400

In VBA I tried to access the different fields in the form:

在 VBA 中,我尝试访问表单中的不同字段:

Form_TEST.Field1....

I want to save the values 200 and 400 in an integer variable (Dim a As Integer) and print it using MsgBox. How can i achieve that??

我想将值 200 和 400 保存在一个整数变量中(Dim a As Integer)并使用 MsgBox 打印它。我怎样才能做到这一点?

回答by bilbo_strikes_back

You can use the Me as the open form and assign the variable if you know the name of the text box.

如果您知道文本框的名称,您可以使用 Me 作为打开的表单并分配变量。

Dim intValue as Integer
'If text box name = TextBox1
intValue = Me.TextBox1.Value

回答by Wiz

I'll try to help you. I understood you created the form with the wizard putting the 2 fields on the form. What is not clear is the View that you are using.

我会尽力帮助你。我知道您使用向导创建了表单,将 2 个字段放在表单上。不清楚的是您正在使用的视图。

Well, your form can be displayed in different ways: - Single form - Continuous forms - Datasheet

好吧,您的表单可以以不同的方式显示: - 单一表单 - 连续表单 - 数据表

This is defined by the Default View property.

这是由默认视图属性定义的。

To see the properties of you form press F4 to see properties and select "Form" as the object that you want to see.

要查看表单的属性,请按 F4 以查看属性并选择“表单”作为您要查看的对象。

If your form is Single Form or Continuous form you can access the two fields you put on it simply addressing them.

如果您的表单是单一表单或连续表单,您可以访问您放置在其上的两个字段,只需对它们进行寻址即可。

Click on the controls you put on the form and press F4 to see the control name.

单击您放置在窗体上的控件,然后按 F4 以查看控件名称。

CASE 1 - SINGLE FORM VIEWLet's assume that your controls are named Text1 (200) and Text2 (400) and for convenience your form is a single form.

案例 1 -单一表单视图让我们假设您的控件被命名为 Text1 (200) 和 Text2 (400),为了方便起见,您的表单是一个单一的表单。

So you can refer to values in the 2 controls writing

所以你可以参考2个控件写的值

Dim intText1 As Integer, intText2 As Integer

intText1 = Me.Text1.Value
intText2 = Me.Text2.Value

The .Value property is not mandatory cause it's the default property. At this point you can print out intText1,2 with a MsgBox

.Value 属性不是强制性的,因为它是默认属性。此时可以用 MsgBox 打印出 intText1,2

MsgBox "Text1 = " & CStr(intText1)+ " - Text2 = " & CStr(intText2)

This will show Text1 = 200 - Text2 = 400

这将显示 Text1 = 200 - Text2 = 400

CASE 2 - CONTINUOUS FORMS VIEWLet's now assume that your view is Continuous form. So the field that contains 200 and 400 is just one but each record (row) is a form repeated as many times as the number of records. In this case to access all the records and store them to an array you can use this in the Form_Load event (you can access it by the Control Properties Window - F4)

案例 2 - 连续表单视图现在让我们假设您的视图是连续表单。因此,包含 200 和 400 的字段只是一个,但每条记录(行)都是一个重复次数与记录数相同的表格。在这种情况下,要访问所有记录并将它们存储到数组中,您可以在 Form_Load 事件中使用它(您可以通过控件属性窗口 - F4 访问它)

Option Explicit 
Option Base 1                      ' Set the base index for vectors to 1

Dim rst as DAO.Recordset           ' Define a recordset to allocate all query records
Dim Values as Variant              ' Define a variant to allocate all the values

set rst = me.RecordsetClone        ' Copy all records in rst

rst.MoveLast                       ' Go to last record
intNumRecords = rst.RecordCount    ' Count records
rst.MoveFirst                      ' Go back to recordset beginning

ReDim Values(intNumRecords)        ' Resize Values to allocate all values 

i = 1
Do While Not rst.EOF               ' Cycle over all records
    Values(i) = rst!FieldName      ' FieldName is the name of the field of
                                   ' the query that stores 200 and 400    
    i = i + 1                      ' Move to next array element
    rst.MoveNext                   ' Move to next record
Loop

rst.Close                          ' Close recordset
set rst = Nothing                  ' Release memory allocated to rst

for i = 1 To intNumRecords         ' Show easch value as message box
  MsgBox Values(i)
next i

NOTESPlease NOte that this solution works if you have less than 32767 records to show (the maximum integer with sign that you can store). The msgbox obliges you to press OK at each value. It's not so comfortable.

注意请注意,如果要显示的记录少于 32767 条(可以存储的带符号的最大整数),则此解决方案有效。msgbox 要求您在每个值处按 OK。它不是那么舒服。

Please tell me if it's what you were looking for.

请告诉我这是否是您要找的。

Bye, Wiz

再见,维兹