Pass text box value to new record on another form (1 Viewer)

hllary

Registered User.
Local time
Today, 05:51
Joined
Sep 23, 2019
Messages
80
I have a form (frm_1) that has an event if the txtbox value equals "no", then it opens another form (frm_2) on new record.

Code:
Private Sub txtYesNo_AfterUpdate()
    
    If Me.txtYesNo.Value = "no" Then
    DoCmd.OpenForm "LatePR_frm", , , , acFormAdd
    End If
End Sub

frm_2 opens on a new record but I would like the Program Name value in frm_1 to be copied into the Program Name field in the new record in frm_2. I've tried the code below but I can't get it to work. The only time frm_2 will open will be from the frm_1.

Code:
Private Sub Form_Open(Cancel As Integer)
    If Me.NewRecord Then
        Me.txtProgramName = Form([PR_Reporting_frm]).[txtProgramName]
    End If
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:51
Joined
Oct 29, 2018
Messages
21,477
Hi. Try it this way:

Me.txtProgramName = Forms![PR_Reporting_frm].[txtProgramName]

Hope it helps...
 

hllary

Registered User.
Local time
Today, 05:51
Joined
Sep 23, 2019
Messages
80
Hi. Try it this way:

Me.txtProgramName = Forms![PR_Reporting_frm].[txtProgramName]

Hope it helps...

Thank you but it's still not copying the Program name from frm_1 into the frm_2 Program Name field in the new record.

1586805654490.png
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:51
Joined
Oct 29, 2018
Messages
21,477
Thank you but it's still not copying the Program name from frm_1 into the frm_2 Program Name field in the new record.

View attachment 80873
Hi. Did you try stepping through the code to make sure it's firing and going through the If/Then block properly? Another way to pass data to a form is by using the OpenArgs argument of the OpenForm method. Maybe you could give that approach a try.
 

hllary

Registered User.
Local time
Today, 05:51
Joined
Sep 23, 2019
Messages
80
Hi. Did you try stepping through the code to make sure it's firing and going through the If/Then block properly? Another way to pass data to a form is by using the OpenArgs argument of the OpenForm method. Maybe you could give that approach a try.

Yes, it's going through the If/Then block properly. I tried the OpenArgs from some examples I found but now I'm getting an "You can't assign a value to this object" error. Below is what I changed my code to.

The code for the if:
Code:
Private Sub txtYesNo_AfterUpdate()
   
    If Me.txtYesNo.Value = "no" Then
    DoCmd.OpenForm "LatePR_frm", acNormal, , , acFormAdd, acWindowNormal,  OpenArgs:=Me.txtProgramName
    End If
End Sub

frm_2
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.txtProgramName.Value = Me.OpenArgs
    
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:51
Joined
Oct 29, 2018
Messages
21,477
Yes, it's going through the If/Then block properly. I tried the OpenArgs from some examples I found but now I'm getting an "You can't assign a value to this object" error. Below is what I changed my code to.

The code for the if:
Code:
Private Sub txtYesNo_AfterUpdate()
  
    If Me.txtYesNo.Value = "no" Then
    DoCmd.OpenForm "LatePR_frm", acNormal, , , acFormAdd, acWindowNormal,  OpenArgs:=Me.txtProgramName
    End If
End Sub

frm_2
Code:
Private Sub Form_Open(Cancel As Integer)
    Me.txtProgramName.Value = Me.OpenArgs
   
End Sub
Hi. With regards to the "can't assign" error. Try moving the focus to another control first.
 

hllary

Registered User.
Local time
Today, 05:51
Joined
Sep 23, 2019
Messages
80
Hi. With regards to the "can't assign" error. Try moving the focus to another control first.

Thanks for you help. It works, I changed the code to:

Code:
Private Sub Form_Open(Cancel As Integer)
    If Not IsNull(Me.OpenArgs) Then
    DoCmd.GoToRecord , , acNewRec
    Me.txtProgramName = Me.OpenArgs
    
    End If
    
End Sub
 

theDBguy

I’m here to help
Staff member
Local time
Today, 05:51
Joined
Oct 29, 2018
Messages
21,477
Thanks for you help. It works, I changed the code to:

Code:
Private Sub Form_Open(Cancel As Integer)
    If Not IsNull(Me.OpenArgs) Then
    DoCmd.GoToRecord , , acNewRec
    Me.txtProgramName = Me.OpenArgs
    
    End If
    
End Sub
Hi. Congratulations! Good luck with your project.
 

Users who are viewing this thread

Top Bottom