C
Christine Vollberg via AccessMonster.com
I have this procedure that I am using the Dmax function with to find the
max charge seq no for a particular defendant and then with an input request
from the user creating duplicate records the number of times requested by
the user. (This is a case management system for sentencing) If a
defendant has 30 counts of the same charge I need 30 records with the
charge seq no assigned 1 -30. The problem is it works the first time and
if I go back to the form again to say add 5 more it trys to repeat starting
at 10, which gives me a duplicate primary key error. Here is my code:
Private Sub cmdDupREc_Click()
Dim count As Integer
Dim sSql As String
Dim DupSql As String
Dim charseq As String
Dim DupSql2 As String
Dim DupSql3 As String
Dim DupSql4 As String
Dim RepeatValue As Integer
Dim db As DAO.Database
Set db = DBEngine(0)(0)
RepeatValue = InputBox("How Many Counts?", Counts)
Rem this statement goes and gets the highest ChargeSeqNo for this
defendant
charseq = DMax("[ChargeSeqNo]", "tblDefChargesSentence", "[CaseNo]= '"
& Me![CaseNo] & "' And [DefendantId]=" & Me![DefendantId]) + 1
count = RepeatValue - 1
Do While count > 0
DupSql = "INSERT INTO tblDefChargesSentence ( DefendantId, CaseNo,
ChargeCode, ChargeSeqNo, FiledOffense, LastName, FirstName" + IIf(IsNull
(SID), " ", ",SID") + IIf(IsNull(ATN), " ", ",ATN") + IIf(IsNull
(DateOfOffense), " ", ",DateOfOffense") + IIf(IsNull(DateOfArrest), " ",
",DateOfArrest") + IIf(IsNull(ArDate), " ", ",ArDate") + IIf(IsNull
(ConvictionDate), " ", ",ConvictionDate") + IIf(IsNull(PAttorney), " ",
",PAttorney") + IIf(IsNull(PAttorney2), " ", ",PAttorney2") + IIf(IsNull
(DAttorney), " ", ",DAttorney") + IIf(IsNull(DAttorney2), " ",
",DAttorney2")
DupSql2 = IIf(IsNull(TrialBy), " ", ",TrialBy") + IIf(IsNull
(DefPlea), " ", ",DefPlea") + IIf(IsNull(DateOfPlea), " ", ",DateOfPlea") +
IIf(IsNull(TypeChargeFM), " ", ",TypeChargeFM") + IIf(IsNull(ChargeTypeORA)
, " ", ",ChargeTypeORA") + ") values ( " + Str(DefendantId) + ",'" + CaseNo
+ "','" + ChargeCode + "','" + charseq + "','" + FiledOffense + "','" +
LastName + "','" + FirstName + "'" + IIf(IsNull(SID), " ", ",'" + SID + "'")
+ IIf(IsNull(ATN), " ", ",'" + ATN + "'")
DupSql3 = IIf(IsNull(DateOfOffense), " ", ",'" + Format
(DateOfOffense, "mm/dd/yyyy") + "'") + IIf(IsNull(DateOfArrest), " ", ",'"
+ Format(DateOfArrest, "mm/dd/yyyy") + "'") + IIf(IsNull(ArDate), " ", ",'"
+ Format(ArDate, "mm/dd/yyyy") + "'") + IIf(IsNull(ConvictionDate), " ",
",'" + Format(ConvictionDate, "mm/dd/yyyy") + "'") + IIf(IsNull(PAttorney),
" ", ",'" + PAttorney + "'") + IIf(IsNull(PAttorney2), " ", ",'" +
PAttorney2 + "'") + IIf(IsNull(DAttorney), " ", ",'" + DAttorney + "'")
DupSql4 = IIf(IsNull(DAttorney2), " ", ",'" + DAttorney2 + "'") +
IIf(IsNull(TrialBy), " ", ",'" + TrialBy + "'") + IIf(IsNull(DefPlea), " ",
",'" + DefPlea + "'") + IIf(IsNull(DateOfPlea), " ", ",'" + Format
(DateOfPlea, "mm/dd/yyyy") + "'") + IIf(IsNull(TypeChargeFM), " ", "," +
Str(TypeChargeFM)) + IIf(IsNull(ChargeTypeORA), " ", "," + Str
(ChargeTypeORA)) + ")"
sSql = DupSql + DupSql2 + DupSql3 + DupSql4
'display sql statement for testing purposes only
sql_text = sSql
db.Execute sSql, dbFailOnError
charseq = charseq + 1
count = count - 1
If count = 0 Then
Exit Do
End If
Loop
End Sub
max charge seq no for a particular defendant and then with an input request
from the user creating duplicate records the number of times requested by
the user. (This is a case management system for sentencing) If a
defendant has 30 counts of the same charge I need 30 records with the
charge seq no assigned 1 -30. The problem is it works the first time and
if I go back to the form again to say add 5 more it trys to repeat starting
at 10, which gives me a duplicate primary key error. Here is my code:
Private Sub cmdDupREc_Click()
Dim count As Integer
Dim sSql As String
Dim DupSql As String
Dim charseq As String
Dim DupSql2 As String
Dim DupSql3 As String
Dim DupSql4 As String
Dim RepeatValue As Integer
Dim db As DAO.Database
Set db = DBEngine(0)(0)
RepeatValue = InputBox("How Many Counts?", Counts)
Rem this statement goes and gets the highest ChargeSeqNo for this
defendant
charseq = DMax("[ChargeSeqNo]", "tblDefChargesSentence", "[CaseNo]= '"
& Me![CaseNo] & "' And [DefendantId]=" & Me![DefendantId]) + 1
count = RepeatValue - 1
Do While count > 0
DupSql = "INSERT INTO tblDefChargesSentence ( DefendantId, CaseNo,
ChargeCode, ChargeSeqNo, FiledOffense, LastName, FirstName" + IIf(IsNull
(SID), " ", ",SID") + IIf(IsNull(ATN), " ", ",ATN") + IIf(IsNull
(DateOfOffense), " ", ",DateOfOffense") + IIf(IsNull(DateOfArrest), " ",
",DateOfArrest") + IIf(IsNull(ArDate), " ", ",ArDate") + IIf(IsNull
(ConvictionDate), " ", ",ConvictionDate") + IIf(IsNull(PAttorney), " ",
",PAttorney") + IIf(IsNull(PAttorney2), " ", ",PAttorney2") + IIf(IsNull
(DAttorney), " ", ",DAttorney") + IIf(IsNull(DAttorney2), " ",
",DAttorney2")
DupSql2 = IIf(IsNull(TrialBy), " ", ",TrialBy") + IIf(IsNull
(DefPlea), " ", ",DefPlea") + IIf(IsNull(DateOfPlea), " ", ",DateOfPlea") +
IIf(IsNull(TypeChargeFM), " ", ",TypeChargeFM") + IIf(IsNull(ChargeTypeORA)
, " ", ",ChargeTypeORA") + ") values ( " + Str(DefendantId) + ",'" + CaseNo
+ "','" + ChargeCode + "','" + charseq + "','" + FiledOffense + "','" +
LastName + "','" + FirstName + "'" + IIf(IsNull(SID), " ", ",'" + SID + "'")
+ IIf(IsNull(ATN), " ", ",'" + ATN + "'")
DupSql3 = IIf(IsNull(DateOfOffense), " ", ",'" + Format
(DateOfOffense, "mm/dd/yyyy") + "'") + IIf(IsNull(DateOfArrest), " ", ",'"
+ Format(DateOfArrest, "mm/dd/yyyy") + "'") + IIf(IsNull(ArDate), " ", ",'"
+ Format(ArDate, "mm/dd/yyyy") + "'") + IIf(IsNull(ConvictionDate), " ",
",'" + Format(ConvictionDate, "mm/dd/yyyy") + "'") + IIf(IsNull(PAttorney),
" ", ",'" + PAttorney + "'") + IIf(IsNull(PAttorney2), " ", ",'" +
PAttorney2 + "'") + IIf(IsNull(DAttorney), " ", ",'" + DAttorney + "'")
DupSql4 = IIf(IsNull(DAttorney2), " ", ",'" + DAttorney2 + "'") +
IIf(IsNull(TrialBy), " ", ",'" + TrialBy + "'") + IIf(IsNull(DefPlea), " ",
",'" + DefPlea + "'") + IIf(IsNull(DateOfPlea), " ", ",'" + Format
(DateOfPlea, "mm/dd/yyyy") + "'") + IIf(IsNull(TypeChargeFM), " ", "," +
Str(TypeChargeFM)) + IIf(IsNull(ChargeTypeORA), " ", "," + Str
(ChargeTypeORA)) + ")"
sSql = DupSql + DupSql2 + DupSql3 + DupSql4
'display sql statement for testing purposes only
sql_text = sSql
db.Execute sSql, dbFailOnError
charseq = charseq + 1
count = count - 1
If count = 0 Then
Exit Do
End If
Loop
End Sub