在更改 DAO 记录上的值之前,您需要运行 Edit 方法。然后您需要在更改后运行 Update 方法。
Private Sub OrderLineNumber_AfterUpdate()
Dim rst As DAO.Recordset
Set rst = Me.Recordset
Dim recNum As Integer
Dim recVal As Double
Dim move As Integer
Dim i As Integer
recNum = Me.CurrentRecord
Me.Requery
DoCmd.GoToRecord , , acGoTo, recNum
recVal = rst!OrderLineNumber.Value
rst.MoveFirst
Do Until rst.EOF
i = rst!OrderLineNumber.Value
If i = recVal Then
move = Me.CurrentRecord
End If
rst.Edit
rst!OrderLineNumber.Value = recVal #Here
rst.Update
DoCmd.GoToRecord , , acGoTo, recNum
rst.Edit
rst!OrderLineNumber.Value = i
rst.Update
rst.MoveNext
Loop
End Sub
这是我使用的一个功能非常相似的功能。我有一个带有向上和向下箭头按钮的主表单,还有一个数据表子表单(连续表单也可以)。子表单中的每一行都有一个 RowOrder 字段,其中的值从 1 开始。这些值在用户添加新记录时被分配。
当用户想要更改子表单中记录的顺序时,他们只需使用位于主表单上的箭头按钮。我的代码中没有的一件事是检查新记录,或者检查他们是否没有输入记录。我的函数不做的另一件事是重新排序或修复所有行。它只影响焦点所在的行以及它上面或下面的行。这是代码:
Private Sub cmdUp_Click()
'Put an "Up" button on one of your forms and pass the function a correct form object
Call MoveCurrentRecordUpOrDown("Up", Me.subform1.Form)
End Sub
Private Sub cmdDown_Click()
'Put a "Down" button on one of your forms and pass the function a correct form object
Call MoveCurrentRecordUpOrDown("Down", Me.subform1.Form)
End Sub
Public Function MoveCurrentRecordUpOrDown(sDirection As String, frm As Form)
Dim lPos As Integer
Dim iCurRowOrder As Integer
iCurRowOrder = Nz(frm!RowOrder, 0)
'Check to see if the record is already up against one of the ends
Dim iChange As Integer
Select Case sDirection
Case "Up"
If iCurRowOrder = 1 Then Exit Function 'Cannot move record up
iChange = -1
Case "Down"
If iCurRowOrder = frm.Recordset.RecordCount Then Exit Function 'Cannot move record down
iChange = 1
End Select
lPos = frm.Recordset.AbsolutePosition + iChange
Dim rs As DAO.Recordset
Set rs = frm.RecordsetClone
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
If rs!RowOrder = iCurRowOrder Then
rs.Edit
rs!RowOrder = iCurRowOrder + iChange
rs.Update
ElseIf rs!RowOrder = (iCurRowOrder + iChange) Then
rs.Edit
rs!RowOrder = iCurRowOrder
rs.Update
End If
rs.MoveNext
Loop
frm.Requery
If lPos > (frm.Recordset.RecordCount - 1) Then
lPos = (frm.Recordset.RecordCount - 1)
End If
frm.Recordset.AbsolutePosition = lPos
End If
rs.Close
Set rs = Nothing
End Function