2

我有一个连续的表单,页脚中有一个命令按钮,它使用一个值更新当前记录,一旦被请求,该记录将不再显示在表单中。我希望用户能够单击按钮,一旦记录更新,移动到下一条记录,而不是默认行为的第一条记录。我有我认为应该可以工作但没有工作的代码,它一直回到表单上的第一条记录。

Private Sub cmdCloseReq_Click()

Dim ReqID As String
ReqID = Me.txtReqID
Dim rst As Recordset
Dim strBookmark As Integer

Set rst = Me.RecordsetClone
rst.MoveNext
If Not rst.EOF Then                 ' if not end-of-file
  strBookmark = rst.Bookmark      ' ...save the next record's bookmark
  Dim cmd As New ADODB.Command
  With cmd
    .ActiveConnection = CurrentProject.Connection
    .CommandType = adCmdStoredProc
    .CommandText = "spUpdateLOG_ReqCompleteDate"
    .Parameters("@ReqID") = ReqID
    .Execute
  End With                 
Me.Requery
Me.Bookmark = strBookmark

End If
Set rst = Nothing   

End Sub

好的,我根据 rene 的帖子找到了解决方案。我抓住下一条记录的主键,进行更新,然后在请求后找到下一条记录并将书签设置为该记录。这是代码:

Private Sub cmdCloseReq_Click()

Dim ReqID As String
ReqID = Me.txtReqID
Dim rst As New ADODB.Recordset
Dim strBookmark As String

Set rst = Me.RecordsetClone

With rst
 .Find "[ReqID] = '" & ReqID & "'"
 .MoveNext
 strBookmark = rst.Fields(0)

End With

If Not rst.EOF Then                 ' if not end-of-file
      ' ...save the next record's bookmark

  Dim cmd As New ADODB.Command

  With cmd
    .ActiveConnection = CurrentProject.Connection
    .CommandType = adCmdStoredProc
    .CommandText = "spUpdateLOG_ReqCompleteDate"
    .Parameters("@ReqID") = ReqID
    .Execute
  End With                 ' ...delete the record

  Me.Requery

 Set rst = Me.RecordsetClone
 With rst
      .Find "[reqID]= " & strBookmark
    Me.Bookmark = .Bookmark

End With

Else
  With cmd
    .ActiveConnection = CurrentProject.Connection
    .CommandType = adCmdStoredProc
    .CommandText = "spUpdateLOG_ReqCompleteDate"
    .Parameters("@ReqID") = ReqID
    .Execute
  End With                 ' ...delete the record
Me.Requery

End If
Set rst = Nothing 
4

1 回答 1

1

我记得书签在重新查询后失效。如果您有一个主键,您可以更好地获取该主键,然后在重新查询后将当前记录移动到先前获得的主键

于 2012-12-06T20:46:23.180 回答