参考列表框按字段名称列 [英] Reference List Box Column by Field Name

查看:107
本文介绍了参考列表框按字段名称列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MS Access表单,在该表单中,列表框项目的选择确定了应用程序的其他几个方面.

I have an MS Access form in which selection of a list box item determines how many other aspects of the application behave.

我有一个表,其中包含ID和说明以及与应用程序其他方面的设置相对应的其他几个字段;该表是列表框的Row Source.

I have a table containing an ID and description and several other fields which correspond to settings for other aspects of the application; this table is the Row Source of the list box.

配置列表框以使前两列(ID和说明)可见,其余列的宽度设置为0-因此,列表框的Column Widths属性类似于:

The list box is configured such that the first two columns (ID & Description) are visible, with the widths of the remaining columns set to 0 - the Column Widths property of the list box is therefore something like:

1cm;5cm;0cm;0cm;0cm;0cm;0cm;0cm;0cm;0cm;0cm;0cm;0cm

然后在我的应用程序中,我使用以下表达式引用其余字段:

Within my application I then reference the remaining fields using expressions such as:

MyListBox.Column(n)

但是,这在很大程度上取决于表中字段的顺序,如果我随后决定将另一个字段添加到表中,则必须:

However, this relies heavily on the order of the fields in the table, and if I then decide to add another field to my table, I have to:

  • 增加列表框的列数"
  • 在以分号分隔的列宽度中再添加一个零
  • 根据表中新字段的添加位置,可能会更改各种Column(n)语句中使用的标识符.
  • Increment the list box Column Count
  • Add another zero to the semi-colon delimited Column Widths
  • Potentially change the identifiers used in the various Column(n) statements, depending on where the new field was added in the table.

使用此方法的最初原因是避免需要重复打开RecordSet来获取与从列表框中选择的项相关联的其他字段的值.

My original reason for using this approach was to avoid the need to repeatedly open a RecordSet to obtain the values of the additional fields associated with the item selected from the list box.

是否可以通过字段名称而不是列号来引用列表框字段?

(或者是否有一种更好的方法来完成此任务?)

(Or is there an entirely better way to approach this task?)

推荐答案

虽然我听到很多不,不可能,但这实际上对于具有行源类型的列表框是可能的通过使用ListBox.RecordSet属性的 Table/Query 来实现.

While I'm hearing a lot of No, not possible, this actually is possible for list boxes that have a row source type of Table/Query, by using the ListBox.RecordSet property.

对于组合框,这很容易,因为记录集随所选值一起移动.对于列表框,它不是,所以您将必须知道ID列的名称和位置(或者,如果ID列是绑定列,则只需使用.Value属性)

For combo boxes, this is easy, since the recordset moves along with the selected value. For list boxes, it doesn't, so you will have to know the name and position of the ID column (or, if the ID column is the bound column, just use the .Value property)

示例:

Dim rs As DAO.Recordset
Set rs = Me.List1.Recordset.Clone
rs.FindFirst "ID = " & Me.List1.Value
Debug.Print rs.Fields!Field1

添加代码以检查是否选择了值,是否设置了recordset属性以及是否需要找到匹配项.

Add code to check if a value is selected, if the recordset property is set, and if a match is found if needed.

这篇关于参考列表框按字段名称列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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