MS Access如何更新当前行,移动到下一条记录,而不是第一条

时间:2012-12-05 19:35:58

标签: vba ms-access access-vba

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

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 

1 个答案:

答案 0 :(得分:1)

我记得在Requery之后书签无效。如果你有一个主键,你可以更好地抓住那个主键,并在重新查询后将当前记录移动到先前获得的主键