Button only adds a new record if another record exists (1 Viewer)

5hadow

Member
Local time
Today, 06:52
Joined
Apr 26, 2021
Messages
89
Like the title says, when I try to add a new record to my subform via a button, it doesn't work UNLESS I manually enter some information in the first field then save it.

My button has the following code:

Code:
Private Sub btnSave_Click()
    With CurrentDb.OpenRecordset("tblObservation")
        If Not .BOF And Not .EOF Then
            .MoveLast
            .MoveFirst
            .AddNew
                !IQAID = fldIQAID
                ![fldCategory] = fldCategory.Value
                ![fldFinding] = fldFinding.Value
                ![fldAuditorNote] = fldAuditorNote.Value
                ![fldLAnote] = fldLAnote.Value
                ![fldAuditorID] = fldAuditorID
            .Update
        End If
    End With
    DoCmd.RunCommand acCmdSaveRecord
    subFrmObservation.Requery
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 03:52
Joined
Oct 29, 2018
Messages
21,467
Sounds like you're trying to create an orphan record, which is not allowed if you have referential integrity turned on.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:52
Joined
Feb 19, 2013
Messages
16,607
this line

If Not .BOF And Not .EOF Then

will only return true if a record already exists.

Perhaps that is why 'it does not work'

Might help if you clarify what you are trying to do
in what way doesn't it work? you get an error? if so what? or a wrong result? or something else
have you stepped through the code to check values?
what is the purpose of this action?
is tblObservation the recordsource to your subform or mainform?
are your forms unbound?
have you stepped through the code?
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:52
Joined
Sep 21, 2011
Messages
14,268
Why do you need an unbound form?
 

Users who are viewing this thread

Top Bottom