Hi,
I have a data entry form which opens default values each time the form is open. Requirement is that users can edit existing data in the form. When the form is closed, it shouldnt update the existing record but rather insert a new record.
On the Form-Beforeupdate function, I first check if the record already exists (Primary keys exist). Then I wrote a "Insert into" SQL that picks all values from the form and inserts into the backend table.
When I open the form and enter new Primary key values, I keep getting an error saying I'm trying to insert duplicate records. Funny part is, it inserts the new record in the table.
The logic I have is below:
Can someone help me with this please?
Thanks!
I have a data entry form which opens default values each time the form is open. Requirement is that users can edit existing data in the form. When the form is closed, it shouldnt update the existing record but rather insert a new record.
On the Form-Beforeupdate function, I first check if the record already exists (Primary keys exist). Then I wrote a "Insert into" SQL that picks all values from the form and inserts into the backend table.
When I open the form and enter new Primary key values, I keep getting an error saying I'm trying to insert duplicate records. Funny part is, it inserts the new record in the table.
The logic I have is below:
Code:
Set dbMyDB = OpenDatabase("J:\SMG3\SMG3.mdb")
Set rsMyRS = dbMyDB.OpenRecordset("tblProject", dbOpenDynaset)
If Not rsMyRS.EOF Then rsMyRS.MoveFirst
Do While Not rsMyRS.EOF
'Primary keys are ResourceName and Dateval
If Me.ResourceName = rsMyRS!ResourceName And Me.Dateval = rsMyRS!Dateval Then
Flag = "Y"
End If
rsMyRS.MoveNext
Loop
End If
If Flag = "Y" Then
CountVar = 1 'Not inserting the record if it exists. Just some Dummy variable
Else
DoCmd.RunSQL "INSERT INTO tblProject(ProjectName,....) VALUES (Forms!frmProjectEntry!ProjectName,....)"
'Cancel = True
'Me.Undo
End If
Can someone help me with this please?
Thanks!
Comment