在带有VBA代码的多个按钮(用于添加/编辑/删除数据)的MS Access表单中,每个按钮子例程末尾的代码刷新屏幕,以便用户在单击按钮之前看到最后一条记录。下面是其中一个按钮的请求代码示例:
Dim rst As Recordset, RecordToView As String
Set rst = Me.RecordsetClone
RecordToView = CStr(ProducerID)
Me.Requery
rst.FindFirst "ProducerID = " & RecordToView
Me.Bookmark = rst.Bookmark
rst.Close
此代码或类似版本的代码当前对窗体上的所有按钮重复。下面是创建泛型函数的尝试,这样就可以消除这种冗余代码:
Public Function RefreshScreen(Field As String, ByVal RecordToView As String)
Dim rst As Recordset
Set rst = Me.RecordsetClone
Me.Requery
rst.FindFirst Field & " = " & RecordToView
Me.Bookmark = rst.Bookmark
rst.Close
End Function
像这样叫:
Call RefreshScreen("ProducerID", ProducerID)
但是,当它击中泛型函数中的Me.RecordsetClone时,它会给出一个“编译错误: Me关键字的无效使用”消息。需要对泛型函数进行哪些修改才能使其工作?
发布于 2015-04-21 15:49:32
更改过程的声明,以期待对调用表单的引用。
Public Function RefreshScreen(ByRef frm As Form, ByVal fld As String, ByVal RecordToView As String)
Dim rst As DAO.Recordset
Set rst = frm.RecordsetClone
frm.Requery
rst.FindFirst fld & " = " & RecordToView
frm.Bookmark = rst.Bookmark
rst.Close
End Function
(顺便提一句,因为您没有从该函数返回一个值,所以它可能是一个子程序。但这在这里并不重要。)
然后在调用过程时包括Me
。
Call RefreshScreen(Me, "ProducerID", ProducerID)
我对这样的评论感到惊讶,它说Call()
在VBA中已经过时了。我以前从未听说过,但你可以在没有它的情况下调用这个程序:
RefreshScreen Me, "ProducerID", ProducerID
发布于 2015-04-21 15:52:00
你也许可以这样做:
Public Function RefreshScreen( _
ByRef FormObject As Form, _
Byval FieldName As String, _
ByVal RecordToView As Long)
Dim rst As Recordset
FormObject.Requery
Set rst = FormObject.RecordsetClone
If rst.RecordCount > 0 Then
rst.FindFirst FieldName & " = " & RecordToView & ""
If Not rst.NoMatch Then
FormObject.Bookmark = rst.Bookmark
End If
End If
rst.Close
Set rst = Nothing
End Function
然后:
RefreshScreen Me, "ProducerID", ProducerID
https://stackoverflow.com/questions/29776378
复制相似问题