O
Ozzone
This is a repost. The original post is 4 pages down from this one entitled
"Deleteing a record causes key field to increment".
TreeId is a primary key field, and its initial value is typed on a form,
subsequent values are calculated based off what was typed in the first new
record for that session. This is what im trying to accomplish:
Open form, do nothing.
Goto new record. Type in first TreeID, followed by other data.
Update a global variable before the record loses focus.
New record gets focus. Check if this is a new record.
If it is, insert a calculated value based off the global variable.
If its not, do nothing.
Update the global variable before the record loses focus.
Rinse and repeat as needed...
In my original code (see original post), i had problems when deleting the
last record. With SteveS help, ive got the code tweaked to this:
Dim gstrLastTreeID As String
Dim gbRecordDeleted As Boolean
Private Sub Form_AfterDelConfirm(Status As Integer)
Select Case Status
Case acDeleteOK
gbRecordDeleted = True
End Select
End Sub
Private Sub Form_AfterUpdate()
' only update the global variable if TreeID has a value
If Not IsNull(Nz(Me![TreeID])) Then gstrLastTreeID = Me![TreeID]
End Sub
Private Sub Form_Current()
Dim strTempTreeID As String
Dim strBaseID As String
Dim intIncrementID As Integer
' assign the global to a private
strTempTreeID = gstrLastTreeID
If gbRecordDeleted = True Then
gbRecordDeleted = False
Exit Sub
Else
' if this is a new record...
If Me.NewRecord Then
' and if the global has not been set yet then...
If strTempTreeID = "" Then
Exit Sub
'but the global has been set...
Else
' then make the current TreeID one higher than the last one
strBaseID = Left$(strTempTreeID, 3)
intIncrementID = Val(Right(strTempTreeID, Len(strTempTreeID) -
3) + 1)
Me![TreeID] = strBaseID & IIf(intIncrementID < 10, "0" &
Format(intIncrementID), Format(intIncrementID))
End If
End If
End If
End Sub
As you can see, im using a flag to indicate whan a record has been deleted,
so i can exit the sub if thats the case. I want to put the Select statement
in an IF statetment to first test if its the last record, but i dont know how
to test for the last record on a form.
As it is, it's nearly working perfectly. After creating a few new records,
I can now delete the last record, however...
Problem #1
If i delete a group of records which includes the last record, the delete
confirmation never appears, so the AfterDelConfirm event never fires. Why is
this and how do i correct/compensate?
Problem #2
When i delete the last record by itself, the focus ends up in the new record
row with the TreeID field empty. I have to change records, then come back to
the new record for the TreeId value to be filled in. If i then delete that
record before creating another new one, the delete confirmation never
appears, so the AfterDelConfirm event never fires. If i move on and create a
2nd new record, then im back to Problem #1.
The most common reason for deleting the last record is tabbing thru and
having a new record created when you are finished with the data entry
session. Because TreeID is automatically filled in, that record must be
deleted. It would be very uncommon to have to delete multiple records that
includes the last record, but i want to fully understand whats happening
behind the scenes and be prepared for all possible scenarios.
Whats preventing the delete confirmation in the above scenarios? How do
check to see if im in the last record on a form?
All feedback welcomed, especially if you know an easier, softer way
Ozzone
"Deleteing a record causes key field to increment".
TreeId is a primary key field, and its initial value is typed on a form,
subsequent values are calculated based off what was typed in the first new
record for that session. This is what im trying to accomplish:
Open form, do nothing.
Goto new record. Type in first TreeID, followed by other data.
Update a global variable before the record loses focus.
New record gets focus. Check if this is a new record.
If it is, insert a calculated value based off the global variable.
If its not, do nothing.
Update the global variable before the record loses focus.
Rinse and repeat as needed...
In my original code (see original post), i had problems when deleting the
last record. With SteveS help, ive got the code tweaked to this:
Dim gstrLastTreeID As String
Dim gbRecordDeleted As Boolean
Private Sub Form_AfterDelConfirm(Status As Integer)
Select Case Status
Case acDeleteOK
gbRecordDeleted = True
End Select
End Sub
Private Sub Form_AfterUpdate()
' only update the global variable if TreeID has a value
If Not IsNull(Nz(Me![TreeID])) Then gstrLastTreeID = Me![TreeID]
End Sub
Private Sub Form_Current()
Dim strTempTreeID As String
Dim strBaseID As String
Dim intIncrementID As Integer
' assign the global to a private
strTempTreeID = gstrLastTreeID
If gbRecordDeleted = True Then
gbRecordDeleted = False
Exit Sub
Else
' if this is a new record...
If Me.NewRecord Then
' and if the global has not been set yet then...
If strTempTreeID = "" Then
Exit Sub
'but the global has been set...
Else
' then make the current TreeID one higher than the last one
strBaseID = Left$(strTempTreeID, 3)
intIncrementID = Val(Right(strTempTreeID, Len(strTempTreeID) -
3) + 1)
Me![TreeID] = strBaseID & IIf(intIncrementID < 10, "0" &
Format(intIncrementID), Format(intIncrementID))
End If
End If
End If
End Sub
As you can see, im using a flag to indicate whan a record has been deleted,
so i can exit the sub if thats the case. I want to put the Select statement
in an IF statetment to first test if its the last record, but i dont know how
to test for the last record on a form.
As it is, it's nearly working perfectly. After creating a few new records,
I can now delete the last record, however...
Problem #1
If i delete a group of records which includes the last record, the delete
confirmation never appears, so the AfterDelConfirm event never fires. Why is
this and how do i correct/compensate?
Problem #2
When i delete the last record by itself, the focus ends up in the new record
row with the TreeID field empty. I have to change records, then come back to
the new record for the TreeId value to be filled in. If i then delete that
record before creating another new one, the delete confirmation never
appears, so the AfterDelConfirm event never fires. If i move on and create a
2nd new record, then im back to Problem #1.
The most common reason for deleting the last record is tabbing thru and
having a new record created when you are finished with the data entry
session. Because TreeID is automatically filled in, that record must be
deleted. It would be very uncommon to have to delete multiple records that
includes the last record, but i want to fully understand whats happening
behind the scenes and be prepared for all possible scenarios.
Whats preventing the delete confirmation in the above scenarios? How do
check to see if im in the last record on a form?
All feedback welcomed, especially if you know an easier, softer way
Ozzone