Solved How to add a new record in subform on open? (1 Viewer)

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
Hi, all,
How to open in add mode (acFormAdd) subform "frmPSD" (on Tab control - page "psdTab") on Main form "frmInspection"?
Main form and subform are linked with Fields.
The Main form "frmInspection" is open by another form with the code:
Code:
Private Sub cboType_AfterUpdate()
   
  If Me.cboType = "PSD" Then
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.OpenForm "frmInspection", , , "[InspectionID]=" & Me.InspectionID
        DoCmd.GoToControl "psdTab"
        DoCmd.GoToControl "cboInspection"
        Exit Sub
    End If
   
End Sub
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 14:07
Joined
Oct 29, 2018
Messages
21,358
Hi. I would suggest going to a new record only (ready to add one), instead of actually creating a new record. Otherwise, you'll get a new record every time the form (subform) opens even if you didn't mean to.
 

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
Hi. I would suggest going to a new record only (ready to add one), instead of actually creating a new record. Otherwise, you'll get a new record every time the form (subform) opens even if you didn't mean to.
Need to open subform in acFormAdd
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:07
Joined
Oct 29, 2018
Messages
21,358
Need to open subform in acFormAdd
Opening a form using acFormAdd is not the same as actually automatically adding a new record. That's basically what I was trying to say. Using acFormAdd opens the form ready to accept new records, rather than automatically creating them.
 

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
Opening a form using acFormAdd is not the same as actually automatically adding a new record. That's basically what I was trying to say. Using acFormAdd opens the form ready to accept new records, rather than automatically creating them.
Уou are right. Do you know how can I do that?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,245
I'm not getting it. Unless you mean a new blank row in a datasheet, or continuous form or a normal form, ready to accept data? is that what you mean?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:07
Joined
Oct 29, 2018
Messages
21,358
Уou are right. Do you know how can I do that?
Hi. We are confused about what you want to do. Did you want the form to open blank/empty, or did you want the form to open and add new data automatically?
 

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
I'm not getting it. Unless you mean a new blank row in a datasheet, or continuous form or a normal form, ready to accept data? is that what you mean?
I apologize for the explanation. Here's what I want - that new record to be next new record created (new data) automatically,
 

Attachments

  • Untitled.png
    Untitled.png
    12.8 KB · Views: 289
Last edited:

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
Hi. We are confused about what you want to do. Did you want the form to open blank/empty, or did you want the form to open and add new data automatically?
Yes, I want the subform to open and add new data automatically
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,245
Yes, I want the subform to open and add new data automatically

In that case, and I think I already asked this, we need to know what the data is. Where is the data coming from?
 

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
In that case, and I think I already asked this, we need to know what the data is. Where is the data coming from?
I redesigned my database in english to explain clearly what I need. As well as where the data comes from.
As I said when I choose cboTypeOfInspecion = "PSD" (on form "subfrmInspections") and it is open form "frmInspection" and page tab "psdTab" (form "PSD"). I need when the form "frmInspection" is open, on that page tab "psdTab" with form "frmPSD" to be created automatically
new record on "psdID".
 

Attachments

  • DB_EN_v1.accdb
    2.4 MB · Views: 274
  • Pic.png
    Pic.png
    29.6 KB · Views: 255

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,245
Try:-
 

Attachments

  • DB_EN_v1.zip
    81.6 KB · Views: 272

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
Uncle Gizmo, I'am trying but I don't see any change. When I make a new entry in "subfrmInspections" and I choose "PSD" form cboTypeOfInspecion, it is open form "frmInspection" goes to page"psdTab" on form "frmPSD", BUT psdID is stays on "(New)". So I want to be created automatically next new record, just like acFormAdd
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:07
Joined
Oct 29, 2018
Messages
21,358
Still not sure I understand what you want but maybe try this one...
 

Attachments

  • DB_EN_v1.zip
    68.4 KB · Views: 256

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:07
Joined
Jul 9, 2003
Messages
16,245
Please Specify the Data to ADD!!!

See Video:-

DB_EN_v1​

 

tihmir

Registered User.
Local time
Today, 14:07
Joined
May 1, 2018
Messages
257
Please Specify the Data to ADD!!!

See Video:-

DB_EN_v1​

Wow, thank you, sir Uncle Gizmo, for your time and the prepared visual material on YouTube.
For the detailed explanation you made for me. Thank you and for you, theDBguy!
I appreciate your patience and the help you give me.
Тhe question I was trying to ask and explain is: Is it possible to filling automatically psdID
with the next record number?
Finally, after so much wandering and banging my head against a wall,
I think I've found a solution to the problem. I just added:
Code:
DoCmd.OpenForm "frmInspection", , , "[InspectionID]=" & Me.InspectionID, , , Me.InspectionID
and
Code:
Private Sub Form_Load()
        If Not IsNull(Me.OpenArgs) Then
        Me!frmPSD.Form!InspectionID = Me.OpenArgs
    End If
End Sub
and and voila. The code is working perfect!
Thank you again, sir Uncle Gizmo!
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 14:07
Joined
Oct 29, 2018
Messages
21,358
Wow, thank you, sir Uncle Gizmo, for your time and the prepared visual material on YouTube.
For the detailed explanation you made for me. Thank you and for you, theDBguy!
I appreciate your patience and the help you give me.
Тhe question I was trying to ask and explain is: Is it possible to filling automatically psdID
with the next record number?
Finally, after so much wandering and banging my head against a wall,
I think I've found a solution to the problem. I just added:
Code:
DoCmd.OpenForm "frmInspection", , , "[InspectionID]=" & Me.InspectionID, , , Me.InspectionID
and
Code:
Private Sub Form_Load()
        If Not IsNull(Me.OpenArgs) Then
        Me!frmPSD.Form!InspectionID = Me.OpenArgs
    End If
End Sub
and and voila. The code is working perfect!
Thank you again, sir Uncle Gizmo!
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:07
Joined
Feb 19, 2002
Messages
42,976
@tihmir ,
The reason for the questions is because it makes no sense at all to add "empty" records to a table. You NEVER want to save a record that has no data except a primary key. The proper technique is to have the form opened to the "new" record. The user types the values he wants. As soon as the user types the first character, Access automatically assigns the new record ID if you are using an autonumber. Then you should have code in the FORM's BeforeUpdate event to validate the data to make sure that all required fields are supplied and no fields with specific edit rules violate the rules. For example if you are capturing DOB, the date would never be > today. If the person hasn't already be born, you cannot predict when they will be born.

If you are using the Access Switchboard, you have the option of opening a form in different modes. Add, Edit, Read Only for example

A word to the wise - NEVER EVER dirty programmatically a record that they user has not first dirtied himself.
 

Users who are viewing this thread

Top Bottom