Solved OpenArge error "You can't assign a value to this object" (1 Viewer)

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
I have seen another post where deleting the target control fixed the issue, but it hasn't worked for me unfortunately so here goes.

I have a combo box with PositionID, PositionName on a form with a listbox below which shows related training record to that PositionID. I have another form, a target form, which can be used to add further records in the listbox for that PositionID, if AddBtn is clicked. I have placed a textbox called PositionID in the form footer in the form footer and a combobox called TrainingName. This is so that I can add multiple trainings at once, to one PositionID. I have the following codes:

On the source form:

Code:
Private Sub AddBtn_Click()
    docmd.openform "frmTraining" , , , , , acFormAdd, Me.PositionID
End Sub

On the target form I have:

Code:
Private Sub  Form_Open(Cancel As Integer)
    Me.PositionID = Me.OpenArgs
End Sub

I get the following error when I click on AddBtn:

1686240419860.png


***Edit: I have just figured out that if I move to a new record in the target form, the PositionID in the footer goes back to 0. Is there a way to keep that same?*** Thanks
 
Last edited:

bob fitz

AWF VIP
Local time
Today, 08:35
Joined
May 23, 2011
Messages
4,726
I'm having trouble trying to visualise your form. Can you post a copy of the db.
 

Gasman

Enthusiastic Amateur
Local time
Today, 08:35
Joined
Sep 21, 2011
Messages
14,299
Do you have an expression on the control source of the target form control?
Generally I use the Load event for setting form controls.
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
I'm having trouble trying to visualise your form. Can you post a copy of the db.

I will have to edit a lot of things to post the db, unfortunately, but here are the images.

Source form:
1686240921684.png


Target form:
1686240955983.png


the textbox in the footer showing 103 is the PositionID from source form i.e., First Aider.
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
Do you have an expression on the control source of the target form control?
Generally I use the Load event for setting form controls.

Yes. Its bound to the table where the record for role vs training will be saved
 

bastanu

AWF VIP
Local time
Today, 00:35
Joined
Apr 13, 2010
Messages
1,402
Try to use the DefaultValue property instead:
Me.PositionID.DefaultValue = Me.OpenArgs
That should also work for subsequent new records in the same session.
Cheers,
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:35
Joined
Feb 19, 2002
Messages
43,275
The Open event happens before the Load event so there is no data in the form at this point. I think if the field you are trying to populate is bound, that is what causes this error.

You can use the Default property as @bastanu suggested OR you can use the form's BeforeInsert event rather than the Open event. The BeforeInsert event runs ONCE for each new record so all new records will end up with the correct FK (which would NOT happen if you used the Open event or the load event since they run only ONCE ever, rather than for each new record added). The other advantage of the BeforeInsert event is that it doesn't run until the user types something into the for so your code doesn't dirty the form as it would if you put the code in a different event.
 

Cronk

Registered User.
Local time
Today, 17:35
Joined
Jul 4, 2013
Messages
2,772
in #1, the code used
docmd.openform "frmTraining" , , , , , acFormAdd, Me.PositionID
has incorrect syntax

Use
docmd.openform "frmTraining" , , , , acFormAdd , , Me.PositionID

That is 4 commas before and 2 commas after, the acFormAdd rather than 5 and one
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
Try to use the DefaultValue property instead:
Me.PositionID.DefaultValue = Me.OpenArgs
That should also work for subsequent new records in the same session.
Cheers,

I get compile error that .DefaultValue method method or data member not found
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
The Open event happens before the Load event so there is no data in the form at this point. I think if the field you are trying to populate is bound, that is what causes this error.

You can use the Default property as @bastanu suggested OR you can use the form's BeforeInsert event rather than the Open event. The BeforeInsert event runs ONCE for each new record so all new records will end up with the correct FK (which would NOT happen if you used the Open event or the load event since they run only ONCE ever, rather than for each new record added). The other advantage of the BeforeInsert event is that it doesn't run until the user types something into the for so your code doesn't dirty the form as it would if you put the code in a different event.

Ahaa! BeforeInsert works perfect! I still got the same error as in the original post whether I tried Open or Load event, nor the default property worked. Thanks as ever, Pat!
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
in #1, the code used
docmd.openform "frmTraining" , , , , , acFormAdd, Me.PositionID
has incorrect syntax

Use
docmd.openform "frmTraining" , , , , acFormAdd , , Me.PositionID

That is 4 commas before and 2 commas after, the acFormAdd rather than 5 and one
yes you were right about that! thanks, Ive fixed that :)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:35
Joined
Feb 19, 2002
Messages
43,275
You're welcome:) Understanding the purpose of form events is very difficult since there is little documentation regarding when you should use one rather than another. Sometimes knowing what triggers them can help you decide but not always.

I created two videos a while back and decided to post the database I created to use with them. The videos concentrate on the Before and AfterUpdate events of the form as well as a couple of others so you might want to view them before you jump in to play with the database but the database has a form that shows you what events are running in the form you are working with. You can modify the forms I included or you can add your own. Just make sure to put the ONE line of code that logs each event as the first line of any event that you want to see fire. The syntax for the control events is slightly different from the form level events.

 

bastanu

AWF VIP
Local time
Today, 00:35
Joined
Apr 13, 2010
Messages
1,402
Glad to hear you solved your issue with BeforeInsert. I don't know how relevant the comment in your edit line in post #1 is now that you have it working, but if you want to give a visual clue to the user about the value about to get inserted (might be better to actually have a combo just like on the source form to show the description and not the ID) you would need to use the DefaultValue property of the control.

Is the name of the textbox in the footer PositionId or something else. In this context the DefaultValue property applies to a control not a field in the form's record source.

Cheers,
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
You're welcome:) Understanding the purpose of form events is very difficult since there is little documentation regarding when you should use one rather than another. Sometimes knowing what triggers them can help you decide but not always.

I created two videos a while back and decided to post the database I created to use with them. The videos concentrate on the Before and AfterUpdate events of the form as well as a couple of others so you might want to view them before you jump in to play with the database but the database has a form that shows you what events are running in the form you are working with. You can modify the forms I included or you can add your own. Just make sure to put the ONE line of code that logs each event as the first line of any event that you want to see fire. The syntax for the control events is slightly different from the form level events.

Thanks, Pat. I have seen these videos before and they are great help in clearing up quite a few concepts!
 

nashaz

Member
Local time
Today, 08:35
Joined
Mar 24, 2023
Messages
111
Glad to hear you solved your issue with BeforeInsert. I don't know how relevant the comment in your edit line in post #1 is now that you have it working, but if you want to give a visual clue to the user about the value about to get inserted (might be better to actually have a combo just like on the source form to show the description and not the ID) you would need to use the DefaultValue property of the control.

Is the name of the textbox in the footer PositionId or something else. In this context the DefaultValue property applies to a control not a field in the form's record source.

Cheers,
That actually resolves my ick as to why .defaultvalue was not working for me. Cheers, bastanu :)
 

Users who are viewing this thread

Top Bottom