gakiss2
Registered User.
- Local time
- Today, 10:53
- Joined
- Nov 21, 2018
- Messages
- 168
Private Sub CtlRefNum_BeforeUpdate(Cancel As Integer)
Const MESSAGETEXT = "A record with this value already exists."
If Len(Me!CtlRefNum.Text) > 0 Then
If Not IsNull(DLookup("RefNum", "TblReports", "RefNum = """ & Me!CtlRefNum.Text & """")) Then
MsgBox MESSAGETEXT, vbExclamation, "Invalid operation"
Cancel = True
Me!CtlRefNum.Undo
DoCmd.FindRecord Me.CtlRefNum.Text
' How to get rid of the empty record left behind after this discovers it is a replacant
' DoCmd.GoToRecord , , acLast
' DoCmd.RunCommand acCmdDeleteRecord
End If
End If
End Sub
The above is code attached to before update on a field where 'RefNum' is entered. For now I am working on behavior if a new record is being entered using FrmReportEntry. If RefNum is a new number nothing happens and that is the desired behavior. If the entry for RefNum already exists then I want to jump to that form so it is added to / changed rather than make a new record. (yes, I tried just setting RefNum to 'no duplicates', it keeps you from saving the record and gives you the warning after you have wasted time entering data).
So from the user perspective, this works beautifully. The database jumps to record with the RefNum that was entered ready for additional information. The problem is that when this happens it leaves behind a near empty record consisting of only the fields the user entered before entering the RefNum which shows up early in the form. It makes a new one each time this code gets activated. Besides the ugliness of useless data, the near empty records show up elsewhere in other forms where they are not wanted. I tried to add code to go the last record and delete this unwated record but if I do that before I DoCmd.FindRecord then the record can't be found. If I try to do it after then the record I wanted to show goes away. I think this has something to do with trying to do the right thing on the right event but not sure. This may be obvious from the code but if take out the DoCmd.FindRecord then the near empty record is not created.
Another related issue is when I attempt to edit an existing record by changing the RefNum to one that already exists. This really shouldn't happen in practice but of course it could happen as an error or stupid user. I'd be OK in never allowing that field to be edited after initially entered but haven't looked for how to do that yet. I only mention it here in case both birds can be killed with one stone.
I have thought about a Delete Query but that would be new territory for me.
Thank You in advance
Const MESSAGETEXT = "A record with this value already exists."
If Len(Me!CtlRefNum.Text) > 0 Then
If Not IsNull(DLookup("RefNum", "TblReports", "RefNum = """ & Me!CtlRefNum.Text & """")) Then
MsgBox MESSAGETEXT, vbExclamation, "Invalid operation"
Cancel = True
Me!CtlRefNum.Undo
DoCmd.FindRecord Me.CtlRefNum.Text
' How to get rid of the empty record left behind after this discovers it is a replacant
' DoCmd.GoToRecord , , acLast
' DoCmd.RunCommand acCmdDeleteRecord
End If
End If
End Sub
The above is code attached to before update on a field where 'RefNum' is entered. For now I am working on behavior if a new record is being entered using FrmReportEntry. If RefNum is a new number nothing happens and that is the desired behavior. If the entry for RefNum already exists then I want to jump to that form so it is added to / changed rather than make a new record. (yes, I tried just setting RefNum to 'no duplicates', it keeps you from saving the record and gives you the warning after you have wasted time entering data).
So from the user perspective, this works beautifully. The database jumps to record with the RefNum that was entered ready for additional information. The problem is that when this happens it leaves behind a near empty record consisting of only the fields the user entered before entering the RefNum which shows up early in the form. It makes a new one each time this code gets activated. Besides the ugliness of useless data, the near empty records show up elsewhere in other forms where they are not wanted. I tried to add code to go the last record and delete this unwated record but if I do that before I DoCmd.FindRecord then the record can't be found. If I try to do it after then the record I wanted to show goes away. I think this has something to do with trying to do the right thing on the right event but not sure. This may be obvious from the code but if take out the DoCmd.FindRecord then the near empty record is not created.
Another related issue is when I attempt to edit an existing record by changing the RefNum to one that already exists. This really shouldn't happen in practice but of course it could happen as an error or stupid user. I'd be OK in never allowing that field to be edited after initially entered but haven't looked for how to do that yet. I only mention it here in case both birds can be killed with one stone.
I have thought about a Delete Query but that would be new territory for me.
Thank You in advance