Solved New Record in Subform and auto populate field (1 Viewer)

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
Hi all
I have a form with a tabular sub form, I have a button on the main form that the user clicks and when they do I want to automatically create a new record in the subform and to populate certain fields in the sub form how do I do this?
Currently running in a module

Main Form: FRMOrderDetails
Sub Form: SFRMCustomerDetails

I have tried this as an example but doesnt seem to work or creare onto a new record line
Code:
[Forms]![FRMOrderDetails]![SFRMCustomerDetails].[Form]![TxtOrderingDate]= NOW()

and

Code:
[Forms]![FFRMOrderDetails]![SFRMCustomerDetails].setfocus
DoCmd.GoToRecord , , acNewRec
[Forms]![FRMOrderDetails]![SFRMCustomerDetails].[Form]![TxtOrderingDate]= NOW()
 

Minty

AWF VIP
Local time
Today, 08:01
Joined
Jul 26, 2013
Messages
10,371
Do you get an error message, and if so what is it?
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
No error messages, just overwrites the current line
 

Minty

AWF VIP
Local time
Today, 08:01
Joined
Jul 26, 2013
Messages
10,371
Is the subform form set to allow additions?
 

ebs17

Well-known member
Local time
Today, 09:01
Joined
Feb 7, 2020
Messages
1,946
Code:
With Me.SFRMCustomerDetails.Form.Recordset
   .AddNew
   .Fields("TxtOrderingDate") = Now

   ' ... further instructions
   ' .Update
End With
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
This doesn't work in a module, runtime error 438, object doesnt support this property

Code:
With Me.SFRMCustomerDetails.Form.Recordset
   .AddNew
   .Fields("TxtOrderingDate") = Now

   ' ... further instructions
   ' .Update
End With
 

Minty

AWF VIP
Local time
Today, 08:01
Joined
Jul 26, 2013
Messages
10,371
Not sure what you mean by this?

This property in the subform need to be set to yes:
1702995913045.png
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
Hi,
This is the error I now get using the below, Run-time error 3000, Reserved error (-1104) There is no message for this error.

Code:
Public Function FUNC_OrderConfirmationt()
With Forms!FRMOrderDetails!SFRMCustomerDetails.Recordset
   .AddNew

   .Fields [Forms]![FRMOrderDetails]![SFRMCustomerDetails].[Form]![TxtOrderingDate]= NOW()

End With
End Function
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
- versus -

Clarity about WHERE you do WHAT is very helpful.

In a module, the internal reference to the current form (Me) must be replaced by a complete reference (Forms.FRMOrderDetails).
Hello,
as I said the button is on the main page and want the action to take place on the sub form named SFRMCustomerDetails?
 

ebs17

Well-known member
Local time
Today, 09:01
Joined
Feb 7, 2020
Messages
1,946
Code:
.Fields [Forms]![FRMOrderDetails]![SFRMCustomerDetails].[Form]![TxtOrderingDate]= NOW()
How do you come up with something like that?
And why do you act like it's my fault when you think of something that doesn't work?

If you copy true, it says
Code:
.Fields("TxtOrderingDate") = Now
That looks kind of different, doesn't it?
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
Hello,
I am not sure how you have made out like I am saying you are at fault I am asking for help, hence the original post

With regards to how I come up with that the code is in a module so I was under the impression I had to write exactly what form to look in sorry if this was wrong?

I have got the text to paste into the form now but just not into a new record line

How do you come up with something like that?
And why do you act like it's my fault when you think of something that doesn't work?

If you copy true, it says
Code:
.Fields("TxtOrderingDate") = Now
That looks kind of different, doesn't it?
 

ebs17

Well-known member
Local time
Today, 09:01
Joined
Feb 7, 2020
Messages
1,946
as I said the button is on the main page and want the action to take place on the sub form

If you can't just copy, what use would additional words be?
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
Hello,
I have solved the issue the code needed was, and seems to work fine

Code:
Public Function FUNC_OrderConfirmationt()
With Forms!FRMOrderDetails!SFRMCustomerDetails.Recordset
   .AddNew
   [Forms]![FRMOrderDetails]![SFRMCustomerDetails].[Form]![TxtOrderingDate]= NOW()

End With
End Function
 

stu_c

Registered User.
Local time
Today, 08:01
Joined
Sep 20, 2007
Messages
489
No need to be rude, your code helped just needed to change some bits
 

nector

Member
Local time
Today, 10:01
Joined
Jan 21, 2020
Messages
368
This will not work without grabbing the primary key from the parent table , I tried it personally it never worked unless you have a work of inserting the primary key into the sub form
 

Minty

AWF VIP
Local time
Today, 08:01
Joined
Jul 26, 2013
Messages
10,371
The FK will already be in the subform on a new record if the Master/Child properties are set correctly?
 

Users who are viewing this thread

Top Bottom