Automatically create records in subform and nested subform (1 Viewer)

DataBass

Registered User.
Local time
Today, 09:45
Joined
Jun 6, 2018
Messages
68
This should keep you happy for a while



Thanks again
 
Last edited:

DataBass

Registered User.
Local time
Today, 09:45
Joined
Jun 6, 2018
Messages
68
I've run into an interesting problem.

When I adapt this method to my current project I get an error when doing the append queries on a totally new record on the main form. It works fine if I append to an Item that previously existed.
It says it failed to append the records due to primary key violations. If I save the main record first, then append, it works.

Any idea why this might be happening? I'm guessing I need to start with fresh tables with no data because JHB's example works perfectly.
 

JHB

Have been here a while
Local time
Today, 18:45
Joined
Jun 17, 2012
Messages
7,732
It happens because the value from ItemID isn't in the table for new created records, (until it is saved).
How do you add/create the records in the subform, click a button or how?
 

DataBass

Registered User.
Local time
Today, 09:45
Joined
Jun 6, 2018
Messages
68
The new records are added as part of the After Update event when the cboItemType combo box is changed.
I forced it to save the record and it works, but I didn't think I should have to do that because your example works without saving the record.
 

JHB

Have been here a while
Local time
Today, 18:45
Joined
Jun 17, 2012
Messages
7,732
Maybe I can make a working without saving the record, if you post your database with some sample data, (I need the database because it is to much guessing how you've set it up).
 

DataBass

Registered User.
Local time
Today, 09:45
Joined
Jun 6, 2018
Messages
68
Thanks JHB,

I will post something later.

Do you have any idea why this isn't a problem with the sample database that you posted?
 

DataBass

Registered User.
Local time
Today, 09:45
Joined
Jun 6, 2018
Messages
68
JHB,

In the process of making the database "postable", I cleared out all the irrelevant database objects and deleted all the data.

Then, when I was making some sample records, the append functionality worked fine for the first 2 records. After that, every time I tried to append the records, I got the Primary Key violation error. I solved this by inserting:

Code:
DoCmd.RunCommand acCmdSaveRecord
I posted the database in another thread where we were discussing another problem with the same database.

You can see the code in the After Update event of cboJobType on frmProjectsNew.

If you wouldn't mind taking a look I would be grateful.

Thanks
 

JHB

Have been here a while
Local time
Today, 18:45
Joined
Jun 17, 2012
Messages
7,732
... every time I tried to append the records, I got the Primary Key violation error. I solved this by inserting:
The problem rise because of the relationships, you're trying to add records to table tblAccrual, before you've a ProjectId in the table tblProjectList.
So you've done the right thing by saving the record first.
I've put in another way to save it as the DoCmd.RunCommand acCmdSaveRecord command.
 

DataBass

Registered User.
Local time
Today, 09:45
Joined
Jun 6, 2018
Messages
68
Ahhh I see. You replaced it with:
Code:
If Me.Dirty Then
Me.Dirty=false
Good idea.

The advantage with this approach is that it only saves the record if the value has actually changed, right?

Thanks
 

JHB

Have been here a while
Local time
Today, 18:45
Joined
Jun 17, 2012
Messages
7,732
From the MS-Access Help-file:
You can use the Dirty property to determine whether the current record has been modified since it was last saved.
 

Users who are viewing this thread

Top Bottom