在子表单访问中翻转记录

时间:2013-07-23 14:49:09

标签: ms-access-2007 access-vba recordset subform

基本上我想在有人在子窗体中更改它们时翻转两个记录中的行号值

所以如果我有线:

12345并且我重命名为5到3我希望5重新编号为3和3重新编号为5所以我会有12543但他们重新洗牌到12345但记录正确切换到位 但是我收到错误(见下文),如果我更改记录1,则找不到任何记录

我到目前为止的代码是:

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!OrderLineNumber.Value = recVal #Here
    DoCmd.GoToRecord , , acGoTo, recNum
    rst!OrderLineNumber.Value = i
rst.MoveNext
Loop
End Sub

并且在没有添加新内容或编辑

的情况下在#Here失败,错误更新或取消更新

1 个答案:

答案 0 :(得分:0)

在更改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