質問

Basically I want to flip the line number values in the two records when someone changes them in the subform

so if i have line:

12345 and I rename 5 to 3 I want 5 renumbered to 3 and 3 renumbered to 5 so I would have 12543 but they reshuffle to 12345 but the records switch places correctly However I get an error (see below) and if I change record 1 it can't find any records

the code I have so far is:

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

and is failing at #Here with error update or cancelupdate without add new or edit

役に立ちましたか?

解決

Before changing a value on a DAO record you need to run the Edit method. And then you need to run the Update method after changing it.

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

Here's a function I use that does something quite similar. I have a main form with buttons with up and down arrows on them, and a datasheet subform (continuous form would work too). Every row in the subform has a RowOrder field with a value in it starting from 1. These values get assigned as the user adds new records.

When the user wants to change the order of the records in the subform they just use the arrow buttons located on the main form. One thing not in my code here is checks for new record, or checks to see if they have no records entered. Another thing my function does not do is reorder or fix all the rows. It only affects the row in focus and the one above or below it. Here's the code:

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top