Code Not Adding Continous Second Record - Run-time error 3022 (1 Viewer)

Ashfaque

Student
Local time
Today, 14:11
Joined
Sep 6, 2004
Messages
894
Hi,

I have write conflict issue on adding next record.

When I add new record from form it records successfully. When I try to add another record after saving first one, Run-time error 3022 displays.

But strange is when I close the form and reopen it for adding new record…it works. I tried refreshing, Requering but not usefull.

Below is my code for on ADD button on my form.

Dim X As String
X = DCount("*", "T_IssuedWarnings", "Criteria = '" & Forms!F_IssuedWarnings!Criteria & "' And Cno = " & Forms!F_IssuedWarnings!CNo) + 1
MsgBox ("Total Warnings") & " - " & X
Me.WCounter = X
If (IsNull(Me.FirstTime) And IsNull(Me.SecondTime)) And (IsNull(Me.ThirdTime) And IsNull(Me.FourthTime)) Then
MsgBox ("This is ") & X
MsgBox ("Please Enter Action"), vbCritical
Exit Sub
Else
If Not IsNull(WarningID) Then

‘’’’’’’’ BELOW IS MAIN CODE WHERE FACTING PROBLEM..ALTHOUGH IT IS SAVING FIRST REC BUT NOT SAVING SECOND REC IN CONTINUATION. I HAVE TO CLOSE FORM AND THEN REOPEN AND PERFORM ADDITION..

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim fld As DAO.Field

Set rst = CurrentDb.OpenRecordset("Select * from T_IssuedWarnings Where WarningID=" & Me.WarningID)
If rst.EOF And rst.BOF Then
rst.AddNew
For Each fld In rst.Fields
rst(fld.name) = Me(fld.name)
Next fld

rst.Update
rst.Close
Set rst = Nothing

If (Eval("MsgBox ('Record Saved Successfully.@ @',64,' Bold Texts')")) = vbOKOnly Then
End If
End If
Me.CmdSaveWarnLetter.Enabled = False
Else

If (Eval("MsgBox ('Nothing To Save.@ @',64,' Bold Texts')")) = vbOKOnly Then
Me.CmdSaveWarnLetter.Enabled = False
Exit Sub
End If

End If

Me.CmdUpdateWarnLett.Enabled = True
Me.CboSearchWarn.Requery
Me.Refresh

End If
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,037
And 3022 error is?
Please also use code tags. You have been here long enough to know this helps us to help you?
 

Ashfaque

Student
Local time
Today, 14:11
Joined
Sep 6, 2004
Messages
894
And 3022 error is?
Please also use code tags. You have been here long enough to know this helps us to help you?
Yes I know I am here for long but I am still learning. I am not professional sir.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:41
Joined
Oct 29, 2018
Messages
21,357
Hi. I'm not sure why you are getting a problem. I can only think of two things.

1. If you want to use code to add records, you could use an unbound form, or

2. Use a bound form and don't use code.
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,037
Yes I know I am here for long but I am still learning. I am not professional sir.
I am talking about using this site, not your skill in VBA.
The error is (I took the trouble to look it up) is that a duplicate record would be created, so I suspect you are copying a field that is meant to be unique?. I am not even sure if you can overwrite an autonumber field, but you are copying ALL the fields.
Find out which have to be unique and omit them.
 

Ashfaque

Student
Local time
Today, 14:11
Joined
Sep 6, 2004
Messages
894
Your hint give me a way....I will check which field is duplicating I am sure it should be primary key field....let me check n I will come back....

Thanks theDBguy......:):)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:41
Joined
May 21, 2018
Messages
8,463
To make your code nice and readable it is the </> symbol which pops up a window

code.jpg

Then you can have indentations in your code which is lost otherwise
PHP:
Dim X As String
X = DCount("*", "T_IssuedWarnings", "Criteria = '" & Forms!F_IssuedWarnings!Criteria & "' And Cno = " & Forms!F_IssuedWarnings!CNo) + 1
MsgBox ("Total Warnings") & " - " & X
Me.WCounter = X
If (IsNull(Me.FirstTime) And IsNull(Me.SecondTime)) And (IsNull(Me.ThirdTime) And IsNull(Me.FourthTime)) Then
  MsgBox ("This is ") & X
  MsgBox ("Please Enter Action"), vbCritical
  Exit Sub
Else
If Not IsNull(WarningID) Then

‘’’’’’’’ BELOW IS MAIN CODE WHERE FACTING PROBLEM..ALTHOUGH IT IS SAVING FIRST REC BUT NOT SAVING SECOND REC IN CONTINUATION. I HAVE TO CLOSE FORM AND THEN REOPEN AND PERFORM ADDITION..

  Dim db As DAO.Database
  Dim rst As DAO.Recordset
  Dim fld As DAO.Field

  Set rst = CurrentDb.OpenRecordset("Select * from T_IssuedWarnings Where WarningID=" & Me.WarningID)
If rst.EOF And rst.BOF Then
  rst.AddNew
For Each fld In rst.Fields
  rst(fld.name) = Me(fld.name)
Next fld

  rst.Update
  rst.Close
  Set rst = Nothing

  If (Eval("MsgBox ('Record Saved Successfully.@ @',64,' Bold Texts')")) = vbOKOnly Then
End If
End If
Me.CmdSaveWarnLetter.Enabled = False
Else

If (Eval("MsgBox ('Nothing To Save.@ @',64,' Bold Texts')")) = vbOKOnly Then
  Me.CmdSaveWarnLetter.Enabled = False
  Exit Sub
End If

End If

Me.CmdUpdateWarnLett.Enabled = True
Me.CboSearchWarn.Requery
Me.Refresh

End If
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:41
Joined
Sep 21, 2011
Messages
14,037
@MajP

O/P has been here over 16 years, way longer than myself and many others. who do use code tags and report the actual error message?
 

Users who are viewing this thread

Top Bottom