J
Joshua A. Booker
Hi there,
I use the following code to renumber lines on a bill of materials
AfterInsert and AfterDeleteConfirm of the form. This works very well. I'd
like to modify it to allow the user to change the sequence of the lines.
For example, they may add a new row (line 10 let's say) and want it inserted
between between lines 5 and 6. I'd like to allow the users to type 6 in the
line number field of the new row and have the code renumber the old 6
through 9 automatically. I have it working by calling this code in the
AfterUpdate of the LineNo field.
The problem is I need it to work in both directions. It should allow the
users to either increase or decrease the line number and renumber the other
lines accordingly. Surely someone as done this before.
Thanks in advance,
Josh
Original Code:
Private Sub RenumberLines_ORIG()
On Error GoTo Err_Sub
Dim rst As Recordset, intLineNo As Integer, intLines As Integer, varRet As
Variant
Set rst = Me.RecordsetClone
intLines = Me.RecordsetClone.RecordCount
DoCmd.Hourglass True
varRet = SysCmd(acSysCmdInitMeter, "Renumbering lines..", intLines)
rst.MoveFirst
Do Until rst.EOF
rst.Edit
intLineNo = intLineNo + 1
rst!LineNo = intLineNo
rst.Update
rst.MoveNext
varRet = SysCmd(acSysCmdUpdateMeter, intLineNo)
Loop
rst.Close
varRet = SysCmd(acSysCmdClearStatus)
DoCmd.Hourglass False
Exit_Sub:
Exit Sub
Err_Sub::
MsgBox Err.description
DoCmd.Hourglass False
varRet = SysCmd(acSysCmdClearStatus)
Resume Exit_Sub
End Sub
My Code:
Public Sub RenumberLines(Optional intOld As Integer, Optional intNew As
Integer)
On Error GoTo Renum_Error
Dim rst As Recordset, intLineNo As Integer, intLines As Integer, varRet As
Variant
Dim tfStart As Boolean, tfUp As Boolean, intStart As Integer
Set rst = Me.RecordsetClone
intLines = Me.RecordsetClone.RecordCount
DoCmd.Hourglass True
varRet = SysCmd(acSysCmdInitMeter, "Renumbering lines..", intLines)
rst.MoveFirst
If intNew > intOld Then tfUp = True
If tfUp Then
intLineNo = 0
intStart = intOld
tfStart = True
Else
intLineNo = 1
intStart = intNew
tfStart = False
End If
Do Until rst.EOF
If rst!LineNo = intStart Then
If Not tfStart Then
intLineNo = intLineNo + 1
tfStart = True
Else
tfStart = False
GoTo MoveNext
End If
Else
intLineNo = intLineNo + 1
End If
rst.Edit
rst!LineNo = intLineNo
rst.Update
MoveNext:
rst.MoveNext
varRet = SysCmd(acSysCmdUpdateMeter, intLineNo)
Loop
rst.Close
varRet = SysCmd(acSysCmdClearStatus)
DoCmd.Hourglass False
Me.Requery
Exit_Sub:
Exit Sub
Err_Sub:
MsgBox Err.Description
DoCmd.Hourglass False
varRet = SysCmd(acSysCmdClearStatus)
Exit Sub
End Sub
Private Sub LineNo_AfterUpdate()
Dim intOldVal As Integer
intOldVal = Me!LineNldValue
AutoSave
RenumberLines intOldVal, Me!LineNo
End Sub
I use the following code to renumber lines on a bill of materials
AfterInsert and AfterDeleteConfirm of the form. This works very well. I'd
like to modify it to allow the user to change the sequence of the lines.
For example, they may add a new row (line 10 let's say) and want it inserted
between between lines 5 and 6. I'd like to allow the users to type 6 in the
line number field of the new row and have the code renumber the old 6
through 9 automatically. I have it working by calling this code in the
AfterUpdate of the LineNo field.
The problem is I need it to work in both directions. It should allow the
users to either increase or decrease the line number and renumber the other
lines accordingly. Surely someone as done this before.
Thanks in advance,
Josh
Original Code:
Private Sub RenumberLines_ORIG()
On Error GoTo Err_Sub
Dim rst As Recordset, intLineNo As Integer, intLines As Integer, varRet As
Variant
Set rst = Me.RecordsetClone
intLines = Me.RecordsetClone.RecordCount
DoCmd.Hourglass True
varRet = SysCmd(acSysCmdInitMeter, "Renumbering lines..", intLines)
rst.MoveFirst
Do Until rst.EOF
rst.Edit
intLineNo = intLineNo + 1
rst!LineNo = intLineNo
rst.Update
rst.MoveNext
varRet = SysCmd(acSysCmdUpdateMeter, intLineNo)
Loop
rst.Close
varRet = SysCmd(acSysCmdClearStatus)
DoCmd.Hourglass False
Exit_Sub:
Exit Sub
Err_Sub::
MsgBox Err.description
DoCmd.Hourglass False
varRet = SysCmd(acSysCmdClearStatus)
Resume Exit_Sub
End Sub
My Code:
Public Sub RenumberLines(Optional intOld As Integer, Optional intNew As
Integer)
On Error GoTo Renum_Error
Dim rst As Recordset, intLineNo As Integer, intLines As Integer, varRet As
Variant
Dim tfStart As Boolean, tfUp As Boolean, intStart As Integer
Set rst = Me.RecordsetClone
intLines = Me.RecordsetClone.RecordCount
DoCmd.Hourglass True
varRet = SysCmd(acSysCmdInitMeter, "Renumbering lines..", intLines)
rst.MoveFirst
If intNew > intOld Then tfUp = True
If tfUp Then
intLineNo = 0
intStart = intOld
tfStart = True
Else
intLineNo = 1
intStart = intNew
tfStart = False
End If
Do Until rst.EOF
If rst!LineNo = intStart Then
If Not tfStart Then
intLineNo = intLineNo + 1
tfStart = True
Else
tfStart = False
GoTo MoveNext
End If
Else
intLineNo = intLineNo + 1
End If
rst.Edit
rst!LineNo = intLineNo
rst.Update
MoveNext:
rst.MoveNext
varRet = SysCmd(acSysCmdUpdateMeter, intLineNo)
Loop
rst.Close
varRet = SysCmd(acSysCmdClearStatus)
DoCmd.Hourglass False
Me.Requery
Exit_Sub:
Exit Sub
Err_Sub:
MsgBox Err.Description
DoCmd.Hourglass False
varRet = SysCmd(acSysCmdClearStatus)
Exit Sub
End Sub
Private Sub LineNo_AfterUpdate()
Dim intOldVal As Integer
intOldVal = Me!LineNldValue
AutoSave
RenumberLines intOldVal, Me!LineNo
End Sub