【发布时间】:2014-04-05 18:39:38
【问题描述】:
我正在编写一个循环遍历记录的 vba 代码,如果记录等于环境用户名,则在标签中显示的表单中显示找到的用户名的偏移记录。
到目前为止,我遇到了一堵砖墙,试图获取属于匹配用户名的值。
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM agentKPI")
'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Unnecessary in this case, but still a good habit
Do Until rs.EOF = True
'Perform an edit
rs.Edit
rs("staffName") = Environ$("username")
Form!agentKPI!label10.Caption
Form!agentKPI!label14.Caption
Form!agentKPI!label23.Caption
Form!agentKPI!label26.Caption
'rs!kpi1 = True
'rs("kpi1") = True 'The other way to refer to a field
'Save contact name into a variable
'sContactName = rs!staffName & " " & rs!staffID
'rs!kpi3 = sContactName
rs.Update
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
MsgBox ("Finished looping through records." & Environ$("username"))
rs.Close 'Close the recordset
Set rs = Nothing 'Clean up
【问题讨论】:
-
每个
Environ$("username")的表中是否可以有多个记录? -
目的是为登录的人显示 kpi 结果。所以它会找到一条记录
-
您只有 3 个 kpi:
kpi1,kpi2,kpi3,但有 4 个标签。第四个标签应该是什么?还有什么是agentKPI,表单还是子表单? -
agentkpi 是一个访问表单,第四个标签将显示代理名称
标签: loops ms-access vba ms-access-2007