Determine New Record and set Default Value

L

lcox400w

I have a subform where users will enter information on people. First a user
will enter information about an event on the main tab. Then the user selects
the person tab where the person sub-form is located. To ensure data is
entered properly, I need to ensure that certain types of people are entered
based on the incident (case type).

I have written the below code, but its not working correctly. I think I am
close, but could use a little help. Here is how the code should work.

If the casetype (main form) = Murder, then ONE of the person records MUST
have a "Victim". You can have multiple person records for an incident. If
the entire event is a new record, then generally the first record would be
new and the person type would be "victim". But my code changes each person
to "victim" if I try and enter more than 1 person record. also if its an old
record, i'm afraid it will erase the current person type.

In addition, there are some instances where a casetype will require 2
mandatory person types as shown below. I cant figure out how to get the code
to fill in both of those types. I think I may be approaching it wrong, in
that maybe I need to complete 1 person record, before the code goes onto
making a second record, but I cant figure out how to identify when the code
has completed 1 record and ready to start a new record so I can force the
mandatory person type in the second "new" record.

Private Sub PersonType_BeforeUpdate(Cancel As Integer)

If NewRecord = True Then
If Forms!frmmainentry.CaseType = "Murder" Then Me.PersonType.Value =
"Victim"
Me.HomicideNO.SetFocus
Me.PersonType.Enabled = False

ElseIf Forms!frmmainentry.CaseType = "OIS - Fatal" Then

Me.PersonType.Value = "OIS Decedent"
NewRecord.Me.PersonType.Value = "Officer"


ElseIf Forms!frmmainentry.CaseType = "ICD" Then Me.PersonType.Value =
"ICD Decedent"

End If

Else

End If
End Sub
 
S

Steve Sanford

I modified your code to use the "Select Case" syntax.

The code in the Case "OIS - Fatal" option is AIR CODE - it is untested.

'-------------- code beg------------------------
Private Sub PersonType_BeforeUpdate(Cancel As Integer)

Dim vCaseType As String
Dim sBM As String


vCaseType = Forms!frmmainentry.CaseType

If NewRecord = True Then

Select Case vCaseType

Case "Murder"
Me.PersonType = "Victim"
Me.HomicideNO.SetFocus
Me.PersonType.Enabled = False

Case "OIS - Fatal" ' UNTESTED!!!!
'first new record
Me.PersonType = "OIS Decedent"
'save it
If Me.Dirty Then
Me.Dirty = False
End If
'save pointer to current record
sBM = Me.Bookmark
'add another record
DoCmd.GoToRecord , , acNewRec
' or
' RunCommand acCmdRecordsGoToNew

'enter data in field
Me.PersonType = "Officer"
'save record
If Me.Dirty Then
Me.Dirty = False
End If
'go back to first record added
Me.Bookmark = sBM

Case "ICD"
Me.PersonType.Value = "ICD Decedent"

End Select
End If

End Sub
'-------------- code end------------------------




HTH
 
L

lcox400w

Steve,

The code is much cleaner than what I wrote. I give it a try and when I
select a new record based on OIS Fatal, it writes the OIS Decedent into the
first record, then goes into an endless loop and just keeps adding OIS
Decedent into new records over and over. I'm trying to troubleshoot the code
to see how to get it to stop after it does it for 1 record.
 
L

lcox400w

Dirk,

I just saw your post after trying out steves. I did change the event to an
enter event. It appears your code is working. It searches for an existing
person type first and if it doesnt find it, then it addes it to a record.
I'm still very new at this and thats obvious by my code vs yours and steve's.

Your help is greatly apprected by all of us.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top