app访问日志 记录字段
基本上有4种方法可用于访问记录集中的字段值。 为了演示的目的,我将使用一个名为tblEmployees的表,一个名为[LastName]的字段和一个DAO记录集,尽管这些方法同样适用于ADO记录集。 下面的代码将使用所有这四种方法在雇员(tblEmployees)表中打印所有姓氏([LastName])值。
Dim MyDB As DAO.Database, MyRS As DAO.Recordset
Dim strFieldName As String
strFieldName = "LastName"
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("tblEmployees", dbOpenForwardOnly)
Do While Not MyRS.EOF
'Method 1 - the Bang or Exclamation Method
Debug.Print MyRS![LastName]
'Method 2 - the String Method
Debug.Print MyRS.Fields("LastName")
'Method 3 - the Index Method (not recommended - see Note #2)
Debug.Print MyRS.Fields(2)
'Method 4 - the Variable Method
Debug.Print MyRS.Fields(strFieldName)
MyRS.MoveNext
Loop
MyRS.Close
NOTE: All 4 Methods will produce the same results.
笔记2:
通过使用索引在记录集中检索字段的值,就可以引用对象(字段)的集合(字段)中的位置编号(索引)。 该索引号从0开始,以集合中的项数结束-1。由于[LastName]是tblEmployees中定义的第三个字段,因此将使用索引2(从零开始)来检索其值。 当对象添加到集合中或从集合中删除时,其他对象的位置可能会受到影响,因为当集合发生更改时Access会自动更新索引号。 因此,索引方法不应用于检索记录集中的字段值。
翻译自: https://bytes.com/topic/access/insights/635678-accessing-field-values-recordsets
app访问日志 记录字段